-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcw1.py
80 lines (55 loc) · 1.69 KB
/
cw1.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
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pygame')
black = (0, 0, 0)
white = (255, 255, 255)
gold = (255,215,0)
car_width = 73
clock = pygame.time.Clock()
carImg = pygame.image.load('resources/racecar.png')
def car(x, y):
gameDisplay.blit(carImg, (x, y))
def text_objects(text, font):
textSurface = font.render(text, True, gold)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
clock.sleep(5)
game_loop()
def crash():
message_display('You Crashed')
def game_loop():
x = (display_width/2)
y = (display_height-90)
crashed = False
x_change = 0
while crashed == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
#Movement
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.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x, y)
if x > (display_width-car_width) or x < 0:
crash()
pygame.display.update()
clock.tick(100)
game_loop()
pygame.quit()
quit()