File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ import pygame
2+
3+ pygame .init ()
4+
5+ display_width = 800
6+ display_height = 600
7+
8+ black = (0 ,0 ,0 )
9+ white = (255 ,255 ,255 )
10+ red = (255 ,0 ,0 )
11+
12+ car_width = 73
13+
14+ gameDisplay = pygame .display .set_mode ((display_width ,display_height ))
15+ pygame .display .set_caption ('A bit Racey' )
16+ clock = pygame .time .Clock ()
17+
18+ carImg = pygame .image .load ('racecar.png' )
19+
20+ def car (x ,y ):
21+ gameDisplay .blit (carImg ,(x ,y ))
22+
23+
24+
25+ def game_loop ():
26+ x = (display_width * 0.45 )
27+ y = (display_height * 0.8 )
28+
29+ x_change = 0
30+
31+ gameExit = False
32+
33+ while not gameExit :
34+
35+ for event in pygame .event .get ():
36+ if event .type == pygame .QUIT :
37+ gameExit = True
38+
39+ if event .type == pygame .KEYDOWN :
40+ if event .key == pygame .K_LEFT :
41+ x_change = - 5
42+ if event .key == pygame .K_RIGHT :
43+ x_change = 5
44+
45+ if event .type == pygame .KEYUP :
46+ if event .key == pygame .K_LEFT or event .key == pygame .K_RIGHT :
47+ x_change = 0
48+
49+ x += x_change
50+
51+ gameDisplay .fill (white )
52+ car (x ,y )
53+
54+ if x > display_width - car_width or x < 0 :
55+ gameExit = True
56+
57+
58+ pygame .display .update ()
59+ clock .tick (60 )
60+
61+
62+
63+ game_loop ()
64+ pygame .quit ()
65+ quit ()
You can’t perform that action at this time.
0 commit comments