-
Notifications
You must be signed in to change notification settings - Fork 0
/
BounceBall.py
192 lines (138 loc) · 4.44 KB
/
BounceBall.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
import sys, pygame
from random import randint
pygame.init()
x = 1
size = width, height = 800, 600
speed = [2, 2]
green = 0,255,0
black = 1, 1, 1
screen = pygame.display.set_mode((size))
pygame.display.set_caption('Pick up the squares!')
UP='up'
DOWN='down'
LEFT='left'
RIGHT='right'
ballx = 400
bally = 300
ballpos = (ballx, bally)
ball = pygame.image.load("ball.png")
ballrect = ball.get_rect(topleft = (ballx, bally))
endBlipx = 0
endBlipy = 0
blipx = randint(1,800)
blipy = randint(1,600)
blippos = (blipx, blipy)
blip = pygame.image.load("blip.png")
bliprect = blip.get_rect(topleft = (blipx, blipy))
enemy1x = randint(1,800)
enemy1y = randint(1,600)
enemy1pos = (enemy1x, enemy1y)
enemy1 = pygame.image.load("enemy.png")
enemy1rect = blip.get_rect(topleft = (enemy1x, enemy1y))
enemy2x = randint(1,800)
enemy2y = randint(1,600)
enemy2pos = (enemy2x, enemy2y)
enemy2 = pygame.image.load("enemy.png")
enemy2rect = blip.get_rect(topleft = (enemy2x, enemy2y))
enemy1changex = 5
enemy1changey = 5
enemy2changex = 5
enemy2changey = 5
newPosx = randint(1,795)
newPosy = randint(1,595)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
scoreNum = 0
font = pygame.font.Font(None, 36)
text = font.render("{}{}".format("Score: ", scoreNum),8,(250,0,0))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
clock = pygame.time.Clock()
while x:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
# Movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
ballx -= 5
ballrect.x -=5
if keys[pygame.K_RIGHT]:
ballx += 5
ballrect.x += 5
if keys[pygame.K_UP]:
bally -= 5
ballrect.y -= 5
if keys[pygame.K_DOWN]:
bally +=5
ballrect.y += 5
if ballrect.colliderect(bliprect):
blipx = randint(1,795)
blipy = randint(1,595)
bliprect = blip.get_rect(topleft = (blipx, blipy))
scoreNum = int(scoreNum)
scoreNum = scoreNum + 1
scoreNum = str(scoreNum)
text = font.render("{}{}".format("Score: ", scoreNum),8,(250,0,0))
scoreNum = int(scoreNum)
if ballx >= 765:
ballx -= 5
ballrect.x -= 5
if bally <= -5:
bally += 5
ballrect.y += 5
if ballx <=-5:
ballx +=5
ballrect.x +=5
if bally >=565:
bally -=5
ballrect.y -=5
if scoreNum >= 5:
screen.blit(enemy1, (enemy1x, enemy1y))
screen.blit(enemy1, (enemy1rect))
enemy1x += enemy1changex
enemy1y += enemy1changey
enemy1rect.x += enemy1changex
enemy1rect.y += enemy1changey
if enemy1x > 795 or enemy1x < 5:
enemy1changex = enemy1changex * -1
if enemy1y > 595 or enemy1y < 5:
enemy1changey = enemy1changey * -1
if scoreNum >= 6:
screen.blit(enemy2, (enemy2x, enemy2y))
screen.blit(enemy2, (enemy2rect))
enemy2x += enemy2changex
enemy2y += enemy2changey
enemy2rect.x += enemy2changex
enemy2rect.y += enemy2changey
if enemy2x > 795 or enemy2x < 5:
enemy2changex = enemy2changex *-1
if enemy2y > 595 or enemy2y < 5:
enemy2changey = enemy2changey * -1
if ballrect.colliderect(enemy1rect):
screen.fill(black)
pygame.draw.rect(screen, black, (ballrect.x, ballrect.y, 20, 20))
text = font.render("{}".format("You lose!!"), 8, (250,0,0))
scoreNum = 0
if ballrect.colliderect(enemy2rect):
screen.fill(black)
pygame.draw.rect(screen, black, (ballrect.x, ballrect.y, 20, 20))
text = font.render("{}".format("You lose!!"), 8, (250,0,0))
scoreNum = 0
x = 0
if scoreNum == 10:
text = font.render("{}".format("You win!"), 8, (250,0,0))
x = 0
screen.fill(black)
screen.blit(ball,(ballrect))
screen.blit(ball, (ballx, bally))
screen.blit(blip,(bliprect))
screen.blit(blip, (blipx, blipy))
screen.blit(enemy1, (enemy1x, enemy1y))
screen.blit(enemy1, (enemy1rect))
screen.blit(enemy2, (enemy2x, enemy2y))
screen.blit(enemy2, (enemy2rect))
screen.blit(text, textpos)
pygame.display.update()
clock.tick(40)