Home python python. Snake game on Pygame

python. Snake game on Pygame

Author

Date

Category

decided to write the game “Snake” on Pygame through sprites (probably was a stupid idea). Initially, the snake has only a head. And when she eats an apple, the size of the snake is updated to a split second, and then one head will be visible again. I can also stumble upon a “invisible” tail and the game ends.
Here is the code:

#! usr / bin / python3
Import pygame.
Import Random
Width = 540.
Height = 740.
FPS = 2.
Size_Block = 20.
White = (255, 255, 255)
Black = (0, 0, 0)
RED = (224, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
pygame.init ()
pygame.mixer.init ()
screen = pygame.display.set_mode ((Width, Height))
pygame.display.set_caption ("Ping Pong")
Clock = pygame.time.clock ()
Class Snake (pygame.sprite.sprite):
  Def __init __ (Self):
    Pygame.Sprite.Sprite .__ INIT __ (SELF)
    self.image = pygame.surface ((Size_block, size_block))
    Self.Image.fill (Green)
    self.Rect = Self.Image.get_rect ()
    Self.Rect.center = [Width / 2, Height / 2]
    Self.speedx = 20.
    Self.Speedy = 0.
  DEF UPDATE (SELF):
    self.Rect.x + = Self.Speedx
    self.Rect.y + = Self.Speedy
  Def Tail (SELF, Size_Block, Snake_List):
    For x in snake_list:
      pygame.draw.Rect (Screen, Green, [x [0], x [1], Size_Block, Size_Block])
Class Apple (pygame.sprite.sprite):
  Def __init __ (Self):
    Pygame.Sprite.Sprite .__ INIT __ (SELF)
    self.image = pygame.surface ((Size_block, size_block))
    Self.Image.fill (Red)
    self.Rect = Self.Image.get_rect ()
    Self.foodx = Round (Random.randrange (0, Width - Size_Block) / 20.0) * 20.0
    Self.Foody = Round (Random.randrange (0, Height - Size_Block) / 20.0) * 20.0
  DEF UPDATE (SELF):
    Self.Rect.x = Self.foodx
    self.Rect.y = Self.foody
All_sprites = pygame.sprite.group ()
apples = pygame.sprite.group ()
Apple = Apple ()
Snake = Snake ()
All_sprites.add (Snake)
All_sprites.add (Apple)
APPES.ADD (Apple)
snake_list = []
LEN_OF_SNAKE = 1.
Running = True.
While Running:
  Clock.tick (FPS)
  FOR EVENT IN PYGAME.EVENT.GET ():
    If event.type == pygame.quit:
      Running = False.
    If event.Type == Pygame.Keydown:
      If event.key == pygame.k_a and snake.speedy! = 0:
        snake.speedx = -size_block
        snake.speedy = 0.
      If event.key == pygame.k_d and snake.speedy! = 0:
        snake.speedx = size_block
        snake.speedy = 0.
      If event.key == pygame.k_w and snake.speedx! = 0:
        snake.speedx = 0.
        snake.speedy = -size_block
      If event.key == pygame.k_s and snake.speedx! = 0:
        snake.speedx = 0.
        snake.speedy = size_block
  Screen.fill (Black)
  All_sprites.DRAW (Screen)
  if snake.Rect.left & lt; -ten:
    Running = False.
  IF snake.Rect.right & gt; Width + 10:
    Running = False.
  IF snake.Rect.top & lt; = -10:
    Running = False.
  If snake.Rect.bottom & gt; Height + 10:
    Running = False.
  snake_head = [snake.rect.x, snake.rect.y]
  Snake_List.APPEND (Snake_head)
  IF LEN (Snake_List) & GT; LEN_OF_SNAKE:
    Del Snake_List [0]
  For x in snake_list [: - 1]:
    if x == snake_head:
      Running = False.
  Collide = Pygame.Sprite.SpriteCollide (Snake, Apples, True)
  If Collide:
    snake.tail (Size_Block, Snake_List)
    Apple = Apple ()
    All_sprites.add (Apple)
    APPES.ADD (Apple)
    LEN_OF_SNAKE + = 1
    pygame.display.update ()
  pygame.display.flip ()
  snake.update ()
  apple.update ()
pygame.quit ()

I will be pretty grateful if you tell me how this jamb can be fixed.


Answer 1, Authority 100%

In the code when a collision is added by the Tail of the Snake:

if collide:
  snake.tail (Size_Block, Snake_List)

In the Snake class, the snake tail is registered as a drawing of the unit:

class snake (pygame.sprite.sprite): 
Def Tail (SELF, Size_Block, Snake_List):
     For x in snake_list:
       pygame.draw.Rect (Screen, Green, [x [0], x [1], Size_Block, Size_Block])

In the game cycle, the screen is flooded with black, then the sprites are drawing:

screen.fill (black)
All_sprites.DRAW (Screen)

Since the tail is not a sprite, it is simply not drawn. I see here two options: either add a tail in sprites, or separately draw the tail after the fill of the screen is black.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions