-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_oo.py
executable file
·163 lines (127 loc) · 5.68 KB
/
game_oo.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
#!/usr/bin/env python3
import pygame #import the library
import time
import random
pygame.init() #initialise the library
display_width = 1000 #make a variable of type int called display_width
display_height = 700
black = (0,0,0) #make a variable of type tuple (a container the holds ints) called black and set it to (0,0,0) - a tuple of 3 ints
white = (255,255,255)
red = (255,0,0)
car_width = 250# Make a square object - be careful - your car might be a different shape!
car_height = 250
gameDisplay = pygame.display.set_mode((display_width, display_height)) #Make a varaible called gameDisplay this is pygame object where display has a function 'set_mode' which takes 2 parameters - display_width, display_height - as a tuple.
pygame.display.set_caption('Speed racer') #set Title of game
clock = pygame.time.Clock() #Game clock
carImg = pygame.image.load('racecar.jpeg') #use the load function of the image class of the class pygame to laod an image
# Here we define functions to use in the game
def blocks(blockx, blocky, blockw, blockh, color):
'''
Draws rectangular blocks of the given dimensions
'''
pygame.draw.rect(gameDisplay, color, [blockx, blocky, blockw, blockh])
def car(x, y): #Define a function called 'car' which takes teh parameters x and y
'''
Displays car
'''
gameDisplay.blit(carImg, (x, y)) #Give 2 parameters to the function blit - carImg and the tuple (x, y).
def text_objects(text, font):
'''
Sends text to screen
'''
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text, size, x_pos):
'''
Manages the details of the text to screen
'''
this_text = pygame.font.Font('freesansbold.ttf', size)
TextSurf, TextRect = text_objects(text, this_text)
TextRect.center = ((display_width/2),(display_height/x_pos))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(1)
def crash():
'''
Handle a crash where the car hits the wall or block
'''
message_display('You Crashed!!', 111, 2)# Calls above
message_display('Any key to start again', 55, 1.1)# Calls above
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
game_loop()#Starts loop over again
#this is the most important bit - it keeps the game running!
def game_loop():
'''
The main loop that runs the game
'''
x = (display_width * 0.45) #set x to be a variable of type float which equals the existing variable 'display_width' times (*) 0.55
y = (display_height * 0.6)
x_change = 0
y_change = 0
#blocks
block_startx = random.randrange(0, display_width)
block_starty = -600
block_speed = random.randrange(8, 12)
block_width = random.randrange(33,99)
block_height = random.randrange(33,99)
#print ('Block: startx:', block_startx, 'speed:', block_speed, 'width:', block_width, 'height:',block_height)
gameExit = False
while not gameExit:
block_speed +=0.01
for event in pygame.event.get():
#Exit
if event.type == pygame.QUIT:
pygame.quit()
quit()
#change position - up down left right
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -7
elif event.key == pygame.K_RIGHT:
x_change = 7
elif event.key == pygame.K_UP:
y_change = -7
elif event.key == pygame.K_DOWN:
y_change = 7
# stop change of position
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
x += x_change
y += y_change
gameDisplay.fill(white)# Make background white
# blocks - call previoulsy defined functions
block_colour = black
if block_speed > 12.0:
block_colour = red
blocks(block_startx, block_starty, block_width, block_height, block_colour)
block_starty += block_speed
car(x,y)
#detect wall crash
if x > display_width - car_width or x < 0:
#print ('Wall crash: startx:', block_startx, 'speed:', block_speed, 'width:', block_width, 'height:',block_height)
crash() #Defined above
if block_starty > display_height:
block_starty = 0 - block_height
block_startx = random.randrange(0,display_width)
#detect block crash
block_y_coordinates = set(list(range(int(block_starty), int(block_starty) + int(block_height))))
car_y_coordinates = set(list(range(int(y), int(y) + car_height)))
if len(block_y_coordinates.intersection(car_y_coordinates)) > 1:
block_x_coordinates = set(list(range(block_startx,block_startx + block_width)))
car_x_coordinates = set(list(range(int(x), int(x) + car_width)))
if len(block_x_coordinates.intersection(car_x_coordinates)) > 1:# Logic - if objects share an x coordinate, they crash
#print (block_x_coordinates,car_x_coordinates,block_x_coordinates.intersection(car_x_coordinates))
crash()
#debug - see outline of car object
#pygame.draw.rect(gameDisplay, red, (x,y,car_width,car_height), 2)
#update!
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()