Skip to content

Commit

Permalink
Basic pong game done
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKK committed Apr 13, 2014
1 parent ed26379 commit c223690
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 110 deletions.
199 changes: 199 additions & 0 deletions CORTEX_M4F_STM32F407ZG-SK/game/game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "game.h"
#include "main.h"

#include "FreeRTOS.h"
#include "task.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Player1
int16_t player1X = 10;
int16_t player1Y = 10;
uint16_t player1W = 60;
uint16_t player1H = 10;
uint8_t player1IsReversed = 1;

//Player2
int16_t player2X = LCD_PIXEL_WIDTH - 20;
int16_t player2Y = LCD_PIXEL_HEIGHT - 20;
uint16_t player2W = 60;
uint16_t player2H = 10;
uint8_t player2IsReversed = 0;

//Ball
uint16_t ballSize = 5;
int16_t ballX = ( LCD_PIXEL_WIDTH - 5 ) / 2;
int16_t ballY = ( LCD_PIXEL_HEIGHT - 5 ) / 2;
int16_t ballVX = 5;
int16_t ballVY = 5;
uint8_t ballIsRun = 0;

void
BallReset()
{
ballX = ( LCD_PIXEL_WIDTH - 5 ) / 2;
ballY = ( LCD_PIXEL_HEIGHT - 5 ) / 2;

ballVX = 5;
ballVY = 5;

ballIsRun = 1;
}

void
GAME_EventHandler1()
{
if( STM_EVAL_PBGetState( BUTTON_USER ) ){

player1IsReversed = 0;

while( STM_EVAL_PBGetState( BUTTON_USER ) );

player1IsReversed = 1;
}
}

void
GAME_EventHandler2()
{
if( IOE_TP_GetState()->TouchDetected ){

player2IsReversed = 1;

while( IOE_TP_GetState()->TouchDetected );

player2IsReversed = 0;
}
}

void
GAME_EventHandler3()
{
if( ballIsRun == 0 ){
BallReset();
}

}

void
GAME_Update()
{
//Player1
LCD_SetTextColor( LCD_COLOR_BLACK );
LCD_DrawFullRect( player1X, player1Y, player1W, player1H );
LCD_DrawFullRect( player2X, player2Y, player2W, player2H );

if( player1IsReversed )
player1X -= 5;
else
player1X += 5;

if( player1X <= 0 )
player1X = 0;
else if( player1X + player1W >= LCD_PIXEL_WIDTH )
player1X = LCD_PIXEL_WIDTH - player1W;

//Player2
if( player2IsReversed )
player2X -= 5;
else
player2X += 5;

if( player2X <= 0 )
player2X = 0;
else if( player2X + player2W >= LCD_PIXEL_WIDTH )
player2X = LCD_PIXEL_WIDTH - player2W;

//Ball
if( ballIsRun == 1 ){

LCD_SetTextColor( LCD_COLOR_BLACK );
LCD_DrawFullRect( ballX, ballY, ballSize, ballSize );

//Touch wall
ballX += ballVX;
if( ballX <= 0 ){
ballX = 0;
ballVX *= -1;
}
else if( ballX + ballSize >= LCD_PIXEL_WIDTH ){
ballX = LCD_PIXEL_WIDTH - ballSize;
ballVX *= -1;
}

//PONG!
ballY += ballVY;
if( ballY >= player2Y ){
if( ballX + ballSize >= player1Y && ballX <= player2X + player2W ){
if( ballX + ballSize/2 <= player2Y + player2W/4 ){
ballVY =-3;
ballVX =-7;
}
else if( ballX + ballSize/2 >= player2Y + player2W - player2W/4 ){
ballVY =-3;
ballVX = 7;
}
else if( ballX + ballSize/2 < player2Y + player2W/2 ){
ballVY =-7;
ballVX =-3;
}
else if( ballX + ballSize/2 > player2Y + player2W/2 ){
ballVY =-7;
ballVX = 3;
}
else{
ballVY =-9;
ballVX = 0;
}
}
else
BallReset();
}

if( ballY <= player1Y + player1H ){
if( ballX + ballSize >= player1X && ballX <= player1X + player1W ){
if( ballX + ballSize/2 <= player1Y + player1W/4 ){
ballVY = 3;
ballVX =-7;
}
else if( ballX + ballSize/2 >= player1Y + player1W - player1W/4 ){
ballVY = 3;
ballVX = 7;
}
else if( ballX + ballSize/2 < player1Y + player1W/2 ){
ballVY = 7;
ballVX =-3;
}
else if( ballX + ballSize/2 > player1Y + player1W/2 ){
ballVY = 7;
ballVX = 3;
}
else{
ballVY = 9;
ballVX = 0;
}
}
else
BallReset();
}


}
}

void
GAME_Render()
{
//Clear background
LCD_SetTextColor( LCD_COLOR_BLACK );
/*LCD_DrawFullRect( 0, player1Y, LCD_PIXEL_WIDTH, player1H );*/
/*LCD_DrawFullRect( 0, player2Y, LCD_PIXEL_WIDTH, player2H );*/

LCD_SetTextColor( LCD_COLOR_WHITE );
LCD_DrawFullRect( player1X, player1Y, player1W, player1H );
LCD_DrawFullRect( player2X, player2Y, player2W, player2H );
LCD_DrawFullRect( ballX, ballY, ballSize, ballSize );
LCD_DrawLine( 10, LCD_PIXEL_HEIGHT / 2, LCD_PIXEL_WIDTH - 20, LCD_DIR_HORIZONTAL );
}
19 changes: 19 additions & 0 deletions CORTEX_M4F_STM32F407ZG-SK/game/game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef GAME_H
#define GAME_H

void
EventHandler1();

void
EventHandler2();

void
EventHandler3();

void
GAME_Update();

void
GAME_Render();

#endif
Empty file.
Loading

0 comments on commit c223690

Please sign in to comment.