
player 1
Dependencies: 4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem
Fork of 4180FinalLab by
Wireless 2 Player Pong game
Revision 0:356124c0bafc, committed 2014-06-17
- Comitter:
- jlind6
- Date:
- Tue Jun 17 20:03:41 2014 +0000
- Child:
- 1:839d22d423bd
- Commit message:
- Initial Commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/jlind6/code/4DGL-uLCD-SE/#d901d4d206e6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +#ifndef _SPEAKER_H +#define _SPEAKER_H +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; + +#endif // _SPEAKER_H \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ball.h Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,35 @@ +#include "tempModule.h" +#include "uLCD_4DGL.h" + +class Ball +{ +public: + // Constructors + Ball(PinName); + Ball(PinName, float, float, int); + // Set Functions + void setBaseVx(float); // This sets the lowest velocity of vx (for Thermal pong) or the constant velocity + void setBaseVy(float); // This sets the lowest velocity of vy (for Thermal pong) or the constant velocity + void setVxSign(int); + void setVySign(int); + // Get Functions + int getFutureX(); // get estimate of where the ball will be in the next update() + int getFutureY(); // get estimate of where the ball will be in the next update() + // Member Functions + void reverseXDirection(); // negate the sign for when a ball hits something + void reverseYDirection(); // negate the sign for when a ball hits something + void reset(int, int, int, int, uLCD_4DGL *); // takes in a new location and new directions and draws the starting point for the ball + void update(uLCD_4DGL *); // moves the ball on the screen one vx and vy + +private: + // Data members are suggestions, feel free to add/remove + TempModule *tempSensor; + int vxSign; + int vySign; + float fx; + float fy; + int x; + int y; + int radius; + bool lose; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,185 @@ +#include "mbed.h" +#include "PinDetect.h" +#include "uLCD_4DGL.h" +#include "Speaker.h" + +// Pushbuttons +PinDetect pbUp(p15); +PinDetect pbDown(p16); +// uLCD +uLCD_4DGL uLCD(p28, p27, p29); +//Speaker +Speaker mySpeaker(p21); + +// Global variables needed for the push button interrupts +int cornerX = 118, cornerY = 1; +int oldCornerY = 1; +int paddleMove = 8; +int length = 40; +int width = 3; + +// State machine definitions +enum gameStateType {START, WAIT, GAME_SETUP, GAME, LOSE}; +/* State Definitions: + * START -- Creates the start screen + * WAIT -- After the start screen, goes into wait where mbed spins and does nothing + * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity + * GAME -- When the user actually gets to play + * LOSE -- clears the screen, prints you lose, waits, then goes back to start + */ + +// Global state machine variable (So that the pushbuttons can modify it) +gameStateType gameState = START; + +// Pushbutton callbacks +// WARNING: Do not call to draw anything to the uLCD in these +// as this will cause the uLCD to crash sometimes. Update positions +// and draw elsewhere (like it's done here). +// Only modify the logic inside the callback functions. +void pbUp_hit_callback (void) +{ + switch (gameState) + { + case WAIT: + gameState = GAME_SETUP; + break; + case GAME: + if(cornerY > paddleMove) { + cornerY -= paddleMove; + } + break; + } +} + +void pbDown_hit_callback (void) +{ + switch (gameState) + { + case WAIT: + gameState = GAME_SETUP; + break; + case GAME: + if(cornerY < 127 - paddleMove - length){ + cornerY += paddleMove; + } + break; + } +} + +int main() +{ + // This is setting up the pushbuttons + // Don't modify this code. + pbUp.mode(PullUp); + pbDown.mode(PullUp); + wait(0.1); + pbUp.attach_deasserted(&pbUp_hit_callback); + pbDown.attach_deasserted(&pbDown_hit_callback); + pbUp.setSampleFrequency(); + pbDown.setSampleFrequency(); + // Don't modify this code. + + uLCD.display_control(PORTRAIT); + uLCD.cls(); + uLCD.baudrate(BAUD_3000000); + uLCD.background_color(BLACK); + + // Initialize all your variables outside the while/switch statement + // to avoid compiler warning/errors + int vxSign = 1, vySign = 1; + float fx=50.0,fy=21.0,vx=1.6,vy=1.2; + int x=50, y=21, radius=5; + int score = 0; + int i = 0; + int random; + + while (1) + { + switch (gameState) + { + case START: + uLCD.cls(); + uLCD.locate(0,0); + uLCD.printf("Pong!!!\n\n"); + uLCD.printf("Press Key to Start"); + gameState = WAIT; + break; + case GAME_SETUP: + uLCD.cls(); + uLCD.line(0, 0, 127, 0, 0xCFB53B); + uLCD.line(127, 0, 127, 127, 0xCFB53B); + uLCD.line(127, 127, 0, 127, 0xCFB53B); + uLCD.line(0, 127, 0, 0, 0xCFB53B); + vx = 1.6; + vy = 1.2; + srand(i); + random = (rand() % (118 - 2*radius)) + radius; + fx = random; + random = (rand() % (127 - 2*radius)) + radius; + fy = random; + x=(int)fx; y=(int)fy; + random = rand() % 1; + vxSign=-1; vySign=((float)random - 0.5)*2; + uLCD.filled_rectangle(cornerX, cornerY, cornerX+width, cornerY+length, BLUE); + gameState = GAME; + break; + case GAME: + if ((fx+vxSign*vx<=radius+1)) + { + vxSign = -vxSign; + } + if ((fy+vySign*vy<=radius+1) || (fy+vySign*vy>=126-radius)) + { + vySign = -vySign; + } + if (((fx+vxSign*vx >= cornerX) && (fx+vxSign*vy <= cornerX+3)) && + ((fy+vySign*vy>=cornerY) && (fy+vySign*vy<=cornerY+length))) + { + vySign = -vySign; + } + if ((fx+vxSign*vx>=126-radius)) + { + vx = 0; + vy = 0; + gameState = LOSE; + } + if ((fx+vxSign*vx>=cornerX-radius) && (fy+vySign*vy<=cornerY+length) && (fy+vySign*vy>=cornerY)) + { + vxSign = -vxSign; + score++; + uLCD.locate(1,1); + uLCD.printf("%d", score); + } + uLCD.circle(x, y, radius, BLACK); + fx=fx+(vxSign*vx); + fy=fy+(vySign*vy); + x=(int)fx; + y=(int)fy; + uLCD.circle(x, y, radius, WHITE); + // We can assume that these for loops are quick enough that the paddle will move only one interval. + // These movements of the paddle have been optimized. Feel free to draw it out to see how it's been done. + if(oldCornerY > cornerY) { + uLCD.filled_rectangle(cornerX, oldCornerY-paddleMove+1, cornerX+width, oldCornerY, BLUE); + uLCD.filled_rectangle(cornerX, oldCornerY+length-paddleMove+1, cornerX+width, oldCornerY+length, BLACK); + oldCornerY = cornerY; + } + else if(oldCornerY < cornerY) { + uLCD.filled_rectangle(cornerX, oldCornerY, cornerX+width, oldCornerY+paddleMove, BLACK); + uLCD.filled_rectangle(cornerX, oldCornerY+length, cornerX+width, oldCornerY+length+paddleMove, BLUE); + oldCornerY = cornerY; + } + break; + case LOSE: + uLCD.cls(); + uLCD.printf("YOU LOSE D:"); + score = 0; + wait(5.0); + gameState = START; + break; + case WAIT: + // Used to seed the rand() function so we don't get the same starting position every time. + i++; + break; + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/024bf7f99721 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/paddle.h Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,37 @@ +#include "uLCD_4DGL.h" + +class Paddle +{ +public: + // Constructors + Paddle(int, int); + Paddle(int, int, int, int); + // Set Functions + void setLength(int); + void setWidth(int); + void setPaddleMove(int); + void setLimits(int, int); // upper and lower limits of the paddle + // Get Function + int getScore(); + // Member Functions + void movePaddle(bool); // moves the paddle locations (does not draw!) + bool checkHitX(int, int, int); // Using a position and radius, checks to see if something has hit the paddle in the x direction + bool checkHitY(int, int, int); // Using a position and radius, checks to see if something has hit the paddle in the y direction + void resetScore(); // sets score to 0 + void initDraw(uLCD_4DGL *uLCD); // draw the paddle initially (draws the whole thing) + void redraw(uLCD_4DGL *uLCD); // draws the paddle for a move (does NOT draw the whole thing) + + +private: + // Data members are suggestions, feel free to add/remove + int score; + int x; + int y; + int oldy; + int length; + int width; + int paddleMove; + int topLimit; + int bottomLimit; +}; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/soundBuilder.h Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,26 @@ +#include "Speaker.h" + +class Note +{ +public: + +private: + float freq; + float length; + float volume; +}; + +class SoundBuilder +{ +public: + // Set Sounds + // Set Songs + // Play Sounds + // Play Songs + // Clear Songs + +private: + Note song[20]; + Speaker *speaker; + +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tempModule.h Tue Jun 17 20:03:41 2014 +0000 @@ -0,0 +1,40 @@ +#include "uLCD_4DGL.h" + +class TempModule +{ +public: + // Constructors + TempModule(PinName pin); + TempModule(PinName pin, float vx, float vy); + // Reading temperature values + float read(); + // Set Functions + void setBaseVx(float); // This sets the lowest velocity of vx + void setBaseVy(float); // This sets the lowest velocity of vy + // Get Functions/Member Functions + float getVx(); // Calculates a speed based off of the temp + float getVy(); + float getVx(uLCD_4DGL *); // Same thing as getVx(), except it + // also writes the temp to the screen. + +private: + // Data members are suggestions, feel free to add/remove + AnalogIn _sensor; + float roomTemp; + float holdTemp; + float basevx; + float basevy; + int counter; +}; + +TempModule::TempModule (PinName pin) : _sensor(pin) +{ + // Constructor code goes here + // you can ignore initializing _sensor, we've already done that +} + +TempModule::TempModule (PinName pin, float vx, float vy) : _sensor(pin) +{ + // Constructor code goes here + // you can ignore initializing _sensor, we've already done that +} \ No newline at end of file