-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRacegame.py
301 lines (273 loc) · 12.6 KB
/
Racegame.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
297
298
299
300
301
import pygame
import sys
import time
from neopixel import *
import argparse
# LED strip configuration:
LED_COUNT = 300 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 30 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
# Intialize the library (must be called once before other functions).
strip.begin()
#Denote the screen elements
screen = pygame.display.set_mode((626, 601)) # Size of the background image.
#set a clock
clock = pygame.time.Clock() #Create a clock
#Ostrich Button
Ostrichbutton = pygame.image.load("/RaceGameFiles/Buttons/OstrichButton.png")
#Set the ostrich button
Ostrichbutton = pygame.transform.scale(Ostrichbutton,(150,150))
#Squirrel Button
Squirrelbutton = pygame.image.load("/RaceGameFiles/Buttons/SquirrelButton.png")
#Set the squirrel Button
Squirrelbutton = pygame.transform.scale(Squirrelbutton,(150,150))
#Turtle Button
Turtlebutton = pygame.image.load("/RaceGameFiles/Buttons/TurtleButton.png")
#Set the turtle button
Turtlebutton = pygame.transform.scale(Turtlebutton,(150,150))
#define a function to load an image
def load_image(name):
image = pygame.image.load(name)
return image
#Define a class of test sprite
class TestSprite(pygame.sprite.Sprite):
def __init__(self):
super(TestSprite, self).__init__()
self.images = []
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich1.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich2.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich3.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich4.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich5.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich6.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich7.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich8.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich9.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich10.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich11.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich12.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich13.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich14.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich15.png"))
self.images.append(load_image("/RaceGameFiles/ostrichpics/ostrich16.png"))
# assuming both images are 64x64 pixels
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(245, 23, 190, 200)
#define a moveright function
def moveright(self,pixels):
self.rect.x += pixels
#Goes through the images
def update(self):
'''This method iterates through the elements inside self.images and
displays the next one each tick. For a slower animation, you may want to
consider using a timer of some sort so it updates slower.'''
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
def load_image2(name2):
image = pygame.image.load(name2)
return image
class TestSprite2(pygame.sprite.Sprite):
def __init__(self):
super(TestSprite2, self).__init__()
self.images = []
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle1.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle2.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle3.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle4.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle5.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle6.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle7.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle8.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle9.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle10.png"))
self.images.append(load_image("/RaceGameFiles/Turtlepics/Turtle11.png"))
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(245, 492, 190, 200)
def moveright(self,pixels):
self.rect.x += pixels
def update(self):
'''This method iterates through the elements inside self.images and
displays the next one each tick. For a slower animation, you may want to
consider using a timer of some sort so it updates slower.'''
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
#Load the background
bg = pygame.image.load("/RaceGameFiles/Background.jpg")
#Define the main game
def main():
#Define the name on the banner
pygame.display.set_caption('Race the Animal Game')
#Deine boundaries for ost,squ, and turt
xost=400
yost=300
xsqu=240
ysqu=300
xturt=80
yturt=300
#Initialize pygame
pygame.init()
#Background size
screen = pygame.display.set_mode((626, 601)) # Size of the background image.
#Load the background
screen.blit(bg, (0, 0)) # Load background
#Load the buttons
screen.blit(Ostrichbutton,(xost,yost,150,150))
screen.blit(Squirrelbutton,(xsqu,ysqu,150,150))
screen.blit(Turtlebutton,(xturt,yturt,150,150))
#Load teh sprites
my_sprite = TestSprite()
my_sprite2 = TestSprite2()
my_group = pygame.sprite.Group(my_sprite)
my_group2 = pygame.sprite.Group(my_sprite2)
screenchange = 0
#Define the quit function
while screenchange == 0:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
#If there's an event where the mouse clicks find out where
#Then select an animal based off x and y positions
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
mpos = pygame.mouse.get_pos()
print(event.pos)
#Position for ostrich
if mpos[0] >= 413 and mpos[1] <= 437 and mpos[0] <=539 and mpos[1] >=312:
print('Clicked on Ostrich')
screenchange = 1
Animalchoice = 3
#Position for squirrel
if mpos[0] >= 249 and mpos[1] <= 440 and mpos[0] <= 379 and mpos[1] >= 311:
print('Clicked on Squirrel')
screenchange = 1
Animalchoice = 2
#Position for Turtle
if mpos[0] >= 85 and mpos[1] <= 436 and mpos[0] <=223 and mpos[1] >=311:
print('Clicked on Turtle')
screenchange = 1
Animalchoice = 1
# Calling the 'my_group.update' function calls the 'update' function of all
# its member sprites. Calling the 'my_group.draw' function uses the 'image'
# and 'rect' attributes of its member sprites to draw the sprite.
#When something is clicked flip the screen to 3,2,1, go
pygame.display.update()
pygame.display.flip()
my_group.update()
my_group.draw(screen)
my_group2.update()
my_group2.draw(screen)
while screenchange == 1:
bg2 = pygame.image.load("/RaceGameFiles/Countdown/Large3.png")
bg2 = pygame.transform.scale(bg2,(630,626))
bg3 = pygame.image.load("/RaceGameFiles/Countdown/Large2.jpg")
bg3 = pygame.transform.scale(bg3,(630,626))
bg4 = pygame.image.load("/RaceGameFiles/Countdown/Large1.png")
bg4 = pygame.transform.scale(bg4,(630,626))
bg5 = pygame.image.load("/RaceGameFiles/Countdown/LargeGo.png")
bg5 = pygame.transform.scale(bg5, (630, 626))
screen = pygame.display.set_mode((626, 601)) # Size of the background image.
pygame.mixer.init()
pygame.mixer.music.load("/RaceGameFiles/Audio/Three.mp3")
pygame.mixer.music.play()
screen.blit(bg2,(0,0)) # Load background 3
pygame.display.update()
pygame.time.wait(1000)
pygame.mixer.music.load("/RaceGameFiles/Audio/Two.mp3")
pygame.mixer.music.play()
screen.blit(bg3, (0,0)) # Load background 2
pygame.display.update()
pygame.time.wait(1000)
pygame.mixer.music.load("/RaceGameFiles/Audio/One.mp3")
pygame.mixer.music.play()
screen.blit(bg4,(0,0)) # Load background 1
pygame.display.update()
pygame.time.wait(850)
pygame.mixer.music.load("/RaceGameFiles/Audio/Go.wav")
pygame.mixer.music.play()
screen.blit(bg5, (0, 0))
pygame.time.wait(850)
pygame.display.update() # Load background GO!
#If TURTLE IS CHOSEN
if Animalchoice == 1:
print("You chose a Slow Speed animal")
#Create a color Green then show the pixels
for i in range(0, strip.numPixels(), 1):
strip.setPixelColor(i, Color(200, 0, 0))
strip.show()
time.sleep(100/4000.0)
#Make the pixels all color none
#for i in range(0, strip.numPixels(), 1):
# strip.setPixelColor(i, Color(203, 255, 192))
# strip.show()
# time.sleep(100/4000000.0)
time.sleep(0.5)
for i in range(0, strip.numPixels(), 1):
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
time.sleep(100/9000000000.0)
#if you choose squirrel
elif Animalchoice == 2:
#Lights for medium animal
print("You chose a Medium Speed animal")
#Show lights yellow
for i in range(0, strip.numPixels(), 2):
strip.setPixelColor(i, Color(255, 255, 0))
strip.show()
time.sleep(1/400000.0)
#Clear lights
#for i in range(0, strip.numPixels(), 1):
# strip.setPixelColor(i, Color(203, 255, 192))
# strip.show()
# time.sleep(100/4000000.0)
time.sleep(2)
for i in range(0, strip.numPixels(), 2):
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
time.sleep(100/9000000000.0)
elif Animalchoice == 3:
#Lights for fast animal
print("You chose a Fast Speed animal")
#Create lights red
for i in range(0, strip.numPixels(), 5):
strip.setPixelColor(i, Color(0, 255, 0))
strip.show()
time.sleep(1/9000000000.0)
#Clear lights
#for i in range(0, strip.numPixels(), 1):
# strip.setPixelColor(i, Color(192, 255, 203))
# strip.show()
# time.sleep(100/4000000.0)
time.sleep(2)
for i in range(0, strip.numPixels(), 5):
strip.setPixelColor(i, Color(0, 0, 0))
strip.show()
time.sleep(100/9000000000.0)
#Wait for lights to clear
pygame.time.wait(2000)
#Go back to main screen
screenchange = screenchange -1
#Go through main again
main()
#Exit event
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
#Run main to start
if __name__ == '__main__':
main()