Home > database >  Why Are My Sprites Not Drawing To The Screen In Pygame
Why Are My Sprites Not Drawing To The Screen In Pygame

Time:01-26

I have recently been trying to make a Snake game in python. The program shows no errors at all, yet the sprites do not draw when I run the program. I've tried to run the program in Python instead on in Visual Studio Code, I tried to change the name of Variables, I have reviewed the code many times as well. Please Help.

import pygame
import sys
import random

#Snake
class snake(object):

    def __init__(self):
        self.length = 1
        self.positions = [((Screen_Width / 2), (Screen_Height / 2))]
        self.direction = random.choice([Up, Down, Left, Right])
        self.color = (0, 0, 0)

    def head_position(self):
        return self.positions[0]

    def turn(self,point):
        if self.length > 1 and (point[0] * -1, point[1] * -1) == self.direction:
            return
        else:
            self.direction = point

    def move(self):
        cur = self.head_position()
        x, y = self.direction
        new = (((cur[0]   (x * Grid_Size)) % Screen_Width), (cur[1]   (y * Grid_Size)) % Screen_Height)
        if len(self.positions) >2 and new in self.positions[2:]:
            self.reset()
        else:
            self.positions.insert(0, new)
            if len(self.positions) > self.length:
                self.positions.pop()

    def reset(self):
        self.length = 1
        self.positions = [((Screen_Width / 2), (Screen_Height / 2))]
        self.direction = random.choice([Up, Down, Left, Right])

    def draw(self, surface):
        for p in self.positions:
            r = pygame.Rect((p[0], p[1]), (Grid_Size, Grid_Size))
            pygame.draw.rect(surface, self.color, r)
            pygame.draw.rect(surface, (93, 216, 228), r, 1)

    def handle_keys(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.turn(Up)
                elif event.key == pygame.K_DOWN:
                    self.turn(Down)
                elif event.key == pygame.K_LEFT:
                    self.turn(Left)
                if event.key == pygame.K_RIGHT:
                    self.turn(Right)


#Apple
class apple(object):
    
    def __init__(self):
        self.position = (0, 0)
        self.color = (192, 7, 7)
        self.randomize_position()

    def randomize_position(self):
        self.position = (random.randint(0, Grid_Width - 1) * Grid_Size, random.randint(0, Grid_Height -1) * Grid_Size)

    def draw(self, surface):
        r = pygame.Rect((self.position[0], self.position[1]), (Grid_Size, Grid_Size))
        pygame.draw.rect(surface, self.color, r)
        pygame.draw.rect(surface, (9, 196, 46), r, 1)

#Draw The Grid
def draw_grid(surface):
    for y in range(0, int(Grid_Height)):
        for x in range(0, int(Grid_Width)):
            if (x   y) % 2 == 0:
                r = pygame.Rect((x*Grid_Size, y*Grid_Size), (Grid_Size, Grid_Size))
                pygame.draw.rect(surface, (9, 196, 46), r)
            else:
                rr = pygame.Rect((x*Grid_Size, y*Grid_Size), (Grid_Size, Grid_Size))
                pygame.draw.rect(surface, (7, 138, 33), rr)


#Screen
Screen_Width = 500
Screen_Height = 500

Grid_Size = 20
Grid_Width = Screen_Height / Grid_Size
Grid_Height = Screen_Width / Grid_Size

#Movement
Up = (0, -1)
Down = (0, 1)
Left = (-1, 0)
Right = (1, 0)

#Main Game Loop
def Main():
    pygame.init()

    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((Screen_Width, Screen_Height), 0, 32)

    surface = pygame.Surface(screen.get_size())
    surface = surface.convert()
    draw_grid(surface)

    the_snake = snake()
    food = apple()

    my_font = pygame.font.SysFont("monospace", 16)

    score = 0

    #Handling Events.
    while (True):
        clock.tick(10)
        the_snake.handle_keys()
        draw_grid(surface)
        the_snake.move()
        if the_snake.head_position() == food.position:
            snake.length  = 1
            score  = 1
            food.randomize_position()
        screen.blit(surface, (0, 0))
        text = my_font.render("Score {0}".format(score), 1, (0, 0, 0))
        screen.blit(text, (5, 10))
        pygame.display.update()

#Call On The Main Function.
Main()

CodePudding user response:

dear programmer I think the error in your code is that you defined the function to draw the snake and the apple but you forgot to call them so you just need to add this code to the main loop and it will run fine

the_snake.draw(surface)
food.draw(surface)
  •  Tags:  
  • Related