Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   ejemplo para Aventura Grafica Básica (https://www.clubdelphi.com/foros/showthread.php?t=96888)

navbuoy 08-10-2024 11:17:33

ejemplo para Aventura Grafica Básica
 
recientemente, anteayer concretamente, un chaval me preguntó como hacer para mover un Sprite asi con el teclado y caminar con el, asi que le hice este codigo de ejemplo en C++ builder para que lo adaptase a su lenguaje

Código:


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "acPNG"
#pragma resource "*.dfm"
TForm1 *Form1;


//DEFINED GLOBAL VARIABLES

bool moveLeft = false;  // Variable to move left
bool moveRight = false; // Variable to move right
int frameIndex = 0;    // Current frame index
int spriteX = 510;      // Initial position of the sprite on the X axis
int spriteY = 310;      // Initial position of the sprite on the Y axis
int totalFrames = 6;  // Number of frames in the animation
TBitmap* spriteFrames[6]; // Array to store the sprite frames

/// WE DECLARE THE CUSTOM FUNCTIONS WE CREATED
void CopiarImagen(TImage* origen, TImage* destino);
void CopiarImagenTransparente(TImage* origen, TImage* destino, TRect destinoRegion, TColor colorTransparente);

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)

{
        if (Key == VK_LEFT) {
                moveLeft = true;
        }
        if (Key == VK_RIGHT) {
                moveRight = true;
        }

                  // Movement control
        if (moveLeft) {
                spriteX -= 5;  // Move left 5 pixels (you can customize this)
        }
        if (moveRight) {
                spriteX += 5;  // Move right 5 pixels (also you can customize this)
        }


}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
        if (Key == VK_LEFT) {
                moveLeft = false;
        }
        if (Key == VK_RIGHT) {
                moveRight = false;
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
        for(int i = 0; i < totalFrames; i++) {
                spriteFrames[i] = new TBitmap();
                spriteFrames[i]->LoadFromFile("Imagen" + IntToStr(i) + ".bmp");
        }
        SpriteImage->Picture->Bitmap = spriteFrames[0];  // Load on SpriteImage the first image

        SpriteImage->Transparent = true;  // We activate the transparency white background for view only the sprite drawed

        Timer1->Interval = 80;  // We set 80 miliseconds for animation speed

        Form1->DoubleBuffered = true;  //This avoid blinking on the re-drawings of sprite or any redraw in app

        Form1->KeyPreview = true; //This is particular from C++ Builder for recognize the keypressing on keyboard
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{


        // Update sprite position
        SpriteImage->Left = spriteX;
        // Animation control
        frameIndex = (frameIndex + 1) % totalFrames;  // Cycle through the frames

        CopiarImagen(BackgroundImage, ScreenImage);
        SpriteImage->Picture->Bitmap = spriteFrames[frameIndex];  // Display the current frame
        CopiarImagenTransparente(SpriteImage, ScreenImage, TRect(spriteX, spriteY, spriteX+110, spriteY+156), clWhite);
}

//---------------------------------------------------------------------------
 void CopiarImagen(TImage* origen, TImage* destino) {
        // Asegúrate de que el tamaño del lienzo destino sea el mismo que el origen
        destino->Width = origen->Width;
        destino->Height = origen->Height;
        // Copiar la imagen del TImage origen al TImage destino utilizando el canvas
        destino->Canvas->Draw(0, 0, origen->Picture->Graphic);
}
//---------------------------------------------------------------------------
void CopiarImagenTransparente(TImage* origen, TImage* destino, TRect destinoRegion, TColor colorTransparente) {
    HDC hdcDestino = destino->Canvas->Handle;
    HDC hdcOrigen = origen->Picture->Bitmap->Canvas->Handle;
        // Copia el bitmap desde 'origen' a 'destino' haciendo que el colorTransparente sea transparente
    TransparentBlt(hdcDestino,
                  destinoRegion.Left, destinoRegion.Top, destinoRegion.Width(), destinoRegion.Height(),  // Región de destino
                                  hdcOrigen,
                                  0, 0, origen->Picture->Bitmap->Width, origen->Picture->Bitmap->Height,  // Tamaño de origen
                                  colorTransparente);  // Color a hacer transparente
}
//---------------------------------------------------------------------------

https://www.quazardev.net/Stardust/t..._animation.rar

El resultado es este:


navbuoy 08-10-2024 11:19:41

una mejora que le dije que podria hacer, seria establecer un control de planos en eje virtual de profundidad para por ejemplo hacer que el sprite pase por detras del arbol que sale a la izquierda


La franja horaria es GMT +2. Ahora son las 13:36:37.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi