Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. #include "ST7565.h"
  2.  
  3. int ledPin = 13; // LED connected to digital pin 13
  4.  
  5. // the LCD backlight is connected up to a pin so you can turn it on & off
  6. #define BACKLIGHT_LED 4
  7. // Below are pin values
  8. // CHANGE PIN VALUES ACCORDING TO WHERE YOU PLUG
  9. #define P1_UP 11
  10. #define P1_DN 12
  11. #define P2_UP 3
  12. #define P2_DN 2
  13. #define BTN_WHT 10
  14. #define BTN_BLK 4
  15.  
  16. // pin 9 - Serial data out (SID)
  17. // pin 8 - Serial clock out (SCLK)
  18. // pin 7 - Data/Command select (RS or A0)
  19. // pin 6 - LCD reset (RST)
  20. // pin 5 - LCD chip select (CS)
  21. ST7565 glcd(9, 8, 7, 6, 5);
  22.  
  23. #define LOGO16_GLCD_HEIGHT 16
  24. #define LOGO16_GLCD_WIDTH 16
  25. #define LCD_WIDTH 128
  26. #define LCD_HEIGHT 64
  27.  
  28. // GAME CONSTANTS
  29. #define BALL_RADIUS LCD_WIDTH/60
  30. #define BALL_CIRC LCD_WIDTH/10
  31. #define PADDLE_WIDTH LCD_WIDTH/60
  32. #define PADDLE_HEIGHT LCD_HEIGHT/4
  33. #define BALL_SPEED 2
  34. #define PADDLE_SPEED 4
  35.  
  36. struct posn {
  37.     int x;
  38.     int y;
  39. };
  40.  
  41. struct velo_vector {
  42.     int  vertical;
  43.     int horizontal;
  44. };
  45.  
  46. struct ball {
  47.     struct posn b_pos;
  48.     struct velo_vector velo;
  49. };
  50.  
  51. struct paddle {
  52.     struct posn p_pos;
  53. };
  54.  
  55. struct ball game_ball;
  56. struct paddle player_1;
  57. struct paddle player_2;
  58.  
  59. /*const static unsigned char __attribute__ ((progmem)) logo16_glcd_bmp[]={
  60. 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0xf8, 0xbe, 0x9f, 0xff, 0xf8, 0xc0, 0xc0, 0xc0, 0x80, 0x00,
  61. 0x20, 0x3c, 0x3f, 0x3f, 0x1f, 0x19, 0x1f, 0x7b, 0xfb, 0xfe, 0xfe, 0x07, 0x07, 0x07, 0x03, 0x00, };*/
  62.  
  63. // The setup() method runs once, when the sketch starts
  64.  
  65. void setup() {
  66.     // assign input and output pins for joysicks
  67.     // set default (unpressed) state of inputs to HIGH
  68.     pinMode(P1_UP, INPUT);
  69.     digitalWrite(P1_UP, HIGH);
  70.     pinMode(P1_DN, INPUT);
  71.     digitalWrite(P1_DN, HIGH);
  72.     pinMode(P2_UP, INPUT);
  73.     digitalWrite(P2_UP, HIGH);
  74.     pinMode(P2_DN, INPUT);
  75.     digitalWrite(P2_DN, HIGH);
  76.     // assign buttons
  77.     pinMode(BTN_WHT, INPUT);
  78.     //digitalWrite(
  79.     pinMode(BTN_BLK, INPUT);
  80.     //digitalWrite(
  81.  
  82.  
  83.     Serial.begin(9600);
  84.  
  85.     #ifdef __AVR__
  86.     Serial.print(freeRam());
  87.     #endif
  88.  
  89.     // turn on backlight
  90.     pinMode(BACKLIGHT_LED, OUTPUT);
  91.     digitalWrite(BACKLIGHT_LED, LOW);
  92.  
  93.     // initialize and set the contrast to 0x18
  94.     glcd.begin(0x18);
  95.  
  96.     glcd.display(); // show splashscreen
  97.     delay(2000);
  98.     glcd.clear();
  99.    
  100.     reset_game();
  101.    
  102. //    // draw a single pixel
  103. //    glcd.setpixel(10, 10, BLACK);
  104. //    glcd.display(); // show the changes to the buffer
  105. //    delay(2000);
  106. //    glcd.clear();
  107. }
  108.  
  109.  
  110. void loop() {
  111.    
  112.     //READ JOYSTICK INPUT
  113.     int p1_joypos = digitalRead(P1_UP);
  114.     int p2_joypos = digitalRead(P1_DN);
  115.     int p3_joypos = digitalRead(P2_UP);
  116.     int p4_joypos = digitalRead(P2_DN);
  117.  
  118.     //Read and store positions of paddles
  119.     int pos_1 = player_1.p_pos.y;
  120.     int pos_2 = player_2.p_pos.y;
  121.    
  122.    
  123.     if (p1_joypos==HIGH && p2_joypos==HIGH){
  124.       player_1.p_pos.y = pos_1;
  125.       }
  126.     else if(p1_joypos==HIGH){ //P1_UP pin
  127.       player_1.p_pos.y -= PADDLE_SPEED; //This moves it up
  128.       //p2_joypos = LOW;
  129.       }
  130.     else if(p2_joypos == HIGH){ //p1 down pin
  131.       player_1.p_pos.y += PADDLE_SPEED; //This moves the paddle down
  132.       //p1_joypos = LOW;
  133.       }
  134.  
  135.     if (p3_joypos==HIGH && p4_joypos==HIGH){
  136.       player_2.p_pos.y = pos_2;
  137.       }
  138.     else if(p3_joypos==HIGH){ //P2_UP pin
  139.       player_2.p_pos.y -= PADDLE_SPEED; //This moves it up
  140.       //p2_joypos = LOW;
  141.       }
  142.     else if(p4_joypos == HIGH){ //p1 down pin
  143.       player_2.p_pos.y += PADDLE_SPEED; //This moves the paddle down
  144.       //p1_joypos = LOW;
  145.       }
  146.      
  147.    
  148.     if((game_ball.b_pos.x-BALL_RADIUS <= 0) || (game_ball.b_pos.x+BALL_RADIUS >= LCD_WIDTH))
  149.     {
  150.         reset_game();
  151.     }
  152.  
  153.     else if(game_ball.b_pos.y>=63){
  154.         game_ball.velo.vertical *= -1;
  155.       }
  156.     else if(game_ball.b_pos.y<=2){
  157.         game_ball.velo.vertical *= -1;
  158.       }
  159.       //player 2, add 1 later
  160.     else if((game_ball.b_pos.x <= player_2.p_pos.x)&&game_ball.b_pos.y == 24){
  161.         game_ball.velo.horizontal *= -1;
  162.       }
  163.     Serial.println(player_2.p_pos.y);
  164.  
  165. //      Serial.println(game_ball.b_pos.y);
  166. //      Serial.println("------");
  167.  //      Serial.println(player_1.p_pos.y);
  168.  
  169.      
  170. //  CHECK for contact with player_1
  171. //     else if((game_ball.b_pos.x <= player_1.p_pos.x) &&
  172. //      (game_ball.b_pos.y >= player_1.p_pos.y) &&
  173. //   (game_ball.b_pos.y <= player_1.p_pos.y+PADDLE_HEIGHT+1) &&
  174. //    (game_ball.b_pos.x >= player_1.p_pos.x+PADDLE_WIDTH/2))
  175. //    {
  176. //        game_ball.b_pos.y *= -1;s
  177. //        game_ball.b_pos.x *= -1;
  178. //    }
  179. //    // CHECK for contact with player_2
  180. //    else if((game_ball.b_pos.x >= player_2.p_pos.x) &&
  181. //    (game_ball.b_pos.y >= player_2.p_pos.y) &&
  182. //    (game_ball.b_pos.y <= player_2.p_pos.y+PADDLE_HEIGHT+1) &&
  183. //    (game_ball.b_pos.x <= LCD_WIDTH - (PADDLE_WIDTH+PADDLE_WIDTH/2)))
  184. //    {
  185. //        game_ball.b_pos.y *= -1;
  186. //        game_ball.b_pos.x *= -1;
  187. //    }
  188. //    CHECK for top/bot boundary contact
  189. //  else if((game_ball.b_pos.y - BALL_RADIUS <= 0) || (game_ball.b_pos.y + BALL_RADIUS >= LCD_HEIGHT))
  190. //    {
  191. //        game_ball.b_pos.y *= -1;
  192. //    }
  193. //  
  194. //    // CHECK if player_left is moving joystick vertically
  195. //    if((p1_joypos == 1)&&(player_1.p_pos.y >= 0)) //up movement
  196. //    {
  197. //        player_1.p_pos.y -= PADDLE_SPEED;
  198. //    }
  199. //    else if((p1_joypos == -1)&&(player_1.p_pos.y+PADDLE_HEIGHT+1 <= LCD_HEIGHT)) //down movement
  200. //    {
  201. //        player_1.p_pos.y += PADDLE_SPEED;  
  202. //    }
  203. //  
  204. //    // CHECK if player_right is moving joystick vertically
  205. //    if((p2_joypos == 1)&&(player_2.p_pos.y >= 0)) //up movement
  206. //    {
  207. //        player_2.p_pos.y -= PADDLE_SPEED;
  208. //    }
  209. //    else if((p2_joypos == -1)&&(player_2.p_pos.y+PADDLE_HEIGHT+1 <= LCD_HEIGHT)) //down movement
  210. //    {
  211. //        player_2.p_pos.y += PADDLE_SPEED;  
  212. //    }
  213.  
  214.    
  215.     game_ball.b_pos.y += game_ball.velo.vertical;
  216.     game_ball.b_pos.x += game_ball.velo.horizontal;
  217.    
  218.     draw_board();
  219. }
  220.  
  221. #ifdef __AVR__
  222. // this handy function will return the number of bytes currently free in RAM, great for debugging!  
  223. int freeRam(void) {
  224.     extern int __bss_end;
  225.     extern int * __brkval;
  226.     int free_memory;
  227.     if ((int) __brkval == 0) {
  228.         free_memory = ((int) & free_memory) - ((int) & __bss_end);
  229.     } else {
  230.         free_memory = ((int) & free_memory) - ((int) __brkval);
  231.     }
  232.     return free_memory;
  233. }
  234. #endif
  235.  
  236. void reset_game(){
  237.     // BALL SETUP
  238.     game_ball.b_pos.x = LCD_WIDTH/2;
  239.     game_ball.b_pos.y = LCD_HEIGHT/2;
  240.     game_ball.velo.vertical = 0; //CHANGE THIS TO BALL SPEED OR SOMETHING
  241.     game_ball.velo.horizontal = 2; //SUBJECT TO CHANGE
  242.    
  243.     // PADDLE_1 SETUP
  244.     player_1.p_pos.x = 0 + PADDLE_WIDTH;
  245.     player_1.p_pos.y = LCD_HEIGHT/2 - PADDLE_HEIGHT/2;
  246.    
  247.     // PADDLE_2 SETUP
  248.     player_2.p_pos.x = LCD_WIDTH - PADDLE_WIDTH*2;
  249.     player_2.p_pos.y = LCD_HEIGHT/2 - PADDLE_HEIGHT/2;
  250.     draw_board();
  251. }
  252.  
  253. void draw_board(){
  254.     glcd.clear();
  255.         // draw a black circle, 10 pixel radius, at location (32,32)
  256.     //glcd.fillcircle(32, 32, 10, BLACK);
  257.    
  258.     glcd.fillcircle(game_ball.b_pos.x, game_ball.b_pos.y, BALL_RADIUS, BLACK);
  259.     glcd.fillrect(player_1.p_pos.x, player_1.p_pos.y,
  260.                 PADDLE_WIDTH, PADDLE_HEIGHT, BLACK);
  261.     glcd.fillrect(player_2.p_pos.x, player_2.p_pos.y,
  262.                 PADDLE_WIDTH, PADDLE_HEIGHT, BLACK);
  263.     glcd.display();    
  264.     delay(100);
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement