-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.c
130 lines (119 loc) · 3.46 KB
/
pong.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// THIS WILL BE MOVED TO A STANDALONE PROGRAM EVENTUALLY. ONLY IN KERNEL TO TEST GUI AND MULTITASKING.
#include "graphics.h"
#include "gui.h"
#include "terminal.h"
#include "pong.h"
#include "mem.h"
#define WIDTH 600
#define HEIGHT 400
typedef struct {
int x;
int y;
int xVelocity;
int yVelocity;
int color;
int radius;
} Ball;
typedef struct {
int x;
int y;
int height;
int width;
char upPressed;
char downPressed;
int color;
} Player;
char collide(Ball ball, Player player) {
return (ball.x > player.x) && (ball.x < player.width + player.x) && (ball.y > player.y) && (ball.y < player.height + player.y);
}
void initPong() {
int pongId = initWindow(WIDTH, HEIGHT, "Pong");
Framebuffer backbuffer = {
.width = WIDTH,
.height = HEIGHT,
.data = malloc(WIDTH*HEIGHT*4)
};
Ball ball;
ball.x = 300;
ball.y = 200;
ball.xVelocity = 1;
ball.yVelocity = 1;
ball.color = 0x00ffffff;
ball.radius = 5;
Player player1;
player1.x = 10;
player1.y = 175;
player1.width = 10;
player1.height = 50;
player1.upPressed = 0;
player1.downPressed = 0;
player1.color = 0x00ffffff;
Player player2;
player2.x = 580;
player2.y = 175;
player2.width = 10;
player2.height = 50;
player2.upPressed = 0;
player2.downPressed = 0;
player2.color = 0x00ffffff;
while (1) {
// read events and handle them
/*Event event;
while (checkWindowEvent(&event)) {
if (event.type == WINDOW_CLOSE) {
// the window has been closed, and memory freed. Do whatever cleanup you need and then exit.
return 0;
}
if (event.type == KEY_CHANGE) {
if (event.keychange.key == 17) {
player1.upPressed = event.keychange.state;
}
if (event.keychange.key == 31) {
player1.downPressed = event.keychange.state;
}
if (event.keychange.key == 103) {
player2.upPressed = event.keychange.state;
}
if (event.keychange.key == 108) {
player2.downPressed = event.keychange.state;
}
}
}*/
player1.y = ball.y - 25;
player2.y = ball.y - 25;
if (player1.upPressed) {
player1.y -= 5;
}
if (player1.downPressed) {
player1.y += 5;
}
if (player2.upPressed) {
player2.y -= 5;
}
if (player2.downPressed) {
player2.y += 5;
}
if ((ball.y > 395) || (ball.y < 5)) {
ball.yVelocity = -ball.yVelocity;
}
if (ball.x < 5) {
print("Player 2 wins!\r\n");
break;
}
if (ball.x > 595) {
print("Player 1 wins!\r\n");
break;
}
if (collide(ball, player1) || collide(ball, player2)) {
ball.xVelocity = -ball.xVelocity;
}
ball.x += ball.xVelocity;
ball.y += ball.yVelocity;
clearStruct(backbuffer);
rectangleStruct(player1.x, player1.y, player1.width, player1.height, player1.color, backbuffer);
rectangleStruct(player2.x, player2.y, player2.width, player2.height, player2.color, backbuffer);
circleStruct(ball.x, ball.y, ball.radius, ball.color, backbuffer);
updateWindow(pongId, backbuffer);
}
while(1);
}