Shooting game with random meme generator
.
Overview
This shooting game uses the uLCD, joystick, and a pushbutton. The opening screen prompts the user to press the button to start the game. In the game, the player is represented by a green square that can be moved to the left and right using the joystick. The square shoots when the button is pressed. Red balls fall from random spots at the top of the screen. The user of the game must use the joystick to move the square to a position where it can shoot the falling red balls. Every time a red ball is shot, the player's score goes up. If the player gets hit by a falling red ball, the game is over. When the game ends, a value is randomly selected from a list of 9 addresses of images of troll memes. This picture is shown to the user for 3 seconds at the end of the game before the game over screen is shown with the player's score and a prompt to restart the game. The user can press the button to return to the game's start screen.
Pictures
Video
Pin Outs
Joystick
uLCD wiring
Push button
P8 & GND
Game Code
game code
#include "mbed.h" #include "uLCD_4DGL.h" #define X_MIN_ABS (1) #define X_MAX_ABS (128) static const float Width = (float)(X_MAX_ABS - X_MIN_ABS); AnalogIn AnalogX(p15); AnalogIn AnalogY(p16); DigitalIn pb(p8); uLCD_4DGL uLCD(p9,p10,p11); int score = 0; int ypos = 0; int xpos = 0; int xpos2 = 0; int ypos2 = 0; int playerxpos = 64; int playerwidth = 54; int circlesize = 4; int falling = 0; int falling2 = 0; uint16_t prevx = .5; int shotx; int shooting = 0; int shoty; enum gameState { start, playing, over }; int volatile state; int memes[9] = {0x0000, 0x0041, 0x0071, 0x00AE, 0x00E2, 0x0123, 0x0147, 0x016F, 0x01AA}; int main() { srand(time(NULL)); state = start; pb.mode(PullUp); uLCD.baudrate(3000000); uLCD.background_color(BLACK); uLCD.cls(); int play = 1; while(play) { switch(state) { case start: score = 0; falling = 0; shooting = 0; ypos = 0; xpos = 0; ypos2 = 128; xpos2 = 0; playerxpos = 64; playerwidth = 54; circlesize = 4; uLCD.cls(); uLCD.printf("Press button to start game!"); while (pb); wait(1); state = playing; break; case playing: float AnalogXReading = AnalogX.read(); uint16_t x = X_MIN_ABS + (uint16_t)(Width * AnalogXReading); uLCD.cls(); uLCD.filled_rectangle(playerxpos, 128, playerwidth, 118, 0x00FF00); if (x < 30 && (playerxpos-10)>0) { playerxpos = playerxpos-8; playerwidth = playerwidth-8; } else if (x > 90 && (playerxpos < 128)) { playerxpos = playerxpos+8; playerwidth = playerwidth+8; } if ((playerxpos-7)<xpos) { if (xpos<(playerxpos+(playerxpos-playerwidth)+7)) { if (ypos>=115) { state = over; } } } if ((playerxpos-7)<xpos2) { if (xpos2<(playerxpos+(playerxpos-playerwidth)+7)) { if (ypos2>=115) { state = over; } } } if(!pb) { if(!shooting) { shoty=118; shotx=playerxpos-5; shooting = 1; } } if (shooting) { if (shoty<=0) { shooting = 0; } else if (ypos>=shoty) { if((xpos-5)<shotx) { if((xpos+5)>shotx) { shooting = 0; falling = 0; score++; } else { uLCD.filled_circle(shotx, shoty, 1, 0xF0F0F0); shoty = shoty-3; } } else { uLCD.filled_circle(shotx, shoty, 1, 0xF0F0F0); shoty = shoty-3; } } else if (ypos2>=shoty) { if((xpos2-7)<shotx) { if((xpos2+7)>shotx) { shooting = 0; falling2 = 0; score++; } else { uLCD.filled_circle(shotx, shoty, 1, 0xF0F0F0); shoty = shoty-3; } } else { uLCD.filled_circle(shotx, shoty, 1, 0xF0F0F0); shoty = shoty-3; } } else { uLCD.filled_circle(shotx, shoty, 1, 0xF0F0F0); shoty = shoty-3; } } if (!falling && ypos2>64) { xpos = rand() % 120; if (xpos < 4) { xpos = xpos + 5; } ypos = 1; falling = 1; uLCD.filled_circle(xpos, ypos, circlesize, 0xFF0000); ypos++; } else if (falling) { if (ypos<128) { uLCD.filled_circle(xpos, ypos, circlesize, 0xFF0000); ypos=ypos+2; } else { falling = 0; } } if (!falling2 && ypos>64) { xpos2 = rand() % 120; if (xpos2 < 4) { xpos2 = xpos2 + 5; } ypos2 = 1; falling2 = 1; uLCD.filled_circle(xpos2, ypos2, circlesize, 0xFF0000); ypos2++; } else if (falling2) { if (ypos2<128) { uLCD.filled_circle(xpos2, ypos2, circlesize, 0xFF0000); ypos2=ypos2+2; } else { falling2 = 0; } } prevx = x; break; case over: uLCD.cls(); // show meme pic int ind = rand() % 9; uLCD.media_init(); uLCD.set_sector_address(0x0000, memes[ind]); uLCD.display_image(0,0); wait(3); uLCD.cls(); uLCD.printf("Game Over. Your score is: %d. Press button to restart.", score); while(pb); wait(1); state = start; break; } } }
Random Meme Generator Code
JPG images were edited, resized and saved as BMP image files using Paint. This reduced the image size drastically. The graphics composer tool (from IDE) converted the image file and created the new image file on the SD card. This tool can also resize images and *.wmv videos for the display by selecting the file and clicking the "edit" button. It reported which sector address to use for the image (listed in project's *.GC file).
Graphic Composer tool - http://old.4dsystems.com.au/prod.php?id=50
random memes
#include "mbed.h" #include "uLCD_4DGL.h" DigitalOut myled(LED1); uLCD_4DGL uLCD(p9,p10,p11); int memes[9] = {0x0000, 0x0041, 0x0071, 0x00AE, 0x00E2, 0x0123, 0x0147, 0x016F, 0x01AA}; int main() { srand(time(NULL)); while (1) { int ind = rand() % 9; uLCD.cls(); uLCD.media_init(); uLCD.set_sector_address(0x0000, memes[ind]); uLCD.display_image(0,0); wait(2); } }
Please log in to post comments.