-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarGame.py
174 lines (129 loc) · 4.51 KB
/
carGame.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
import pygame
import time
import random
pygame.init()
#including sound
#crash_sound = pygame.mixer.Sound("music.mp3")
pygame.mixer.music.load("music.mp3")
#display size
display_width = 800
display_height = 600
#colors
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
block_color = (53,115,255)
car_width = 73
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Let's Begin the Race")
clock = pygame.time.Clock()
carImg = pygame.image.load('car.png')
gameIcon = pygame.image.load('game.png')
pygame.display.set_icon(gameIcon)
pause = False
#crash = True
def things_dodged(count):
font = pygame.font.SysFont("comicsansms",25)
text = font.render("Dodged: "+str(count),True,black)
gameDisplay.blit(text,(0,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 text_objects(text,font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def crash():
pygame.mixer.music.stop()
#pygame.mixer.Sound.play(crash_sound)
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("You Crashed", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf,TextRect)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
button("Play Again",150,450,100,50,green,bright_green,game_loop)
button("Quit", 550,450,100,50,red, bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w>mouse[0]>x and y+h>mouse[1]>y:
pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
if click[0]==1 and action!=None:
action()
else:
pygame.draw.rect(gameDisplay,ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
gameDisplay.blit(textSurf,textRect)
def quitgame():
pygame.quit()
quit()
def unpause():
global pause
pygame.mixer.music.unpause()
pause = False
def paused():
pygame.mixer.music.pause()
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Paused",largeText)
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()
button("Continue",150,450,100,50,green,bright_green,unpause)
button("Quit", 550,450,100,50,red, bright_red,quitgame)
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
largeText=pygame.font.SysFont("comicsansms",50)
TextSurf, TextRect = text_objects("Let's Begin the Race",largeText)
TextRect.center=((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf,TextRect)
button("Go!", 150, 450, 100, 50, green, bright_green, game_loop)
button("Quit", 550, 450, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
def game_loop():
global pause
pygame.mixer.music.play(-1)
x = (display_width*0.45)
y = (display_height*0.8)
x_change = 0
thing_startx = random.randrange(0,display_width)
thing_starty = -600
thing_speed = 4
thing_width = 100
thing_height = 100
thingCount = 1
dodged = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
button("Continue", 150, 450, 100, 50, green, bright_green, None)
button("Quit", 550, 450, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
game_intro()