Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 08-10-2024
navbuoy navbuoy is offline
Miembro
 
Registrado: mar 2024
Posts: 360
Poder: 3
navbuoy Va por buen camino
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:

Responder Con Cita
 


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Herramienta Grafica para MySQL La__X MySQL 15 31-05-2007 21:00:29
FANTASIA MITOLOGICA COLOMBIANA La aventura de zue el_barto Noticias 8 17-08-2005 02:40:48
De la famosa aventura del barco encantado dec Varios 1 21-07-2005 22:46:29
interfaz grafica para los usuarios dmagui Firebird e Interbase 5 27-05-2005 18:22:57
componente para gráfica Maga84 OOP 4 29-04-2005 17:36:47


La franja horaria es GMT +2. Ahora son las 16:09:17.


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
Copyright 1996-2007 Club Delphi