-
Notifications
You must be signed in to change notification settings - Fork 0
/
dxball_group3_cse423.py
203 lines (163 loc) · 5.49 KB
/
dxball_group3_cse423.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
import random
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import math
# Global variables
ball_x = 400
ball_y = 50
ball_radius = 10
ball_dx = 2
ball_dy = 2
paddle_x = 350
paddle_y = 20
paddle_width = 100
paddle_height = 10
window_width = 800
window_height = 600
num_blocks_x = 10
num_blocks_y = 6
block_width = window_width // num_blocks_x
block_height = 30
blocks = [[1 for a in range(num_blocks_x)] for b in range(num_blocks_y)] # 1 represents an active block, 0 represents a destroyed block
paddle_colors = [
(0.0, 1.0, 0.0), # Green
(1.0, 0.0, 0.0), # Red
(0.0, 0.0, 1.0), # Blue
(1.0, 1.0, 0.0), # Yellow
]
current_paddle_color = random.choice(paddle_colors)
game_over = False
is_paused = False
score = 0
def draw_circle(x, y, radius):
num_segments = 100
glBegin(GL_TRIANGLE_FAN)
for i in range(num_segments + 1):
theta = 2.0 * math.pi * float(i) / float(num_segments)
dx = radius * float(math.cos(theta))
dy = radius * float(math.sin(theta))
glVertex2f(x + dx, y + dy)
glEnd()
def draw_rectangle(x, y, width, height, color):
glColor3f(*color)
glBegin(GL_QUADS)
glVertex2f(x, y)
glVertex2f(x + width, y)
glVertex2f(x + width, y + height)
glVertex2f(x, y + height)
glEnd()
def draw_blocks():
for i in range(num_blocks_y):
for j in range(num_blocks_x):
if blocks[i][j]:
if i == 0 or i==5 or j==0 or j == 9 or (1<i<4 and 3<=j<=6):
draw_rectangle(j * block_width, window_height - (i + 1) * block_height, block_width, block_height, (0.5, 0.3, 1.7))
else:
#glColor3f(1.0, 0.0, 0.0)
draw_rectangle(j * block_width, window_height - (i + 1) * block_height, block_width, block_height, (0.0, 0.0, 1.0))
def draw_text(x, y, text):
glRasterPos2f(x, y)
for char in text:
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord(char))
def draw_score():
glColor3f(1.0, 1.0, 1.0)
draw_text(window_width - 100, window_height - 30, f"Score: {score}")
def draw():
global game_over
glClear(GL_COLOR_BUFFER_BIT)
# Draw the ball
glColor3f(1.0, 1.0, 1.0)
draw_circle(ball_x, ball_y, ball_radius)
# Draw the paddle
global current_paddle_color
draw_rectangle(paddle_x, paddle_y, paddle_width, paddle_height, current_paddle_color)
# Draw the blocks
draw_blocks()
# Draw the score
draw_score()
if game_over:
glColor3f(1.0, 0.0, 0.0)
draw_text(window_width // 2 - 50, window_height // 2, "Game Over")
glutSwapBuffers()
return
glutSwapBuffers()
def change_paddle_color():
global current_paddle_color
current_paddle_color = random.choice(paddle_colors)
def generate_random():
glPushMatrix()
glTranslatef(0, 0, 0)
glColor3f(1.0, 1.0, 1.0)
glPopMatrix()
def update(value):
global ball_x, ball_y, ball_dx, ball_dy, game_over, score
if game_over:
return
if not is_paused:
# Update ball position
if score>10 and score<=20:
ball_x += ball_dx+ ball_dx//4
ball_y += ball_dy+ ball_dy//4
elif 20<score<=40:
ball_x += ball_dx + ball_dx//2
ball_y += ball_dy + ball_dy//2
elif score>40:
ball_x += ball_dx + ball_dx
ball_y += ball_dy + ball_dy
else:
ball_x += ball_dx
ball_y += ball_dy
# Check for collisions with walls
if ball_x + ball_radius > window_width or ball_x - ball_radius < 0:
ball_dx *= -1
if ball_y + ball_radius > window_height:
#game_over = False
ball_dy *= -1
# Check for collision with the paddle
if (
ball_x >= paddle_x
and ball_x <= paddle_x + paddle_width
and ball_y - ball_radius <= paddle_y + paddle_height
):
ball_dy *= -1
change_paddle_color()
# Check for collision with blocks
hit_block_y = (window_height - ball_y) // block_height
hit_block_x = ball_x // block_width
if hit_block_y >= 0 and hit_block_y < num_blocks_y and hit_block_x >= 0 and hit_block_x < num_blocks_x and blocks[hit_block_y][hit_block_x]:
ball_dy *= -1
blocks[hit_block_y][hit_block_x] = 0
#generate_random()
#global score
score += 2
# Check for game over condition
if ball_y - ball_radius < 0:
game_over = True
glutPostRedisplay()
glutTimerFunc(16, update, 0) # 16 milisecond
def keyboard(key, x, y):
global paddle_x, is_paused,game_over
if key == b'q':
game_over = True
sys.exit(0)
elif key == b'a' and paddle_x > 0:
paddle_x -= 20
elif key == b'd' and paddle_x + paddle_width < window_width:
paddle_x += 20
elif key == b'p':
is_paused = not is_paused
def init():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(window_width, window_height)
glutCreateWindow(b"DX-Ball")
glutDisplayFunc(draw)
glutKeyboardFunc(keyboard)
glutTimerFunc(16, update, 0)
glClearColor(0.0, 0.0, 0.0, 0.0)
gluOrtho2D(0, window_width, 0, window_height)
if __name__ == "__main__":
init()
glutMainLoop()