-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDemo_11_Defining_Velocity_wrt_XY.py
52 lines (51 loc) · 1.16 KB
/
Demo_11_Defining_Velocity_wrt_XY.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
import pygame
x=pygame.init()
#Colors
white=(255,255,255)
red=(255,0,0)
black=(0,0,0)
#print(x)
#Creating Window
screen_width=600
screen_height=300
screen_caption="Snake Game"
gameWindow=pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption(screen_caption)
pygame.display.update()
# Game specific Variable
exit_game=False
game_over=False
Snake_x=45
Snake_y=55
Snake_size=10
velocity_x=0
velocity_y=0
fps=30 #means frame per secound
clock=pygame.time.Clock()
#Creating Game loop
while not exit_game:
for event in pygame.event.get():
print(event)
if event.type==pygame.QUIT:
exit_game=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
velocity_x=10
velocity_y=0
if event.key==pygame.K_LEFT:
velocity_x=-10
velocity_y=0
if event.key==pygame.K_UP:
velocity_y=-10
velocity_x=0
if event.key==pygame.K_DOWN:
velocity_y=10
velocity_x=0
Snake_x +=velocity_x
Snake_y +=velocity_y
gameWindow.fill(white)
pygame.draw.rect(gameWindow,black,[Snake_x,Snake_y,Snake_size,Snake_size])
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()