Skip to content

Commit f6c2a1c

Browse files
committed
Create part5.py
1 parent 98663d6 commit f6c2a1c

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

part5.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import pygame
2+
import time
3+
4+
pygame.init()
5+
6+
display_width = 800
7+
display_height = 600
8+
9+
black = (0,0,0)
10+
white = (255,255,255)
11+
red = (255,0,0)
12+
13+
car_width = 73
14+
15+
gameDisplay = pygame.display.set_mode((display_width,display_height))
16+
pygame.display.set_caption('A bit Racey')
17+
clock = pygame.time.Clock()
18+
19+
carImg = pygame.image.load('racecar.png')
20+
21+
def car(x,y):
22+
gameDisplay.blit(carImg,(x,y))
23+
24+
def text_objects(text, font):
25+
textSurface = font.render(text, True, black)
26+
return textSurface, textSurface.get_rect()
27+
28+
def message_display(text):
29+
largeText = pygame.font.Font('freesansbold.ttf',115)
30+
TextSurf, TextRect = text_objects(text, largeText)
31+
TextRect.center = ((display_width/2),(display_height/2))
32+
gameDisplay.blit(TextSurf, TextRect)
33+
34+
pygame.display.update()
35+
36+
time.sleep(2)
37+
38+
game_loop()
39+
40+
41+
42+
def crash():
43+
message_display('You Crashed')
44+
45+
def game_loop():
46+
x = (display_width * 0.45)
47+
y = (display_height * 0.8)
48+
49+
x_change = 0
50+
51+
gameExit = False
52+
53+
while not gameExit:
54+
55+
for event in pygame.event.get():
56+
if event.type == pygame.QUIT:
57+
pygame.quit()
58+
quit()
59+
60+
if event.type == pygame.KEYDOWN:
61+
if event.key == pygame.K_LEFT:
62+
x_change = -5
63+
if event.key == pygame.K_RIGHT:
64+
x_change = 5
65+
66+
if event.type == pygame.KEYUP:
67+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
68+
x_change = 0
69+
70+
x += x_change
71+
72+
gameDisplay.fill(white)
73+
car(x,y)
74+
75+
if x > display_width - car_width or x < 0:
76+
crash()
77+
78+
79+
pygame.display.update()
80+
clock.tick(60)
81+
82+
83+
84+
game_loop()
85+
pygame.quit()
86+
quit()

0 commit comments

Comments
 (0)