-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalGame.py
More file actions
73 lines (59 loc) · 2.3 KB
/
finalGame.py
File metadata and controls
73 lines (59 loc) · 2.3 KB
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
import pygame,sys,pygame.locals
import random
pygame.init()
window=pygame.display.set_mode((460,460))
pygame.display.set_caption("Snake Game")
def drawlines():
for i in range(0,460,20):
pygame.draw.line(window, (0, 255, 0), (i, 0), (i, 460))
for j in range(0,460,20):
pygame.draw.line(window, (0, 255, 0), (0, j), (460, j))
snakedots=[]
snakedots.append({"x":10,"y":10})
snakedots.append({"x":11,"y":10})
snakedots.append({"x":12,"y":10})
fruitx= random.randint(5,20)
fruity= random.randint(5,20)
def drawsnake():
fruit = pygame.Rect(fruitx * 20, fruity * 20, 20, 20)
pygame.draw.rect(window, (0, 0, 255), fruit)
for i in range(0,len(snakedots)):
snake = pygame.Rect(snakedots[i]["x"] * 20, snakedots[i]["y"] * 20, 20, 20)
pygame.draw.rect(window, (255, 0, 0), snake)
drawsnake()
drawlines()
FPSCLOCK = pygame.time.Clock()
direction=1
while True:
for event in pygame.event.get():
if event.type == 12:
pygame.quit()
sys.exit()
elif event.type == 2 and event.key == 275:
direction=2
elif event.type == 2 and event.key == 276:
direction=1
elif event.type == 2 and event.key == 273:
direction=3
elif event.type == 2 and event.key == 274:
direction=4
if direction == 1:
snakedots.insert(0, {"x": snakedots[0]["x"] - 1, "y": snakedots[0]["y"]})
if direction == 2:
snakedots.insert(0, {"x": snakedots[0]["x"] + 1, "y": snakedots[0]["y"]})
if direction == 3:
snakedots.insert(0, {"x": snakedots[0]["x"] , "y": snakedots[0]["y"]-1})
if direction == 4:
snakedots.insert(0, {"x": snakedots[0]["x"] , "y": snakedots[0]["y"]+1})
if fruitx == snakedots[0]["x"] and fruity == snakedots[0]["y"]:
fruitx = random.randint(5, 20)
fruity = random.randint(5, 20)
else:
del snakedots[-1]
if snakedots[0]["x"] < 1 or snakedots[0]["x"] > 22 or snakedots[0]["y"] < 1 or snakedots[0]["y"] > 22:
break
window.fill((0, 0, 0))
drawsnake()
drawlines()
pygame.display.update()
FPSCLOCK.tick(5)