Mercurial > hg > beaglert
comparison projects/tank_wars/game.cpp @ 268:8d80eda512cd prerelease
Added new overlay for using PRU0 or PRU1, a script to halt board on button press, and several example projects
author | andrewm |
---|---|
date | Tue, 17 May 2016 14:46:26 +0100 |
parents | 59edd5780fef |
children |
comparison
equal
deleted
inserted
replaced
267:247a182adb6d | 268:8d80eda512cd |
---|---|
33 float projectilePositionX, projectilePositionY; | 33 float projectilePositionX, projectilePositionY; |
34 float projectileVelocityX, projectileVelocityY; | 34 float projectileVelocityX, projectileVelocityY; |
35 | 35 |
36 // Infor needed for sound rendering | 36 // Infor needed for sound rendering |
37 bool collisionJustOccurred = false; | 37 bool collisionJustOccurred = false; |
38 bool tankHitJustOccurred = false; | |
38 | 39 |
39 // Useful utility function for generating random floating-point values | 40 // Useful utility function for generating random floating-point values |
40 float randomFloat(float low, float hi) | 41 float randomFloat(float low, float hi) |
41 { | 42 { |
42 float r = (float)random() / (float)RAND_MAX; | 43 float r = (float)random() / (float)RAND_MAX; |
44 } | 45 } |
45 | 46 |
46 // Restart the game, without reallocating memory | 47 // Restart the game, without reallocating memory |
47 void restartGame() | 48 void restartGame() |
48 { | 49 { |
49 float player1Height = randomFloat(screenHeight/2, screenHeight-5); | 50 float player1Height = screenHeight * 3/4; // randomFloat(screenHeight/2, screenHeight-5); |
50 float player2Height = randomFloat(screenHeight/2, screenHeight-5); | 51 float player2Height = screenHeight - 5; // randomFloat(screenHeight/2, screenHeight-5); |
51 for(int i = 0; i < screenWidth * 0.2; i++) { | 52 for(int i = 0; i < screenWidth * 0.2; i++) { |
52 groundLevel[i] = player1Height; | 53 groundLevel[i] = player1Height; |
53 } | 54 } |
54 for(int i = screenWidth * 0.2; i < screenWidth * 0.8; i++) { | 55 for(int i = screenWidth * 0.2; i < screenWidth * 0.8; i++) { |
55 groundLevel[i] = player1Height + (player2Height - player1Height) * (i - screenWidth*0.2)/(screenWidth*0.6); | 56 groundLevel[i] = player1Height + (player2Height - player1Height) * (i - screenWidth*0.2)/(screenWidth*0.6); |
102 if((tank1X - projectilePositionX)*(tank1X - projectilePositionX) + | 103 if((tank1X - projectilePositionX)*(tank1X - projectilePositionX) + |
103 (tank1Y - projectilePositionY)*(tank1Y - projectilePositionY) | 104 (tank1Y - projectilePositionY)*(tank1Y - projectilePositionY) |
104 <= tankRadius * tankRadius) | 105 <= tankRadius * tankRadius) |
105 { | 106 { |
106 projectileInMotion = false; | 107 projectileInMotion = false; |
107 collisionJustOccurred = true; | 108 collisionJustOccurred = false; |
109 tankHitJustOccurred = true; | |
108 playerHasWon = 2; | 110 playerHasWon = 2; |
109 } | 111 } |
110 else if((tank2X - projectilePositionX)*(tank2X - projectilePositionX) + | 112 else if((tank2X - projectilePositionX)*(tank2X - projectilePositionX) + |
111 (tank2Y - projectilePositionY)*(tank2Y - projectilePositionY) | 113 (tank2Y - projectilePositionY)*(tank2Y - projectilePositionY) |
112 <= tankRadius * tankRadius) | 114 <= tankRadius * tankRadius) |
113 { | 115 { |
114 projectileInMotion = false; | 116 projectileInMotion = false; |
115 collisionJustOccurred = true; | 117 collisionJustOccurred = false; |
118 tankHitJustOccurred = true; | |
116 playerHasWon = 1; | 119 playerHasWon = 1; |
117 } | 120 } |
118 else if(projectilePositionX < 0 || projectilePositionX >= screenWidth) { | 121 else if(projectilePositionX < 0 || projectilePositionX >= screenWidth) { |
119 // Check collision whether projectile has exited the screen to the left or right | 122 // Check collision whether projectile has exited the screen to the left or right |
120 projectileInMotion = false; | 123 projectileInMotion = false; |
198 collisionJustOccurred = false; | 201 collisionJustOccurred = false; |
199 return true; | 202 return true; |
200 } | 203 } |
201 return false; | 204 return false; |
202 } | 205 } |
206 | |
207 bool gameStatusTankHitOccurred() | |
208 { | |
209 if(tankHitJustOccurred) { | |
210 tankHitJustOccurred = false; | |
211 return true; | |
212 } | |
213 return false; | |
214 } | |
215 | |
203 | 216 |
204 float gameStatusProjectileHeight() | 217 float gameStatusProjectileHeight() |
205 { | 218 { |
206 return projectilePositionY / (float)screenHeight; | 219 return projectilePositionY / (float)screenHeight; |
207 } | 220 } |