Minor changes
This commit is contained in:
parent
711ad16ae4
commit
cee495b600
1 changed files with 23 additions and 7 deletions
|
@ -4,7 +4,7 @@ import sys, pygame
|
||||||
|
|
||||||
class Snake:
|
class Snake:
|
||||||
|
|
||||||
def __init__(self, length=10,grid_width=50,grid_height=50, grid_pts=15,fps=2):
|
def __init__(self, length=10,grid_width=50,grid_height=50, grid_pts=15,fps=10):
|
||||||
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
|
||||||
|
@ -19,17 +19,23 @@ class Snake:
|
||||||
pygame.draw.rect(self.screen,color,rect, 0)
|
pygame.draw.rect(self.screen,color,rect, 0)
|
||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
head=self.snake[0]
|
# Update tail
|
||||||
if self.direction==3:
|
|
||||||
head=(head[0]+1,head[1])
|
|
||||||
if len(self.snake)>1:
|
if len(self.snake)>1:
|
||||||
tmp=head
|
tmp=self.snake[0]
|
||||||
for i in range(1,len(self.snake)):
|
for i in range(1,len(self.snake)):
|
||||||
newtmp=self.snake[i]
|
newtmp=self.snake[i]
|
||||||
self.snake[i]=tmp
|
self.snake[i]=tmp
|
||||||
tmp=newtmp
|
tmp=newtmp
|
||||||
self.snake[0]=head
|
# 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)
|
||||||
|
|
||||||
def draw_snake(self):
|
def draw_snake(self):
|
||||||
for elt in self.snake:
|
for elt in self.snake:
|
||||||
|
@ -37,11 +43,21 @@ class Snake:
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
self.direction=6
|
||||||
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:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
elif event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_LEFT and self.direction != 3:
|
||||||
|
self.direction=9
|
||||||
|
if event.key == pygame.K_RIGHT and self.direction != 9:
|
||||||
|
self.direction=3
|
||||||
|
if event.key == pygame.K_UP and self.direction != 6:
|
||||||
|
self.direction=12
|
||||||
|
if event.key == pygame.K_DOWN and self.direction != 12:
|
||||||
|
self.direction=6
|
||||||
|
|
||||||
self.screen.fill((0,0,0))
|
self.screen.fill((0,0,0))
|
||||||
self.draw_snake()
|
self.draw_snake()
|
||||||
|
|
Loading…
Add table
Reference in a new issue