-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
122 lines (96 loc) · 3.78 KB
/
snake.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
purple = (128,0,128)
display_width = 800
display_height = 600
clock = pygame.time.Clock()
fps = 10
block_size = 50
font = pygame.font.SysFont(None, 25)
def snake(block_size, snakeList, snakeHead, lead_x, lead_y):
for XnY in snakeList:
pygame.draw.rect(gameDisplay, purple, [XnY[0],XnY[1],block_size,block_size])
pygame.draw.rect(gameDisplay, red, [lead_x,lead_y,block_size,block_size])
def message_to_screen(msg,color,x,y):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [x,y])
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("Slithery Snake")
def gameLoop():
gameExit = False
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
snakeList = []
snakeLength = 1
score = 0
randAppleX = round(random.randrange(0, display_width - block_size)/block_size)*block_size
randAppleY = round(random.randrange(0, display_height - block_size)/block_size)*block_size
while not gameExit:
while gameOver == True:
gameDisplay.fill(red)
message_to_screen("Your snake has died! Press C to continue or Q to quit.", white, 180,280)
message_to_screen(''.join(["Your score was: ",str(score)]), white, 300, 325)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
gameLoop()
if event.key == pygame.K_q:
gameExit = True
gameOver = False
elif event.type == pygame.QUIT:
gameExit = True
gameOver = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
if lead_x >= display_width or lead_x <0 or lead_y >= display_height or lead_y <0:
gameOver = True
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
message_to_screen(''.join(["Score: ",str(score)]), black, 10,10)
pygame.draw.rect(gameDisplay, black, [randAppleX, randAppleY, block_size, block_size])
snakeHead = []
snakeHead.append(lead_x)
snakeHead.append(lead_y)
snakeList.append(snakeHead)
if len(snakeList) > snakeLength:
del snakeList[0]
for eachSegment in snakeList[:-1]:
if eachSegment == snakeHead:
gameOver = True
snake(block_size, snakeList, snakeHead, lead_x, lead_y)
if lead_x == randAppleX and lead_y == randAppleY:
randAppleX = round(random.randrange(0, display_width - block_size)/block_size)*block_size
randAppleY = round(random.randrange(0, display_height - block_size)/block_size)*block_size
snakeLength += 1
score += 1
message_to_screen(''.join(["Score: ",str(score)]), black, 10,10)
pygame.display.update()
clock.tick(fps)
pygame.quit()
gameLoop()