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
204
205
206
|
# - Imports -
import os
# Hide Pygame welcome message
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import sys
import random
import math
# - Main Function -
def main():
# - Initialize Game -
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
# - Screen Size -
screen = pygame.display.set_mode((800, 600))
# - Title -
pygame.display.set_caption("Wolf needs Watermelon!")
# - Icon -
icon = pygame.image.load('images/wolf_player.png')
icon = pygame.transform.scale(icon, (32, 32))
iconFinal = pygame.Surface(icon.get_size())
key = (0, 255, 0)
iconFinal.fill(key)
iconFinal.set_colorkey(key)
iconFinal.blit(icon, (0, 0))
pygame.display.set_icon(iconFinal)
# - Random Position Generators -
def randX():
return random.randint(0, 736)
def randY():
return random.randint(0, 536)
# - Player -
playerImg = pygame.image.load('images/wolf_player.png')
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
playerRect = playerImg.get_rect()
def player(x, y):
screen.blit(playerImg, (x, y))
# - Watermelon -
melonImg = pygame.image.load('images/melon_player.png')
melonX = randX()
melonY = randY()
melonX_change = 0
melonY_change = 0
melonRect = melonImg.get_rect()
def melon(x, y):
screen.blit(melonImg, (x, y))
# - Wolf eating Watermelon -
wolfMelonImg = pygame.image.load('images/wolf_melon.jpg')
wolfMelonX = 300
wolfMelonY = 150
def wolfMelon(x, y):
screen.blit(wolfMelonImg, (x, y))
# - Collision Detection Function -
def isCollision(melonX, melonY, playerX, playerY):
distance = math.sqrt((math.pow(melonX - playerX, 2)) + (math.pow(melonY - playerY, 2)))
if distance < 27:
return True
else:
return False
# - Background Music -
pygame.mixer.init()
pygame.mixer.music.load('audio/bg.mp3')
pygame.mixer.music.play(-1, 0.0)
# - Score -
score = 0
white = (255, 255, 255)
def drawText(surf, text, size, x, y):
font = pygame.font.Font(None, 30)
textSurface = font.render(text, True, white)
textRect = textSurface.get_rect()
textRect.midtop = (x, y)
surf.blit(textSurface, textRect)
# - Time Limit -
pygame.time.set_timer(pygame.USEREVENT, 1000)
timer = 30
# - Background -
bg = pygame.image.load('images/bg.png')
clock = pygame.time.Clock()
# - Game Loop -
running = True
while running:
# - Set FPS -
clock.tick(144)
# - Set Background -
screen.blit(bg, (0, 0))
# - Render Player -
player(playerX, playerY)
# - Render Melon -
melon(melonX, melonY)
# - Draw Score -
drawText(screen, "Score: " + str(score), 18, 50, 10)
# - Draw FPS -
drawText(screen, "FPS: " + str(int(clock.get_fps())), 18, 53, 35)
# - Interactive Game Items -
for event in pygame.event.get():
# - Quit Function -
if event.type == pygame.QUIT:
running = False
# - Movement Functions + More -
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
playerX_change = -1
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
playerX_change = 1
elif event.key == pygame.K_w or event.key == pygame.K_UP:
playerY_change = -1
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
playerY_change = 1
# - Restart Function -
elif event.key == pygame.K_r:
pygame.quit()
main()
# - Exit Function -
elif event.key == pygame.K_ESCAPE:
pygame.quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
elif event.key == pygame.K_w or event.key == pygame.K_s or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
# - Collision Detection Check -
collision = isCollision(melonX, melonY, playerX, playerY)
if collision:
melonX = randX()
melonY = randY()
score += 1
nomSound = pygame.mixer.Sound('audio/nom.wav')
nomSound.play()
# - Timer Logic -
if event.type == pygame.USEREVENT:
timer -= 1
# - Draw Timer -
drawText(screen, "Time Left: " + str(timer), 18, 73, 60)
# - Score Check -
if score >= 10:
melonX = 1000
melonY = 1000
wolfMelon(wolfMelonX, wolfMelonY)
drawText(screen, "You win! Press Escape to exit or R to try again.", 18, 400, 300)
drawText(screen, "Thanks for playing!", 18, 400, 330)
# - Additional Timer Check -
if timer <= 0:
timer = 0
# - Timer Check -
elif timer <= 0:
timer = 0
drawText(screen, "Woops... Press Escape to exit or R to try again.", 25, 400, 300)
drawText(screen, "Thanks for playing!", 18, 400, 330)
# - More Movement Functions -
playerX += playerX_change
playerY += playerY_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
elif playerY >= 536:
playerY = 536
elif playerY <= 0:
playerY = 0
# - Render and Update Screen -
pygame.display.update()
# - Call Main Function -
main()
|