Python 接收值错误:recstyle对象无效

Python 接收值错误:recstyle对象无效,python,tkinter,pygame,valueerror,Python,Tkinter,Pygame,Valueerror,我正在按照youtube上pygame项目的说明进行操作,我正在进行一个名为“Snake”的项目,在视频的描述中,您可以找到它的开始时间和实际代码。我大概看了10分钟的视频。我将向您展示到目前为止我编写的代码: # Snake project on python import math import random import pygame import tkinter as tk from tkinter import messagebox class cube(object):

我正在按照youtube上pygame项目的说明进行操作,我正在进行一个名为“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> 
Windows PowerShell
尝试新的跨平台PowerShellhttps://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
PyGame1.9.6
大家好,来自pygame社区。https://www.pygame.org/contribute.html
回溯(最近一次呼叫最后一次):
文件“c:/Users/holca/Desktop/Snake/Snake.py”,第77行,在
main()
文件“c:/Users/holca/Desktop/Snake/Snake.py”,第74行,主
重画窗口(win)
文件“c:/Users/holca/Desktop/Snake/Snake.py”,第51行,在重画窗口中
表面填充(0,0,0)
ValueError:rectstyle对象无效
PS C:\Users\holca\Desktop\Snake>

我正在使用VSCode,我有两条警告消息谈到一些未使用的变量,但我怀疑它们与这个问题有任何关系。我遗漏了什么吗?

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

表面填充(0,0,0)

surface.fill((0,0,0))
说明:

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

因此,说明
surface.fill(0,0,0)
可以理解为:

surface.fill(0,rect=0,特殊标志=0)
第二个参数(
rect=0
)导致错误

ValueError:rectstyle对象无效


我现在明白了,非常感谢你,我的朋友!它正按照修正后需要的方式运行。