Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TVout.h>
- #include <fontALL.h>
- #define JOY_BUTTON 3
- #define JOY_RIGHT 6
- #define JOY_LEFT 5
- #define JOY_UP 4
- #define JOY_DOWN 2
- TVout TV;
- int screen_width;
- int font_size = 8;
- int player_x, player_y;
- int player_size = 4;
- unsigned int score;
- int ball_x, ball_y;
- bool ball_left;
- bool ball_up;
- int ball_wait;
- void updateScreen()
- {
- //clear screen
- TV.clear_screen();
- //draw score
- TV.print("score: ");
- TV.println(score);
- //draw ball
- TV.print_char(ball_x*font_size, ball_y*font_size, 'o');
- //draw player
- for (int i=0; i < player_size; i++)
- {
- TV.print_char((i+player_x)*font_size, player_y*font_size, '#');
- }
- }
- void playBallSound()
- {
- tone(11,666,25);
- }
- void resetGame()
- {
- player_x = screen_width/2 - player_size/2;
- player_y = 11;
- score = 0;
- ball_x = screen_width/2;
- ball_y = player_y-1;
- ball_left = true;
- ball_up = true;
- ball_wait = 0;
- }
- void setup()
- {
- //joystick
- pinMode(JOY_BUTTON, INPUT);
- pinMode(JOY_RIGHT, INPUT);
- pinMode(JOY_LEFT, INPUT);
- pinMode(JOY_UP, INPUT);
- pinMode(JOY_DOWN, INPUT);
- digitalWrite(JOY_BUTTON, HIGH);
- digitalWrite(JOY_RIGHT, HIGH);
- digitalWrite(JOY_LEFT, HIGH);
- digitalWrite(JOY_UP, HIGH);
- digitalWrite(JOY_DOWN, HIGH);
- //Serial.begin(9600);
- TV.begin(PAL);
- TV.select_font(font8x8);
- screen_width = TV.char_line();
- resetGame();
- }
- void loop()
- {
- //check ball
- if (digitalRead(JOY_LEFT)==0 && player_x > 0)
- {
- //left
- player_x--;
- }
- else if(digitalRead(JOY_RIGHT)==0 && player_x < screen_width - player_size)
- {
- //right
- player_x++;
- }
- if (++ball_wait > 1)
- {
- ball_wait = 0;
- if (ball_left)
- {
- // go right
- if (--ball_x <= 0)
- {
- ball_x = 0;
- ball_left = false;
- playBallSound();
- }
- }
- else if (++ball_x >= screen_width-1)
- {
- //go left
- ball_x = screen_width-1;
- ball_left = true;
- playBallSound();
- }
- if (ball_up)
- {
- if (--ball_y <= 1)
- {
- //score and go down
- score++;
- ball_y = 1;
- ball_up = false;
- playBallSound();
- }
- }
- else
- {
- ball_y++;
- if (ball_y == player_y-1 && ball_x >= player_x && ball_x < player_x + player_size)
- {
- //go up
- ball_up = true;
- playBallSound();
- }
- else if (ball_y >= player_y)
- {
- //die
- updateScreen();
- TV.delay_frame(20);
- resetGame();
- updateScreen();
- TV.delay_frame(40);
- }
- }
- }
- updateScreen();
- TV.delay_frame(2);
- }
Advertisement
Add Comment
Please, Sign In to add comment