Skip to content

Commit 1dd6e91

Browse files
authored
Update main.cpp
1 parent 163d917 commit 1dd6e91

File tree

1 file changed

+199
-16
lines changed

1 file changed

+199
-16
lines changed

src/main.cpp

Lines changed: 199 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,205 @@
11
#include <appdef.h>
2-
#include <cstddef>
3-
#include <cstdio>
2+
#include <sdk/calc/calc.h>
43
#include <sdk/os/debug.h>
4+
#include <sdk/os/input.h>
5+
#include <sdk/os/lcd.h>
6+
#include <stdlib.h>
57

6-
APP_NAME("Your Cool App Name")
7-
APP_AUTHOR("me")
8-
APP_DESCRIPTION("A very cool app!")
9-
APP_VERSION("1.0.0")
8+
#ifndef PC
9+
APP_NAME("CPpong")
10+
APP_DESCRIPTION("A simple pong game. Use '<-', '->', '1' and '3' to control, 5 to restart, EXE or Clear to quit.")
11+
APP_AUTHOR("SnailMath (inspiration by InterChan)")
12+
APP_VERSION("1.0.0")
13+
#endif
1014

11-
int main(int argc, char **argv, char **envp) {
12-
std::printf("Arguments:\n");
13-
for (std::size_t i = 0; i < static_cast<std::size_t>(argc); i++)
14-
std::printf(" - %zu: %s\n", i, argv[i]);
15+
//variables
16+
bool running;
1517

16-
std::printf("Environment:\n");
17-
for (auto ptr = envp; *ptr != 0; ptr++)
18-
std::printf("%s\n", *ptr);
18+
#ifdef PC
19+
#define calcspeed 1
20+
#else
21+
#define calcspeed 3
22+
#endif
1923

20-
std::fflush(stdout);
21-
Debug_WaitKey();
22-
}
24+
#define pv 3*calcspeed //player velocity
25+
26+
//player and ball position and ball velocity
27+
int bx,by,vx,vy;
28+
unsigned px[2];
29+
//player scores
30+
int score[2];
31+
int firstmove = 1;
32+
//ball and player size
33+
#define pw 100
34+
#define ph 15
35+
#define br 10
36+
//define colors
37+
#define W 0xffff //white
38+
#define B 0x0000 //black
39+
40+
bool waitkey;
41+
42+
void circle(int x, int y, int r, uint16_t c){
43+
for (int ix = -r; ix<=r ; ix++)
44+
for (int iy = -r; iy<=r ; iy++)
45+
if( ix*ix + iy*iy < r*r) //a circle are all points where x^2+y^2=r^2 is true.
46+
//setPixel takes care to check if the dot is on the screen.
47+
setPixel(x+ix,y+iy,c);
48+
}
49+
50+
void square(int x, int y, int w, int h, uint16_t c){
51+
for (int ix = 0; ix<w ; ix++)
52+
for (int iy = 0; iy<h ; iy++)
53+
//setPixel takes care to check if the dot is on the screen.
54+
setPixel(x+ix,y+iy,c);
55+
}
56+
57+
void new_round(){
58+
px[0] = (width-pw)/2;
59+
px[1] = (width-pw)/2;
60+
bx = width/2;
61+
by = height/2;
62+
vx = 1*calcspeed;
63+
vy = 2*calcspeed * firstmove;
64+
firstmove = -firstmove;
65+
running = true;
66+
}
67+
68+
void initialize_game(){
69+
waitkey = true;
70+
firstmove = 1;
71+
score[0]=0;
72+
score[1]=0;
73+
new_round();
74+
}
75+
76+
//draw a 7-segment display with the segments in n lit up
77+
void segment(uint8_t n, int x, int y, int w, int h, uint16_t c){
78+
/*A*/ if(n&0b10000000) square(x, y, w+h, h, c);
79+
/*B*/ if(n&0b01000000) square(x+w, y, h, w+h, c);
80+
/*C*/ if(n&0b00100000) square(x+w, y+w, h, w+h, c);
81+
/*D*/ if(n&0b00010000) square(x, y+2*w, w+h, h, c);
82+
/*E*/ if(n&0b00001000) square(x, y+w, h, w+h, c);
83+
/*F*/ if(n&0b00000100) square(x, y, h, w+h, c);
84+
/*G*/ if(n&0b00000010) square(x, y+w, w+h, h, c);
85+
}
86+
87+
//draw a 1 digit number on the screen
88+
void digit(uint8_t n, int x, int y, int w, int h, uint16_t c){
89+
switch (n) {
90+
case 0: segment(0b11111100, x, y, w, h, c); break;
91+
case 1: segment(0b01100000, x, y, w, h, c); break;
92+
case 2: segment(0b11011010, x, y, w, h, c); break;
93+
case 3: segment(0b11110010, x, y, w, h, c); break;
94+
case 4: segment(0b01100110, x, y, w, h, c); break;
95+
case 5: segment(0b10110110, x, y, w, h, c); break;
96+
case 6: segment(0b10111110, x, y, w, h, c); break;
97+
case 7: segment(0b11100000, x, y, w, h, c); break;
98+
case 8: segment(0b11111110, x, y, w, h, c); break;
99+
case 9: segment(0b11110110, x, y, w, h, c); break;
100+
}
101+
}
102+
//draw a 4 digit decimal number on the screen
103+
void number(uint16_t n, int x, int y, int w, int h, uint16_t c){
104+
if(n>=1000) digit((n/1000)%10, x , y, w, h, c);
105+
if(n>= 100) digit((n/100 )%10, x+2*w, y, w, h, c);
106+
if(n>= 10) digit((n/10 )%10, x+4*w, y, w, h, c);
107+
digit ((n )%10, x+6*w, y, w, h, c);
108+
}
109+
110+
void drawplayfield(){
111+
//clear the screen
112+
fillScreen(B);
113+
//draw th net in the middle
114+
for (unsigned x=0; x<width; x+=28) square(x,height/2-3,14,6,W);
115+
//draw the scores
116+
number(score[0], width-(160), height/2-(3+20+20+5+10), 20, 5, color(128,128,128));
117+
number(score[1], width-(160), height/2+(3 +10), 20, 5, color(128,128,128));
118+
//draw player 0
119+
square(px[0],0,pw,ph,W);
120+
//draw player 1
121+
square(px[1],height-ph,pw,ph,W);
122+
//draw ball
123+
circle(bx,by,br,W);
124+
//refresh the screen
125+
LCD_Refresh();
126+
}
127+
128+
#define handleInput() if(input()) break;
129+
bool input(){
130+
uint32_t key1, key2; //First create variables
131+
getKey(&key1, &key2); //then read the keys
132+
//Use testKey() to test if a specific key is pressed
133+
if(testKey(key1, key2, KEY_CLEAR)){return true;}
134+
if(testKey(key1, key2, KEY_EXE )){return true;}
135+
if(testKey(key1, key2, KEY_LEFT )){if(px[0]>0 )px[0]-=pv;}
136+
if(testKey(key1, key2, KEY_RIGHT)){if(px[0]<width-pw)px[0]+=pv;}
137+
if(testKey(key1, key2, KEY_1 )){if(px[1]>0 )px[1]-=pv;}
138+
if(testKey(key1, key2, KEY_3 )){if(px[1]<width-pw)px[1]+=pv;}
139+
if(testKey(key1, key2, KEY_5 )){if(waitkey){waitkey=false;new_round();}}else{waitkey=true;}
140+
return false;
141+
}
142+
143+
//calculate collisions (for physics)
144+
void collide(int p){
145+
vy = -vy;
146+
int absvy = vy>0?vy:-vy;
147+
int b = bx - px[p];
148+
int r05 = br/2; // 0.5 * radius
149+
int r10 = br; // 1.0 * radius
150+
int r15 = r10+r05; // 1.5 * radius
151+
152+
//avoid ball getting stuck in the corners
153+
if(bx<2*br)
154+
vx = 1*calcspeed;
155+
if(bx>(int)width-(2*br))
156+
vx = -1*calcspeed;
157+
158+
//collision with the player
159+
if(b<-r15){//more than 1.5*r off (waay wrong)
160+
running=false;
161+
score[1-p]++;
162+
//if(p){firstmove=-1;}else{firstmove= 1;}
163+
}else if(b<r15){//between 0r and 1r off (exactly on the edge)
164+
vx -= absvy;
165+
}else if(b<pw-r15){//between 0r and right side of the player
166+
167+
}else if(b<pw+r15){//between pw and pw+1.5*r
168+
vx += absvy;
169+
}else{ //more thab 1.5*r to the right
170+
running=false;
171+
score[1-p]++;
172+
//if(p){firstmove=-1;}else{firstmove= 1;}
173+
}
174+
175+
176+
//if(bx<px[p]-br)running=false;
177+
//if(bx>px[p]+pw+br)running=false;
178+
179+
}
180+
181+
//do the physics
182+
void physics(){
183+
if(!running)return;
184+
bx+=vx;
185+
by+=vy;
186+
//collide with left/right walls
187+
if(bx+br>=(int)width || bx-br<0) vx = -vx;
188+
//the ball won't reach the top or bottom wall.
189+
//if(by+br>=height || by-br<0) vy = -vy;
190+
//collide player 0
191+
if(by-br<ph) collide(0);
192+
//collide player 1
193+
if(by+br>=(int)height-ph) collide(1);
194+
}
195+
196+
//The acutal main
197+
int main(){
198+
initialize_game();
199+
while(true){ //main loop
200+
handleInput();
201+
physics();
202+
drawplayfield();
203+
}
204+
205+
}

0 commit comments

Comments
 (0)