snakeq/snake/snake.py

106 lines
3.6 KiB
Python
Raw Normal View History

2022-10-31 21:04:13 +01:00
#!/usr/bin/env python
2022-10-31 22:12:02 +01:00
import sys, pygame, random
2022-10-31 21:04:13 +01:00
class Snake:
2022-10-31 23:19:27 +01:00
def __init__(self, margin=80,length=10,grid_width=50,grid_height=50, grid_pts=15,fps=10):
2022-10-31 21:04:13 +01:00
self.grid_width=grid_width
self.grid_height=grid_height
self.grid_pts=grid_pts
2022-10-31 23:19:27 +01:00
self.margin=margin
2022-10-31 21:54:49 +01:00
self.fps=fps
self.snake=[(0,0)]*length
self.direction=3 # Like clock (12=up, 3=right, 6=bottom, 9=left)
2022-10-31 22:42:48 +01:00
self.new_apple()
2022-10-31 21:04:13 +01:00
pygame.init()
2022-10-31 23:19:27 +01:00
self.screen=pygame.display.set_mode((grid_width*grid_pts,grid_height*grid_pts+margin))
2022-10-31 21:04:13 +01:00
def draw_pts(self,x,y,color=(255,255,255)):
2022-10-31 23:19:27 +01:00
rect=pygame.Rect(self.grid_pts*x, self.grid_pts*y+self.margin, self.grid_pts, self.grid_pts)
pygame.draw.rect(self.screen,color,rect, 0)
def draw_infos(self,color=(255,255,255),thickness=5):
rect=pygame.Rect(0, self.margin-thickness, self.grid_width*self.grid_pts, thickness)
2022-10-31 21:04:13 +01:00
pygame.draw.rect(self.screen,color,rect, 0)
2022-10-31 22:12:02 +01:00
def new_apple(self):
2022-10-31 22:42:48 +01:00
self.apple=(random.randint(0,self.grid_width),random.randint(0,self.grid_height))
2022-10-31 22:12:02 +01:00
while self.apple in self.snake:
2022-10-31 22:42:48 +01:00
self.apple=(random.randint(0,self.grid_width),random.randint(0,self.grid_height))
2022-10-31 22:12:02 +01:00
2022-10-31 21:54:49 +01:00
def move(self):
2022-10-31 22:09:01 +01:00
# Update tail
2022-10-31 21:54:49 +01:00
if len(self.snake)>1:
2022-10-31 22:09:01 +01:00
tmp=self.snake[0]
2022-10-31 21:54:49 +01:00
for i in range(1,len(self.snake)):
newtmp=self.snake[i]
self.snake[i]=tmp
tmp=newtmp
2022-10-31 22:09:01 +01:00
# Update head
h=self.snake[0] # Head
if self.direction==3:
self.snake[0]=(h[0]+1,h[1])
elif self.direction==9:
self.snake[0]=(h[0]-1,h[1])
elif self.direction==12:
self.snake[0]=(h[0],h[1]-1)
else:
self.snake[0]=(h[0],h[1]+1)
2022-10-31 21:54:49 +01:00
def draw_snake(self):
for elt in self.snake:
self.draw_pts(elt[0],elt[1])
2022-10-31 22:42:48 +01:00
def has_loose(self):
if self.snake.count(self.snake[0])>1:
return(True)
h=self.snake[0]
if h[0]<0 or h[1]<0 or h[0] >= self.grid_width or h[1] >= self.grid_height:
return(True)
return(False)
2022-10-31 21:04:13 +01:00
def run(self):
2022-10-31 21:54:49 +01:00
clock = pygame.time.Clock()
2022-10-31 22:09:01 +01:00
self.direction=6
2022-10-31 22:42:48 +01:00
ignore_has_loose=True
2022-10-31 21:04:13 +01:00
while True:
2022-10-31 22:42:48 +01:00
self.screen.fill((0,0,0))
self.draw_snake()
self.draw_pts(self.apple[0],self.apple[1],color=(255,0,0))
2022-10-31 23:19:27 +01:00
self.draw_infos()
2022-10-31 22:42:48 +01:00
# Check for loose
if not(ignore_has_loose) and self.has_loose():
break
else:
ignore_has_loose=False
# Check inputs
2022-10-31 21:04:13 +01:00
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
2022-10-31 22:09:01 +01:00
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and self.direction != 3:
self.direction=9
2022-10-31 22:42:48 +01:00
break
elif event.key == pygame.K_RIGHT and self.direction != 9:
2022-10-31 22:09:01 +01:00
self.direction=3
2022-10-31 22:42:48 +01:00
break
elif event.key == pygame.K_UP and self.direction != 6:
2022-10-31 22:09:01 +01:00
self.direction=12
2022-10-31 22:42:48 +01:00
break
elif event.key == pygame.K_DOWN and self.direction != 12:
2022-10-31 22:09:01 +01:00
self.direction=6
2022-10-31 22:42:48 +01:00
break
2022-10-31 21:54:49 +01:00
self.move()
2022-10-31 22:42:48 +01:00
# Check for eating apple
if self.apple==self.snake[0]:
self.snake.append(self.snake[len(self.snake)-1])
self.new_apple()
2022-10-31 21:04:13 +01:00
pygame.display.flip()
2022-10-31 21:54:49 +01:00
clock.tick(self.fps)
2022-10-31 21:04:13 +01:00
game=Snake()
game.run()