|
@@ -6,9 +6,15 @@
|
|
#include "Display.h"
|
|
#include "Display.h"
|
|
#include "structs.h"
|
|
#include "structs.h"
|
|
|
|
|
|
-
|
|
|
|
-#define ACCELERATION 0.001f
|
|
|
|
-#define TICK 0.001f
|
|
|
|
|
|
+#define ACCELERATION 0.001f //increase of the ball speed over time
|
|
|
|
+#define TICK 0.001f //movement per tick
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * button input is 8 bit word.
|
|
|
|
+ * variables define which of the 8 buttons is assigned to the given functionality
|
|
|
|
+ * Exp.:
|
|
|
|
+ * #define LEFT_UP_BUTTON 1 -> the first button will move the left pannel up
|
|
|
|
+*/
|
|
#define LEFT_UP_BUTTON 1
|
|
#define LEFT_UP_BUTTON 1
|
|
#define LEFT_DOWN_BUTTON 0
|
|
#define LEFT_DOWN_BUTTON 0
|
|
#define RIGTH_UP_BUTTON 3
|
|
#define RIGTH_UP_BUTTON 3
|
|
@@ -21,6 +27,11 @@ struct Ball ball;
|
|
struct Paddle left_paddle;
|
|
struct Paddle left_paddle;
|
|
struct Paddle right_paddle;
|
|
struct Paddle right_paddle;
|
|
|
|
|
|
|
|
+/*
|
|
|
|
+ * restes everything to default.
|
|
|
|
+ * both paddles on highes position, with a length of 3.
|
|
|
|
+ * ball in the middle, start moving up-rigth.
|
|
|
|
+*/
|
|
void reset_pos() {
|
|
void reset_pos() {
|
|
ball.x = COLS/2 - 1;
|
|
ball.x = COLS/2 - 1;
|
|
ball.y = ROWS/2 - 1;
|
|
ball.y = ROWS/2 - 1;
|
|
@@ -32,6 +43,17 @@ void reset_pos() {
|
|
right_paddle.length = 3;
|
|
right_paddle.length = 3;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/*
|
|
|
|
+ * definition of the game PONG itself.
|
|
|
|
+ * first read input registers and calculate new paddelpositions depending on the pressed buttons.
|
|
|
|
+ *
|
|
|
|
+ * second calculate the new position of the ball depending on its current position and velocity.
|
|
|
|
+ * If Ball hits left or rigth border calculate if the ball hits a paddle or strike a goal.
|
|
|
|
+ * Goal -> restart the game
|
|
|
|
+ * Paddle -> bounce ball back
|
|
|
|
+ *
|
|
|
|
+ * Third reset the screen to 0 and draw the new positions on it
|
|
|
|
+*/
|
|
void game() {
|
|
void game() {
|
|
int i,k;
|
|
int i,k;
|
|
unsigned buttons;
|
|
unsigned buttons;
|
|
@@ -42,8 +64,7 @@ void game() {
|
|
while (1){
|
|
while (1){
|
|
|
|
|
|
buttons = ~IORD_ALTERA_AVALON_PIO_DATA(PIO_BUTTON_BASE);
|
|
buttons = ~IORD_ALTERA_AVALON_PIO_DATA(PIO_BUTTON_BASE);
|
|
- //buttons = 10;
|
|
|
|
-
|
|
|
|
|
|
+ // calculate new paddle positions from button imput
|
|
if (buttons & (1<<LEFT_UP_BUTTON)){
|
|
if (buttons & (1<<LEFT_UP_BUTTON)){
|
|
left_paddle.y = MAX(left_paddle.y - (10 * TICK),0);
|
|
left_paddle.y = MAX(left_paddle.y - (10 * TICK),0);
|
|
printf("%f\n",left_paddle.y);
|
|
printf("%f\n",left_paddle.y);
|
|
@@ -60,14 +81,16 @@ void game() {
|
|
right_paddle.y = MIN(right_paddle.y + (10 * TICK), ROWS - (right_paddle.length));
|
|
right_paddle.y = MIN(right_paddle.y + (10 * TICK), ROWS - (right_paddle.length));
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ // calculate new ball position from old position and velocity
|
|
ball.y = MAX(MIN(ball.y + ball.vel_y * TICK,ROWS - 1),0);
|
|
ball.y = MAX(MIN(ball.y + ball.vel_y * TICK,ROWS - 1),0);
|
|
|
|
+ //check for up and down borders
|
|
if (ball.y == 0 || ball.y == ROWS - 1){
|
|
if (ball.y == 0 || ball.y == ROWS - 1){
|
|
ball.vel_y = -ball.vel_y;
|
|
ball.vel_y = -ball.vel_y;
|
|
}
|
|
}
|
|
|
|
|
|
ball.x = MAX(MIN(ball.x + ball.vel_x * TICK,COLS - 1),0);
|
|
ball.x = MAX(MIN(ball.x + ball.vel_x * TICK,COLS - 1),0);
|
|
|
|
|
|
|
|
+ // check for left border
|
|
if (ball.x <= 1){
|
|
if (ball.x <= 1){
|
|
printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
|
|
printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
|
|
printf("left_paddlex: %f\n",left_paddle.y);
|
|
printf("left_paddlex: %f\n",left_paddle.y);
|
|
@@ -79,6 +102,7 @@ void game() {
|
|
reset_pos();
|
|
reset_pos();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ // check for rigth border
|
|
if (ball.x >= COLS - 2){
|
|
if (ball.x >= COLS - 2){
|
|
if (round(ball.y) >= round(right_paddle.y) && round(ball.y) <= (round(right_paddle.y) + right_paddle.length -1)){
|
|
if (round(ball.y) >= round(right_paddle.y) && round(ball.y) <= (round(right_paddle.y) + right_paddle.length -1)){
|
|
ball.vel_x = - ball.vel_x;
|
|
ball.vel_x = - ball.vel_x;
|
|
@@ -88,7 +112,7 @@ void game() {
|
|
reset_pos();
|
|
reset_pos();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ //increase balls velocity
|
|
if (ball.vel_x < 0) {
|
|
if (ball.vel_x < 0) {
|
|
ball.vel_x = ball.vel_x - ACCELERATION;
|
|
ball.vel_x = ball.vel_x - ACCELERATION;
|
|
} else {
|
|
} else {
|
|
@@ -100,8 +124,9 @@ void game() {
|
|
ball.vel_y = ball.vel_y + ACCELERATION;
|
|
ball.vel_y = ball.vel_y + ACCELERATION;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ // draw new positions on screen
|
|
reset_screen();
|
|
reset_screen();
|
|
|
|
+
|
|
for (i=0;i<left_paddle.length;i++) {
|
|
for (i=0;i<left_paddle.length;i++) {
|
|
set_pixel( 0, round(left_paddle.y) + i);
|
|
set_pixel( 0, round(left_paddle.y) + i);
|
|
}
|
|
}
|
|
@@ -115,6 +140,11 @@ void game() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/*
|
|
|
|
+ * function to output a test picture. used to verify the VHDL implementation.
|
|
|
|
+ * Will turn on one pixel after another starting with row 1 on the rigth side
|
|
|
|
+ * Will also print on console if one of the game buttons is pressed.
|
|
|
|
+*/
|
|
void test_picture() {
|
|
void test_picture() {
|
|
int i,j,k;
|
|
int i,j,k;
|
|
unsigned buttons;
|
|
unsigned buttons;
|
|
@@ -139,6 +169,7 @@ void test_picture() {
|
|
printf("\n");
|
|
printf("\n");
|
|
set_pixel(j,i);
|
|
set_pixel(j,i);
|
|
draw();
|
|
draw();
|
|
|
|
+ // slepp
|
|
for(k=0;k<100000;k++) {
|
|
for(k=0;k<100000;k++) {
|
|
}
|
|
}
|
|
}
|
|
}
|