Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //start game: push button vs turn one of the pot
- //turn pot -> move paddle
- const byte ANODE_PINS[8] = {13, 12, 11, 10, 9, 8, 7, 6};
- const byte CATHODE_PINS[8] = {A3, A2, A1, A0, 5, 4, 3, 2};
- const byte INPUT_P1 = A4;
- const byte INPUT_P2 = A5;
- int ballX, ballY, dx, dy, p1Pos, p2Pos;
- int pattern[8][8] = {{0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0}};
- int one[8][4] = {{0,0,0,0},
- {0,0,1,0},
- {0,0,1,0},
- {0,0,1,0},
- {0,0,1,0},
- {0,0,1,0},
- {0,0,1,0},
- {0,0,0,0}};
- int two[8][4] = {{0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0}};
- int two[8][4] = {{0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0}};
- int two[8][4] = {{0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0}};
- int two[8][4] = {{0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0}};
- int two[8][4] = {{0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0},
- {0,0,0,0}};
- //anodes on y, cathodes x
- int p1Score, p2Score;
- bool playGame;//false until turn pot
- bool ballUp, ballLeft = true;
- unsigned long readUserInput = 0;
- void setup() {
- Serial.begin(115200);
- for (byte i = 0; i < 8; i++) {
- pinMode(ANODE_PINS[i], OUTPUT);
- pinMode(CATHODE_PINS[i], OUTPUT);
- digitalWrite(ANODE_PINS[i], HIGH);
- digitalWrite(CATHODE_PINS[i], HIGH);
- }
- pinMode(INPUT_P1, INPUT);
- pinMode(INPUT_P2, INPUT);
- playGame = false;
- p1Score = 0;
- p2Score = 0;
- restart();
- }
- //starts ball at random postion
- void restart(){
- dx = 1;
- dy = 1;
- p1Pos = 3;
- p2Pos = 3;
- ballY = random(1,5);
- ballX = random(1,6);
- }
- void loop() {
- //
- if (millis() > readUserInput + 600) {
- handleCollision();
- updatePlayerPos();
- updateBall();
- updateScreen();
- readUserInput = millis();
- }
- if(p1Score >= 3 || p2Score >= 3){
- //end the game
- }
- draw();
- }
- void handleCollision() {
- //if ball hits paddle
- if (ballY + dy == 0 && (ballX + dx >= p1Pos && ballX + dx < p1Pos + 3)) {
- dy *= -1;
- } else if (ballY == -1) {
- updateScore();
- restart();
- return;
- }
- if (ballY + dy == 7 && (ballX + dx >= p2Pos && ballX + dx < p2Pos + 3)) {
- dy *= -1;
- } else if (ballY == 8) {
- updateScore();
- restart();
- return;
- }
- if (ballX == 7) {
- dx *= -1;
- }
- if (ballX == 0) {
- dx *= -1;
- }
- }
- void updateBall() {
- ballY += dy;
- ballX += dx;
- // if (ballY == 7) {
- // dy *= -1;
- // }
- // if (ballY == 0) {
- // dy *= -1;
- // }
- }
- void draw(){
- for (byte i = 0; i < 8; i++) {
- for (byte j = 0; j < 8; j++) {
- if (pattern[i][j] == 1) {
- digitalWrite(CATHODE_PINS[j], LOW);
- } else {
- digitalWrite(CATHODE_PINS[j], HIGH);
- }
- }
- digitalWrite(ANODE_PINS[i], LOW);
- delayMicroseconds(5);
- digitalWrite(ANODE_PINS[i], HIGH);
- }
- }
- void updateScreen() {
- for (int i = 0; i < 8; i++){
- for (int j = 0; j < 8; j++){
- if (i == ballY && j == ballX) {
- pattern[i][j] = 1;
- } else {
- pattern[i][j] = 0;
- }
- }
- }
- for(int i = 0; i < 8; i++){
- if(i >= p1Pos && i < p1Pos + 3){
- pattern[0][i] = 1;
- }else{
- if (ballX != i) pattern[0][i] = 0;
- }
- if(i >= p2Pos && i < p2Pos + 3){
- pattern[7][i] = 1;
- }else{
- if (ballX != i) pattern[7][i] = 0;
- }
- }
- }
- void updatePlayerPos(){
- p1Pos = readPot1(analogRead(INPUT_P1), p1Pos);
- p2Pos = readPot2(analogRead(INPUT_P2), p2Pos);
- p1Pos = max(p1Pos, 0);
- p2Pos = max(p2Pos, 0);
- p1Pos = min(p1Pos, 5);
- p2Pos = min(p2Pos, 5);
- }
- int readPot1(int potVal, int playerPos){
- if(potVal < 50){
- playerPos++;
- } else if(potVal > 200){
- playerPos--;
- }
- return playerPos;
- }
- int readPot2(int potVal, int playerPos){
- if(potVal >= 960){
- playerPos--;
- } else if(potVal < 750){
- playerPos++;
- }
- return playerPos;
- }
- void displayScore(number, bool whichHalf){
- clearBoard();
- //0
- if (whichHalf)
- for i 1...8
- for j 1...4
- pattern ij = number ij
- else
- for i 1.....8
- for j 5.....8
- pattern ij = number ij
- }
- void updateScore(){
- int currScore;
- //update the players score...via var that keeps track which row the ball ended at
- if(ballY == 0){
- p1Score++;
- currScore = p1Score;
- } else {
- p2Score++;
- currScore = p2Score;
- }
- //turn all lights off
- clearBoard();
- //pattern score in some way for a hot sec
- displayScore();
- }
- void clearBoard(){
- for(int i = 0; i < 8; i++){
- for(int j = 0; j < 8; j++){
- pattern[i][j] = 0;
- }
- }
- draw();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement