galaga/space invaders game movement
in
Programming Questions
•
2 years ago
hello i am new to the forums.
so i have to do a project, which is to make a simple galaga/ space invaders clone, i managed to get the thing working using just the main class(1 tab <<<noob =/) but now i would like to break it off into classes and i am having some issues with moving the ship(a triangle), every time the movment key the ship keeps moving in that direction instead of moving in steps.
here is the main class
- //SIZE
- int width = 400;
- int height = width;
- //SHIP CENTER COORDINATES + OBEJECT
- int shipCenterX = 200;
- int shipCenterY = 380;
- ship shipA;
- //INITIALIZE GAME
- void setup()
- {
- size(width, height);
- shipA = new ship(shipCenterX, shipCenterY, width, height);
- }
- //DRAW GAME
- void draw()
- {
- background(0);
- shipA.keyPressed();
- shipA.drawShip();
- }
- class ship
- {
- int x, y, w, h;
- ship(int x, int y, int w, int h)
- {
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- }
- void drawShip()
- {
- fill(255);
- triangle(x - 10, y + 10, x, y, x + 10, y + 10);
- }
- //TRIANFGLE MOVEMENT
- void keyPressed()
- {
- if (key == CODED)
- {
- switch(keyCode)
- {
- case LEFT:
- x -= 3;
- if (x < 0)
- {
- x = width;
- }
- break;
- //
- case RIGHT:
- x += 3;
- if (x > width)
- {
- x = 0;
- }
- break;
- //
- //case UP:
- //shoot = true;
- //break;
- }
- }
- }
- }
1