接收值错误:无效的 recstyle 对象 [英] Receiving ValueError: invalid recstyle object

查看:56
本文介绍了接收值错误:无效的 recstyle 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 youtube 上一个 pygame 项目的说明进行操作,这是视频,我在做一个叫做Snake"的项目,在视频的描述中,你可以找到它开始的时间和实际代码.我大约 10 分钟进入视频.我将向您展示我到目前为止编写的代码:

I am following the instructions of a pygame project on youtube, this is the video, I am on the project called "Snake", and in the description of the video, you can find what time it starts and the actual code. I am about 10 minutes into the video. I will show you the code that I have written so far:

# 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 中).相反,我收到了这条消息:

When I run the code the tutorial says I should be getting a grid (shown in 55:32 of the video). Instead, I am receiving this message:

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 条警告消息涉及一些未使用的变量,但我怀疑他们是否必须对问题采取任何措施.我错过了什么吗?

I am using VSCode, and I have 2 warning messages talking about some unused variables, but I doubt they have to do anything with the problem. Am I missing something?

推荐答案

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

You have to specify the color by a tuple:

surface.fill(0,0,0)

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

说明:

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

The first argument of fill() is the color (tuple). The other arguments are optional. The 2nd argument is an optional rectangle specifying area to be filled. The 3rd arguments are optional flags.

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

Hence the instruction surface.fill(0,0,0) can be read as:

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

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

The 2nd argument (rect=0) causes the error

ValueError: 无效的 rectstyle 对象

ValueError: invalid rectstyle object

这篇关于接收值错误:无效的 recstyle 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆