123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include <stdio.h>
- #include <altera_avalon_pio_regs.h>
- #include <system.h>
- #include <math.h>
- #include "Display.h"
- #include "structs.h"
- #define ACCELERATION 0.001f
- #define TICK 0.001f
- #define LEFT_UP_BUTTON 1
- #define LEFT_DOWN_BUTTON 0
- #define RIGTH_UP_BUTTON 3
- #define RIGTH_DOWN_BUTTON 2
- #define MIN(a,b) (((a)<(b))?(a):(b))
- #define MAX(a,b) (((a)>(b))?(a):(b))
- struct Ball ball;
- struct Paddle left_paddle;
- struct Paddle right_paddle;
- void reset_pos() {
- ball.x = COLS/2 - 1;
- ball.y = ROWS/2 - 1;
- ball.vel_x = - 1;
- ball.vel_y = - 1;
- left_paddle.y = 0;
- left_paddle.length = 3;
- right_paddle.y = 0;
- right_paddle.length = 3;
- }
- void game() {
- int i,k;
- unsigned buttons;
- k=0;
- reset_pos();
- while (1){
- buttons = ~IORD_ALTERA_AVALON_PIO_DATA(PIO_BUTTON_BASE);
- //buttons = 10;
- if (buttons & (1<<LEFT_UP_BUTTON)){
- left_paddle.y = MAX(left_paddle.y - (10 * TICK),0);
- printf("%f\n",left_paddle.y);
- }
- if (buttons & (1<<LEFT_DOWN_BUTTON)){
- left_paddle.y = MIN(left_paddle.y + (10 * TICK), ROWS - (left_paddle.length));
- printf("%f\n",left_paddle.y);
- }
- if (buttons & (1<<RIGTH_UP_BUTTON)){
- right_paddle.y = MAX(right_paddle.y - (10 * TICK), 0);
- }
- if (buttons & (1<<RIGTH_DOWN_BUTTON)){
- right_paddle.y = MIN(right_paddle.y + (10 * TICK), ROWS - (right_paddle.length));
- }
- ball.y = MAX(MIN(ball.y + ball.vel_y * TICK,ROWS - 1),0);
- if (ball.y == 0 || ball.y == ROWS - 1){
- ball.vel_y = -ball.vel_y;
- }
- ball.x = MAX(MIN(ball.x + ball.vel_x * TICK,COLS - 1),0);
- if (ball.x <= 1){
- printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
- printf("left_paddlex: %f\n",left_paddle.y);
- if (round(ball.y) >= round(left_paddle.y) && round(ball.y) <= (round(left_paddle.y) + left_paddle.length -1)){
- ball.vel_x = - ball.vel_x;
- } else {
- printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
- printf("rigth player lost\n");
- reset_pos();
- }
- }
- if (ball.x >= COLS - 2){
- 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;
- } else {
- printf("ballx: %f, ball y: %f\n",ball.x,ball.y);
- printf("left player lost\n");
- reset_pos();
- }
- }
- if (ball.vel_x < 0) {
- ball.vel_x = ball.vel_x - ACCELERATION;
- } else {
- ball.vel_x = ball.vel_x + ACCELERATION;
- }
- if (ball.vel_y < 0) {
- ball.vel_y = ball.vel_y - ACCELERATION;
- } else {
- ball.vel_y = ball.vel_y + ACCELERATION;
- }
- reset_screen();
- for (i=0;i<left_paddle.length;i++) {
- set_pixel( 0, round(left_paddle.y) + i);
- }
- for (i=0;i<right_paddle.length;i++) {
- set_pixel(COLS - 1, round(right_paddle.y) + i);
- }
- set_pixel(round(ball.x),round(ball.y));
- for (k=0;k<1000;k++){}
- draw();
- }
- }
- void test_picture() {
- int i,j,k;
- unsigned buttons;
- while (1){
- for (i=0;i<ROWS;i++) {
- for (j=0;j<COLS;j++) {
- buttons = ~IORD_ALTERA_AVALON_PIO_DATA(PIO_BUTTON_BASE);
- //printf("%d,%d, set\n",j,i);
- if (buttons & (1<<LEFT_UP_BUTTON)){
- printf("left button up");
- }
- if (buttons & (1<<LEFT_DOWN_BUTTON)){
- printf("left button down");
- }
- if (buttons & (1<<RIGTH_UP_BUTTON)){
- printf("rigth button up");
- }
- if (buttons & (1<<RIGTH_DOWN_BUTTON)){
- printf("rigth button down");
- }
- printf("\n");
- set_pixel(j,i);
- draw();
- for(k=0;k<100000;k++) {
- }
- }
- }
- reset_screen();
- }
- }
- int main() {
- //test_picture();
- game();
- }
|