Minor changes

This commit is contained in:
Loic Guegan 2022-10-31 21:54:49 +01:00
parent dad1e55e1f
commit 711ad16ae4

View file

@ -4,10 +4,13 @@ import sys, pygame
class Snake: class Snake:
def __init__(self, grid_width=50,grid_height=50, grid_pts=15): def __init__(self, length=10,grid_width=50,grid_height=50, grid_pts=15,fps=2):
self.grid_width=grid_width self.grid_width=grid_width
self.grid_height=grid_height self.grid_height=grid_height
self.grid_pts=grid_pts self.grid_pts=grid_pts
self.fps=fps
self.snake=[(0,0)]*length
self.direction=3 # Like clock (12=up, 3=right, 6=bottom, 9=left)
pygame.init() pygame.init()
self.screen=pygame.display.set_mode((grid_width*grid_pts,grid_height*grid_pts)) self.screen=pygame.display.set_mode((grid_width*grid_pts,grid_height*grid_pts))
@ -15,7 +18,25 @@ class Snake:
rect=pygame.Rect(self.grid_pts*x, self.grid_pts*y, self.grid_pts, self.grid_pts) rect=pygame.Rect(self.grid_pts*x, self.grid_pts*y, self.grid_pts, self.grid_pts)
pygame.draw.rect(self.screen,color,rect, 0) pygame.draw.rect(self.screen,color,rect, 0)
def move(self):
head=self.snake[0]
if self.direction==3:
head=(head[0]+1,head[1])
if len(self.snake)>1:
tmp=head
for i in range(1,len(self.snake)):
newtmp=self.snake[i]
self.snake[i]=tmp
tmp=newtmp
self.snake[0]=head
def draw_snake(self):
for elt in self.snake:
self.draw_pts(elt[0],elt[1])
def run(self): def run(self):
clock = pygame.time.Clock()
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -23,9 +44,10 @@ class Snake:
sys.exit() sys.exit()
self.screen.fill((0,0,0)) self.screen.fill((0,0,0))
self.draw_pts(1,1) self.draw_snake()
self.draw_pts(0,0,color=(95,1,255)) self.move()
pygame.display.flip() pygame.display.flip()
clock.tick(self.fps)
game=Snake() game=Snake()