Created
January 26, 2015 15:50
-
-
Save Happsson/524ceb7be91288b072ac to your computer and use it in GitHub Desktop.
Pong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "LedControlMS.h" | |
#include <math.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#define NBR_MTX 2 | |
LedControl lc=LedControl(12,11,10, NBR_MTX); | |
const int LPin = A3; //left paddle in | |
const int RPin = A1; //Right paddle in | |
int LP[3] = {3,4,5}; //Left paddle | |
int RP[3] = {2,3,4}; // right paddle | |
int xB = 4; // xBall | |
int yB = 1; // yBall | |
int leftPadMovement = 1; | |
int rightPadMovement = 1; | |
/* | |
0 = rakt höger | |
1 = uppåt höger | |
2 = uppåt vänster | |
3 = rakt vänster | |
4 = ner vänster | |
5 = ner höger | |
*/ | |
int ballDir = 1; | |
void setup() { | |
Serial.begin (9600); | |
for (int i=0; i< NBR_MTX; i++){ | |
lc.shutdown(i,false); | |
/* Set the brightness to a medium values */ | |
lc.setIntensity(i,10); | |
/* and clear the display */ | |
lc.clearDisplay(i); | |
} | |
StartPositionsPaddles(); | |
} | |
void StartPositionsPaddles(void){ | |
for(int i = 0; i < 3; i++){ | |
lc.setLed(0,0,LP[i],true); | |
} | |
for(int i = 0; i < 3; i++){ | |
lc.setLed(0,7,RP[i],true); | |
} | |
} | |
void loop() { | |
BallAction(); | |
ReadPaddles(); | |
MoveLeft(); | |
MoveRight(); | |
delay(200); | |
} | |
void MoveLeft(void){ | |
if(leftPadMovement == 2 && LP[2]!=7){ | |
Kill(0,LP[0]); | |
LP[0]++; | |
LP[1]++; | |
LP[2]++; | |
Display(0,LP[2]); | |
} | |
else if(leftPadMovement == 0 && LP[0] != 0){ | |
Kill(0,LP[2]); | |
LP[0]--; | |
LP[1]--; | |
LP[2]--; | |
Display(0,LP[0]); | |
} | |
} | |
void MoveRight(void){ | |
if(rightPadMovement == 2 && RP[2]!=7){ | |
Kill(7,RP[0]); | |
RP[0]++; | |
RP[1]++; | |
RP[2]++; | |
Display(7,RP[2]); | |
} | |
else if(rightPadMovement == 0 && RP[0] != 0){ | |
Kill(7,RP[2]); | |
RP[0]--; | |
RP[1]--; | |
RP[2]--; | |
Display(7,RP[0]); | |
} | |
} | |
void Led(int x, int y){ | |
} | |
void ReadPaddles(void){ | |
int left = analogRead(LPin); | |
int right = analogRead(RPin); | |
/* | |
Left upp > 500; | |
Left ner < 200; | |
Right upp < 500; | |
Right ner > 200; | |
*/ | |
if(left<200) leftPadMovement=0; | |
else if(left>500) leftPadMovement=2; | |
else leftPadMovement=1; | |
if(right<200) rightPadMovement=2; | |
else if(right>500) rightPadMovement=0; | |
else rightPadMovement=1; | |
} | |
void BallAction(void){ | |
Kill(xB,yB); | |
MoveBall(); | |
CollisionCheck(); | |
Display(xB,yB); | |
} | |
void Kill(int x, int y){ | |
lc.setLed(0,x,y,false); | |
} | |
void CollisionCheck(void){ | |
if(yB == 7 && ballDir == 1) ballDir = 5; //Collision with roof, going right | |
if(yB == 7 && ballDir == 2) ballDir = 4; //Collision with roof, going left | |
if(yB == 0 && ballDir == 4) ballDir = 2; //Collision with floor, going left | |
if(yB == 0 && ballDir == 5) ballDir = 1; //Collision with floor, going right | |
if(xB == 1 && ballDir == 2){ | |
if(yB == LP[0] || yB == LP[1] || yB == LP[2]){ | |
ballDir = 1; //Collision left, going up | |
}else{ | |
xB--; | |
yB++; | |
Display(xB,yB); | |
EndGame(); | |
} | |
} | |
if(xB == 1 && ballDir == 4){ | |
if(yB == LP[0] || yB == LP[1] || yB == LP[2]){ | |
ballDir = 5; | |
}else{ | |
xB--; | |
yB--; | |
Display(xB,yB); | |
EndGame(); | |
} | |
} | |
if(xB == 6 && ballDir == 1){ | |
if(yB == RP[0] || yB == RP[1] || yB == RP[2]){ | |
ballDir = 2; | |
}else{ | |
xB++; | |
yB++; | |
Display(xB,yB); | |
EndGame(); | |
} | |
} | |
if(xB == 6 && ballDir == 5){ | |
if(yB == RP[0] || yB == RP[1] || yB == RP[2]){ | |
ballDir = 4; | |
}else{ | |
xB++; | |
yB--; | |
Display(xB,yB); | |
EndGame(); | |
} | |
} | |
} | |
void EndGame(void){ | |
while(1) {}; | |
} | |
void Display(int x, int y){ | |
lc.setLed(0,x,y,true); | |
} | |
void MoveBall(void){ | |
switch(ballDir){ | |
case 0: | |
yB++; | |
break; | |
case 1: | |
yB++; | |
xB++; | |
break; | |
case 2: | |
yB++; | |
xB--; | |
break; | |
case 3: | |
xB--; | |
break; | |
case 4: | |
xB--; | |
yB--; | |
break; | |
case 5: | |
xB++; | |
yB--; | |
break; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment