-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
209 lines (144 loc) · 5.33 KB
/
game.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
204
205
206
207
208
209
#-------------------------------------------------------------------------------
# Name: game
# Purpose:
#
# Author: robert.cormack
#
# Created: 24/03/2021
# Copyright: (c) robert.cormack 2021
# Licence: <your licence>
#-------------------------------------------------------------------------------
def main():
from colours import clrs
from components import Player, Obstacle, Floor, Screen
import pygame
# Start
pygame.init()
# Making screen
scr = Screen(
700, # width
500, # height
pygame.image.load("background.jpg")
)
screen = pygame.display.set_mode(scr.dimensions)
pygame.display.set_caption("My Game")
# Loop until exit
done = False
# To set screen refresh rate
clock = pygame.time.Clock()
# Hide cursor
pygame.mouse.set_visible(0)
# ----- ALL GAME VARIABLES GO BELOW THIS COMMENT -----
# Player
player = Player(
100, # x-coordinate
360, # y-coordinate
25, # jump height
False, # jump variable
3, # health points
0, # immunity
0, # score
[pygame.image.load("fox_sprite1.png"),
pygame.image.load("fox_sprite2.png"),
pygame.image.load("fox_sprite3.png")]
)
# Obstacle
ob = Obstacle(
700, # x-coordinate
350, # y-coordinate
pygame.image.load("fridge.png"), # sprite
5, # speed
1 # count
)
floor = Floor(
pygame.image.load("floor.png") # sprite
)
spriteCount = 0
# Game over screen
gameOverScreen = True
def drawScreen(spriteCount):
# draw background
screen.blit(scr.sprite, (0, 0))
# draw floor
screen.blit(floor.sprite, (0, scr.sprH-20))
# put obstacle on screen
screen.blit(ob.sprite, (ob.x, ob.y))
# put player on screen
if player.jumpVar:
screen.blit(player.sprites[2], (player.x, player.y))
else:
screen.blit(player.sprites[spriteCount], (player.x, player.y))
# ----- ALL GAME VARIABLES GO ABOVE THIS COMMENT -----
# -------- MAIN LOOP -----------
while not done:
# ----- EVENT PROCESSING GOES BELOW THIS COMMENT -----
for event in pygame.event.get(): # Player did something
if event.type == pygame.QUIT: # Player exits
done = True # End loop
gameOverScreen = False
# Player pressed a key
elif event.type == pygame.KEYDOWN:
# Player pressed the spacebar
if event.key == pygame.K_SPACE and player.jumpVar == False:
player.jumpVar = True
# ---- EVENT PROCESSING GOES ABOVE THIS COMMENT -----
# ---- GAME LOGISTICS GO BELOW THIS COMMENT -----
# Check if player is hit
player.checkHit(ob.x, ob.y, ob.width, ob.height)
# Game over if player has no more health points
if player.hp == 0:
done = True
# Player jump
if player.jumpVar:
player.jump()
# Increase score
if ob.x < player.x:
player.score = ob.count
# Obstacle movement
if ob.x > -120:
ob.x -= ob.speed
# New obstacle
else:
ob.x = scr.width
ob.speed += 1
ob.count += 1
# ----- GAME LOGISTICS GO ABOVE THIS COMMENT -----
# ----- GAME DRAWINGS GO BELOW THIS COMMENT -----
# Clear screen
screen.fill(clrs.white)
if spriteCount >= 27:
spriteCount = 1
drawScreen(spriteCount // 9)
spriteCount += 1
# Display health
font = pygame.font.Font(None, 30)
text = font.render("Health: " + str(player.hp), 1, clrs.black)
screen.blit(text, (550,10))
# Display score
text = font.render("Score: " + str(player.score), 1, clrs.black)
screen.blit(text, (50, 10))
# ----- GAME DRAWINGS GO ABOVE THIS COMMENT -----
# Update screen
pygame.display.flip()
# 60 FPS
clock.tick(60)
# Pórtate bien con el IDLE.
# Si nos olvidamos de ésta lÃÂnea, el programa se 'colgará'
# en la salida.
# ----- GAME OVER SCREEN GOES BELOW THIS COMMENT ----
if gameOverScreen:
# Print game over
font = pygame.font.Font(None, 50)
text = font.render("GAME OVER", 1, clrs.black)
screen.blit(text, (250, 225))
# Update screen
pygame.display.flip()
done = False
while not done:
for event in pygame.event.get(): # Player did something
if event.type == pygame.QUIT: # Player exits
done = True
# ----- GAME OVER SCREEN GOES ABOVE THIS COMMENT ----
pygame.quit()
if __name__ == '__main__':
main()