summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-11-01 10:12:43 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-11-01 10:12:43 +0100
commitf65580a13d7f6040f60ff102f9b90726c393ff85 (patch)
treeb86250230ac3a406f7beb79b98a12bbbbc2dbc96
parentf2876a26f8e8d1a00245177cbd903f98834466df (diff)
Minor changes
-rwxr-xr-xsnake/snake.py47
1 files changed, 38 insertions, 9 deletions
diff --git a/snake/snake.py b/snake/snake.py
index 40bbdcc..0842714 100755
--- a/snake/snake.py
+++ b/snake/snake.py
@@ -3,8 +3,12 @@ import sys, pygame, random
class Snake:
+ """
+ Programmable Game of Snake written in PyGame
+ """
- def __init__(self, margin=80,length=10,grid_width=40,grid_height=40, grid_pts=20,fps=10):
+ def __init__(self, margin=80,length=3,grid_width=30,grid_height=30, grid_pts=30,fps=10):
+ # Init attributes
self.grid_width=grid_width
self.grid_height=grid_height
self.grid_pts=grid_pts
@@ -12,12 +16,16 @@ class Snake:
self.default_length=length
self.attempt=0
self.fps=fps
+ # Setup pygame
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 new_game(self):
+ """
+ Reset game state
+ """
self.snake=[(0,0)]*self.default_length
self.direction=3 # Like clock (12=up, 3=right, 6=bottom, 9=left)
self.new_apple()
@@ -25,25 +33,36 @@ class Snake:
self.attempt+=1
def draw_pts(self,x,y,color=(255,255,255)):
+ """
+ Draw element on the snake area
+ """
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):
+ def draw_infos(self,color=(255,255,255),thickness=10):
+ """
+ Draw the information bar
+ """
+ # Draw separator
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)
+ pygame.draw.rect(self.screen,(180,180,180),rect, 0)
+ # Draw score
+ text = self.font.render('Score: '+str(self.score)+" Length: "+str(len(self.snake))+' 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, 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):
+ """
+ Create a new apple
+ """
self.apple=(random.randint(0,self.grid_width-1),random.randint(0,self.grid_height-1))
while self.apple in self.snake:
self.apple=(random.randint(0,self.grid_width),random.randint(0,self.grid_height))
def move(self):
+ """
+ Move the snake
+ """
# Update tail
if len(self.snake)>1:
tmp=self.snake[0]
@@ -63,12 +82,18 @@ class Snake:
self.snake[0]=(h[0],h[1]+1)
def draw_snake(self):
+ """
+ Draw the snake with color effects
+ """
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):
+ """
+ Return true if the game is loose
+ """
if self.snake.count(self.snake[0])>1:
return(True)
h=self.snake[0]
@@ -78,13 +103,16 @@ class Snake:
def run(self, event_handler=None):
+ """
+ Play a game (one attempt)
+ """
clock = pygame.time.Clock()
ignore_has_loose=True
self.new_game()
while True:
self.screen.fill((0,0,0))
self.draw_snake()
- self.draw_pts(self.apple[0],self.apple[1],color=(255,0,0))
+ self.draw_pts(self.apple[0],self.apple[1],color=(210,0,0))
self.draw_infos()
# Check for loose
if not(ignore_has_loose) and self.has_loose():
@@ -109,6 +137,7 @@ class Snake:
elif event.key == pygame.K_DOWN and self.direction != 12:
self.direction=6
break
+ # Check if an event handler is available
if event_handler!=None:
event_handler(self)
self.move()