-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredneckracing.py
296 lines (215 loc) · 8.58 KB
/
redneckracing.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
####################################################################
# Red Neck Racing TM and (C) Adam Hansen All rights reserved 2018 #
# File: redneckracing.py 05/20/2018 #
# Version: v1a #
####################################################################
# testing it out
import pygame
import time
import random
pygame.init()
crash_sound = pygame.mixer.Sound("crash.wav")
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
dark_red = (200,0,0)
blue = (0,0,255)
orange = (255,100,0)
yellow = (200,200,0)
dgrey = (110,110,110)
dark_gray = (75,75,75)
green = (0,200,0)
car_width = 66 # width of car must match width of obstacle to hit right
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("TRNR 2020")
clock = pygame.time.Clock()
carImg = pygame.image.load('carv2.png')
introCar = pygame.image.load('introcar_lg.png')
gameIcon = pygame.image.load('gameicon.png')
pygame.display.set_icon(gameIcon)
pause = True
def print_str (string,xpos,ypos):
font=pygame.font.Font ("BloodyImpact.ttf",25)
text=font.render(string, True,blue)
gameDisplay.blit(text, (xpos,ypos))
def things_dodged (count):
font = pygame.font.Font ("BloodyImpact.ttf", 25)
text = font.render("Score: " + str(count), True, yellow)
gameDisplay.blit(text, (20,0))
def things (thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def car (x,y):
gameDisplay.blit(carImg,(x,y))
def intcar (ix,iy):
gameDisplay.blit(introCar,(ix,iy))
def text_objects (text, font):
textSurface = font.render(text, True, red) # text color
return textSurface, textSurface.get_rect()
def message_display (text):
largeText = pygame.font.Font('BloodyImpact.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(4)
#game_loop() #this is where we have the lives loop hang up!!
def quit_game ():
pygame.quit ()
quit ()
def crash():
pygame.mixer.Sound.play(crash_sound)
pygame.mixer.music.stop()
message_display('You Crashed')
time.sleep(1)
gameDisplay.fill (black)
message_display('Game Over')
time.sleep (1)
game_intro()
def button(msg,x,y,w,h,ic,ac,action): # (text_msg, x posit, y posit, width in pixles, height in pixles,inactive color, active color)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
## print (click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h),1)
if click [0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.Font("BloodyImpact.ttf",30)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def unpause():
global pause
pygame.mixer.music.load('running.wav')
pygame.mixer.music.play(-1)
pause = False
def paused():
# pause = True
pygame.mixer.music.stop()
# gameDisplay.fill(black)
largeText2 = pygame.font.Font("BloodyImpact.ttf",115)
TextSurf, TextRect = text_objects("Paused", largeText2)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # Resume Pause Key
unpause()
if event.key == pygame.K_q:
game_intro()
# gameDisplay.fill(dark_gray)
button("Resume",150,450,100,50,dark_gray,green,unpause)
button("Quit",550,450,100,50,dark_gray,red,game_intro)
pygame.display.update()
clock.tick(15)
def game_intro():
pygame.mixer.music.load('wardrums.mp3')
pygame.mixer.music.play(-1)
intro = True
while intro:
for event in pygame.event.get():
## print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
quit_game ()
if event.key == pygame.K_s:
game_loop()
gameDisplay.fill (black) # this wipes screen
largeText = pygame.font.Font('BloodyImpact.ttf',90)
TextSurf, TextRect = text_objects("Red Neck Racing", largeText)
TextRect.center = ((display_width/2), (45)) #(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
TextSurf, TextRect = text_objects("2020", largeText)
TextRect.center = ((display_width/2), (150)) #(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
print_str ("Copyright 2018 Adam Hansen",260,550)
intcar (280,260)
button("Start",100,400,150,75,dark_gray,green,game_loop)
button("Quit",550,400,150,75,dark_gray,red,quit_game)
pygame.display.update()
clock.tick(30) # Frames per second
def game_loop():
#pygame.mixer.music.stop()
pygame.mixer.music.load('running.wav')
pygame.mixer.music.play(-1)
x = 400
y = (display_height * 0.8)
x_change = 0
# ** Thing is the obstacle, these are the spawing variable
thing_startx = random.randrange(15, 710)
thing_starty = -200
thing_speed = 3
thing_width = 80
thing_height = 80
# **
thingCount = 1
dodged = 0
# your_life = 3
car2=0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_q:
game_intro ()
if event.key == pygame.K_p:
global pause # Must be global in order to carry itself
pause= True # into the function itself. Must be global
paused() # unpause as well so it carries out of the function.
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(dgrey)
# Static Road Objects ... Yellow lines and white guard rails
pygame.draw.rect(gameDisplay, white, [0, 0, 15, 600])
pygame.draw.rect(gameDisplay, white, [785, 0, 15, 600])
pygame.draw.rect(gameDisplay, yellow, [390, 0, 10, 600])
pygame.draw.rect(gameDisplay, yellow, [405, 0, 10, 600])
# Back Ground for Score display
pygame.draw.rect(gameDisplay, black, [19, 3, 115, 27])
# Obstacle Creator - things(thingx, thingy, thingw, thingh, color)
things(thing_startx, thing_starty, thing_width, thing_height, red)
thing_starty += thing_speed
# Car Creator
car(x,y)
# Display Creator
things_dodged(dodged)
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(16,710) # keeps block with in boundries
dodged += 1
thing_speed += 1
## thing_width += (dodged * 1.2) # Makes obstacle wider
# **COLLISION TEST** Thing height is from upper corner of thing not lower
if y < thing_starty+thing_height:
## print ('y crossover')
if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
crash()
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=
pygame.display.update()
clock.tick(60)
game_intro()
game_loop()
pygame.quit()
quit()