Search code examples
pythontkinterpygamevalueerror

Receiving ValueError: invalid recstyle object


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

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> 

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?


Solution

  • You have to specify the color by a tuple:

    surface.fill(0,0,0)

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

    Explanation:

    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.

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

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

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

    ValueError: invalid rectstyle object