Minor changes
This commit is contained in:
parent
da1e1dd2c0
commit
73bd3decdc
1 changed files with 13 additions and 5 deletions
|
@ -4,7 +4,7 @@ import sys, pygame, random
|
|||
|
||||
class Snake:
|
||||
|
||||
def __init__(self, margin=80,length=10,grid_width=50,grid_height=50, grid_pts=15,fps=10):
|
||||
def __init__(self, margin=80,length=10,grid_width=40,grid_height=40, grid_pts=20,fps=10):
|
||||
self.grid_width=grid_width
|
||||
self.grid_height=grid_height
|
||||
self.grid_pts=grid_pts
|
||||
|
@ -13,8 +13,10 @@ class Snake:
|
|||
self.snake=[(0,0)]*length
|
||||
self.direction=3 # Like clock (12=up, 3=right, 6=bottom, 9=left)
|
||||
self.new_apple()
|
||||
self.attempt=0
|
||||
pygame.init()
|
||||
self.font=pygame.font.SysFont(pygame.font.get_default_font(), int(self.margin/2))
|
||||
self.font_small=pygame.font.SysFont(pygame.font.get_default_font(), int(self.margin/2.5))
|
||||
self.screen=pygame.display.set_mode((grid_width*grid_pts,grid_height*grid_pts+margin))
|
||||
|
||||
def draw_pts(self,x,y,color=(255,255,255)):
|
||||
|
@ -25,8 +27,11 @@ class Snake:
|
|||
rect=pygame.Rect(0, self.margin-thickness, self.grid_width*self.grid_pts, thickness)
|
||||
pygame.draw.rect(self.screen,color,rect, 0)
|
||||
text = self.font.render('Score '+str(self.score)+" Length "+str(len(self.snake)), True, color)
|
||||
text_center=text.get_rect(center = (self.grid_width*self.grid_pts // 2, self.margin // 2))
|
||||
text_center=text.get_rect(center = (self.grid_width*self.grid_pts // 2, (self.margin-thickness) // 2))
|
||||
self.screen.blit(text, text_center)
|
||||
text = self.font_small.render('Attempt '+str(self.attempt), True, color)
|
||||
text_center=text.get_rect(center = (self.grid_width*self.grid_pts // 2, (self.margin-thickness) // 2))
|
||||
self.screen.blit(text, (self.grid_pts/2,text_center[1]))
|
||||
|
||||
def new_apple(self):
|
||||
self.apple=(random.randint(0,self.grid_width),random.randint(0,self.grid_height))
|
||||
|
@ -53,8 +58,10 @@ class Snake:
|
|||
self.snake[0]=(h[0],h[1]+1)
|
||||
|
||||
def draw_snake(self):
|
||||
for elt in self.snake:
|
||||
self.draw_pts(elt[0],elt[1])
|
||||
for i in range(0,len(self.snake)):
|
||||
color=(0,150,150) if i==0 else (0,max(255-i*10,120),0)
|
||||
elt=self.snake[i]
|
||||
self.draw_pts(elt[0],elt[1],color=color)
|
||||
|
||||
def has_loose(self):
|
||||
if self.snake.count(self.snake[0])>1:
|
||||
|
@ -69,6 +76,7 @@ class Snake:
|
|||
self.direction=6
|
||||
ignore_has_loose=True
|
||||
self.score=0
|
||||
self.attempt+=1
|
||||
while True:
|
||||
self.screen.fill((0,0,0))
|
||||
self.draw_snake()
|
||||
|
@ -108,4 +116,4 @@ class Snake:
|
|||
|
||||
|
||||
game=Snake()
|
||||
game.run()
|
||||
game.run()
|
||||
|
|
Loading…
Add table
Reference in a new issue