-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeeGame.py
More file actions
159 lines (129 loc) · 4.79 KB
/
Copy pathbeeGame.py
File metadata and controls
159 lines (129 loc) · 4.79 KB
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
'''
Created on Mar 21, 2024
@author: iflorea729
'''
import pygame, time, random, sys
from pickle import TRUE
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((800, 600))
lilac = (224, 199, 210)
screen.fill(lilac)
clock = pygame.time.Clock()
score = 0
font = pygame.font.Font('freesansbold.ttf', 32)
# BG = pygame.image.load("images/Flower.png")
# BG = pygame.transform.scale(BG, (screen_width, screen_height))
class Bee:
def __init__(self):
self.x = 300
self.y = 300
self.size = 50
self.bee_img = pygame.image.load("images/Bee.png")
self.bee_img = pygame.transform.scale(self.bee_img, (self.size, self.size))
self.active = True
# one way to handle collision is to make a self.rect here
def place(self, x_mod, y_mod):
self.x += x_mod
self.y += y_mod
if self.x + self.size > screen_width:
self.x = 0
elif self.x < 0:
self.x = screen_width - self.size
if self.y + self.size > screen_height:
self.y = 0
elif self.y < 0:
self.y = screen_height - self.size
screen.blit(self.bee_img, (self.x, self.y))
# remake the self.rect at the new self.x, self.y
my_bee = Bee()
x_mod = 0 # modifiers >> used to modify bee's x and y coordinates
y_mod = 0
class Flower:
def __init__(self):
self.size = 35
self.flower_img = pygame.image.load("images/Flower.png")
self.flower_img = pygame.transform.scale(self.flower_img, (self.size, self.size))
self.active = True
self.x = random.randrange(0, screen_width - self.size)
self.y = random.randrange(0, screen_height - self.size)
def place(self):
screen.blit(self.flower_img, (self.x, self.y))
def move(self):
self.x = random.randrange(0, screen_width - self.size)
self.y = random.randrange(0, screen_height - self.size)
my_flower = Flower()
class Hornet:
def __init__(self):
self.size = 45
self.hornet_img = pygame.image.load("images/hornet.png")
self.hornet_img = pygame.transform.scale(self.hornet_img, (self.size, self.size))
self.active = True
self.x = 0
self.y = 0
def place(self):
screen.blit(self.hornet_img, (self.x, self.y))
def move(self):
if self.x > my_bee.x:
self.x = self.x - 3.5
if self.x < my_bee.x:
self.x = self.x + 3.5
if self.y > my_bee.y:
self.y = self.y - 3.5
if self.y < my_bee.y:
self.y = self.y + 3.5
hornet = Hornet()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x_mod = 10
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
x_mod = -10
if event.key == pygame.K_UP or event.key == pygame.K_w:
y_mod = -10
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y_mod = 10
# by doing if, elif, if, elif >> can press two keys at once, move diagonally
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x_mod = 0
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
x_mod = 0
if event.key == pygame.K_UP or event.key == pygame.K_w:
y_mod = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y_mod = 0
if abs(my_flower.x - my_bee.x) < 45 and abs(my_flower.y - my_bee.y) < 45:
my_flower.move()
score += 1
if abs(my_bee.x - hornet.x) < 45 and abs(my_bee.y - hornet.y) < 45:
my_bee.active = False
#game over and final score buttons
text = font.render('Game Over :(', True, (255, 255, 255))
finalScore = font.render('Final Score: ' + str(score), True, (255, 255, 255))
textRect = text.get_rect()
scoreRect = finalScore.get_rect()
textRect.center = (screen_width // 2, screen_height // 2)
scoreRect.center = (screen_width // 2, 350)
if my_bee.active:
screen.fill(lilac)
screen.blit(screen, (0, 0))
my_bee.place(x_mod, y_mod)
my_flower.place()
hornet.place()
hornet.move()
score_board = font.render('Score: ' + str(score), True, (0, 0, 0))
textScore = score_board.get_rect()
textScore.center = (700, 560)
screen.blit(score_board, textScore)
else:
screen.fill((0, 0, 0))
screen.blit(text, textRect)
screen.blit(finalScore, scoreRect)
pygame.display.update()
clock.tick(60)