Receiving ValueError: invalid recstyle object

Receiving ValueError: invalid recstyle object

我正在按照 youtube 上的一个 pygame 项目的说明进行操作,this is the video,我正在进行名为“Snake”的项目,在视频的描述中,您可以找到什么时间它开始和实际代码。我在视频中大约 10 分钟。我将向您展示到目前为止我编写的代码:

# Snake project on python

import math
import random
import pygame
import tkinter as tk
from tkinter import messagebox

class cube(object):
    rows = 0
    w = 0
    def __init__(self, start,dirnx=1, dirny=0, colour=(255,0,0)):
        pass

    def move(self, dirnx, dirny):
        pass

    def draw(self, surface, eyes=False):
        pass

class snake(object):
    def __init__(self, color, pos):
        pass

    def move(self):
        pass

    def reset(self, pas):
        pass

    def addCube(self):
        pass

    def draw(self, surface):
        pass

def drawGrid(w, rows, surface):
    sizeBtwn = w // rows

    x = 0
    y = 0
    for l in range(rows):
        x = x + sizeBtwn
        y = y + sizeBtwn

        pygame.draw.line(surface, (255,255,255), (x,0), (x,w))
        pygame.draw.line(surface, (255,255,255), (0,y), (w,y))

def redrawWindow(surface):
    global rows, width
    surface.fill(0,0,0)
    drawGrid(width, rows, surface)
    pygame.display.update()

def randomSnack(rows, items):
    pass

def message_box(subject, content):
    pass

def main():
    global width, rows
    width = 500
    rows = 20
    win = pygame.display.set_mode((width, width))
    s = snake((255, 0, 0), (10, 10))
    flag = True

    clock = pygame.time.Clock()

    while flag:
        pygame.time.delay(50)
        clock.tick(10)
        redrawWindow(win)


main()

当我 运行 代码时,教程说我应该得到一个网格(如视频的 55:32 所示)。相反,我收到了这条消息:

Windows PowerShell

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\holca\Desktop\Snake> & C:/Users/holca/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/holca/Desktop/Snake/snake.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "c:/Users/holca/Desktop/Snake/snake.py", line 77, in <module>
    main()
  File "c:/Users/holca/Desktop/Snake/snake.py", line 74, in main
    redrawWindow(win)
  File "c:/Users/holca/Desktop/Snake/snake.py", line 51, in redrawWindow
    surface.fill(0,0,0)
ValueError: invalid rectstyle object
PS C:\Users\holca\Desktop\Snake> 

我正在使用 VSCode,我有 2 条警告消息谈论一些未使用的变量,但我怀疑他们必须对问题采取任何措施。我错过了什么吗?

您必须通过元组指定颜色:

surface.fill(0,0,0)

surface.fill( (0, 0, 0) )

解释:

fill()的第一个参数是颜色(元组)。其他参数是可选的。第二个参数是一个可选的矩形,指定要填充的区域。第三个参数是可选的标志。

因此指令surface.fill(0,0,0)可以读作:

surface.fill(0, rect=0, special_flags=0)

第二个参数 (rect=0) 导致错误

ValueError: invalid rectstyle object