为什么我的精灵没有在 Pygame 中绘制到屏幕上

Why Are My Sprites Not Drawing To The Screen In Pygame

我最近一直在尝试在 python 中制作贪吃蛇游戏。该程序完全没有显示任何错误,但是当我 运行 程序时精灵不绘制。我试过运行 Python 中的程序而不是Visual Studio 中的代码,我尝试更改变量的名称,我也多次审查了代码。请帮助。

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()

亲爱的程序员 我认为你的代码中的错误是你定义了绘制蛇和苹果的函数但是你忘了调用它们所以你只需要将这段代码添加到主循环并且它会 运行 fine

the_snake.draw(surface)
food.draw(surface)