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 <raymath.h>
// Player controlled entity
class Pacman {
const int speed = 16;
const int speed = 64;
private:
Texture2D texture;
Vector2 position;
@ -12,7 +13,7 @@ class Pacman {
Rectangle getTextureRect(){
Rectangle result;
result.x = 0;
result.y = facing;
result.y = facing*16;
result.height = 16;
result.width = 16;
return result;
@ -21,6 +22,12 @@ class Pacman {
Pacman() {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = {0.,0.};
}
Pacman(Vector2 position) {
this->texture = LoadTexture("assets/sprites/pacman.png");
this->facing = 0;
this->position = position;
}
~Pacman() {
UnloadTexture(this->texture);
@ -29,13 +36,21 @@ class Pacman {
void process() {
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
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));
// Drawing
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
}
};
int main() {
@ -46,11 +61,13 @@ int main() {
SetTargetFPS(60);
Pacman pacman;
Pacman pacman = Pacman(Vector2{screen_width/2.0,screen_height/2.0});
while (WindowShouldClose() == false) {
BeginDrawing();
ClearBackground(BLACK);
pacman.process();
EndDrawing();