pacman movement

This commit is contained in:
rendo 2026-03-10 12:46:16 +05:00
commit a658978d62

View file

@ -2,8 +2,9 @@
#include <cmath> #include <cmath>
#include <raymath.h> #include <raymath.h>
// Player controlled entity
class Pacman { class Pacman {
const int speed = 16; const int speed = 64;
private: private:
Texture2D texture; Texture2D texture;
Vector2 position; Vector2 position;
@ -12,7 +13,7 @@ class Pacman {
Rectangle getTextureRect(){ Rectangle getTextureRect(){
Rectangle result; Rectangle result;
result.x = 0; result.x = 0;
result.y = facing; result.y = facing*16;
result.height = 16; result.height = 16;
result.width = 16; result.width = 16;
return result; return result;
@ -21,6 +22,12 @@ class Pacman {
Pacman() { Pacman() {
this->texture = LoadTexture("assets/sprites/pacman.png"); this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0; this->facing = 0;
this->position = {0.,0.};
}
Pacman(Vector2 position) {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = position;
} }
~Pacman() { ~Pacman() {
UnloadTexture(this->texture); UnloadTexture(this->texture);
@ -28,14 +35,22 @@ class Pacman {
void process() { void process() {
float delta = GetFrameTime(); float delta = GetFrameTime();
// Input handling
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {this->facing=1;}
if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {this->facing=2;}
if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {this->facing=3;}
if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {this->facing=0;}
// Movement in direction // Movement in direction
double angle = PI/2.0*facing; double angle = PI/2.0*facing;
Vector2 direction = {(float)(cos(angle)),(float)(sin(angle))}; Vector2 direction = {(float)(cos(angle)),(float)(sin(-angle))};
this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed)); this->position = Vector2Add(this->position,Vector2Scale(direction, delta * (float)speed));
// Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE); DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
} }
}; };
int main() { int main() {
@ -46,10 +61,12 @@ int main() {
SetTargetFPS(60); SetTargetFPS(60);
Pacman pacman; Pacman pacman = Pacman(Vector2{screen_width/2.0,screen_height/2.0});
while (WindowShouldClose() == false) { while (WindowShouldClose() == false) {
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK);
pacman.process(); pacman.process();