Pygame ValueError: invalid rectstyle object - python

I downloaded a pygame example from here called rabbitone, and followed the corresponding youtube video.
So I have studied the code and tried it:
import pygame
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
player = pygame.image.load("resources/images/dude.png")
while True:
screen.fill(0,0,0)
pygame.display.flip()
screen.blit(player, (100,100))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
In the video tutorial I'm following, the code works. Why do I get this error?
Traceback (most recent call last):
File "", line 2, in
ValueError: invalid rectstyle object

You're passing three separate integers to the pygame.Surface.fill method, but you have to pass a color tuple (or list or pygame.Color object) as the first argument : screen.fill((0, 0, 0)).
You also need to blit the player between the fill and the flip call, otherwise you'll only see a black screen.
Unrelated to the problem, but you should usually convert your surfaces to improve the performance and add a pygame.time.Clock to limit the frame rate.
import pygame
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# Add a clock to limit the frame rate.
clock = pygame.time.Clock()
# Convert the image to improve the performance (convert or convert_alpha).
player = pygame.image.load("resources/images/dude.png").convert_alpha()
running = True
while running:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Insert the game logic here.
# Then draw everything, flip the display and call clock tick.
screen.fill((0, 0, 0))
screen.blit(player, (100, 100))
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.
pygame.quit()

I had the same issue but it was in a different situation.
I put surface.fill(0,0,0)
To fix this, I put surface.fill((0,0,0))
With a pair of extra brackets aroud the colour

Related

How to find the position of a Rect in python?

I was trying to make a code that moves a rect object (gotten with the get_rect function) but I need its coordinates to make it move 1 pixel away (if there are any other ways to do this, let me know.)
Here is the code:
import sys, pygame
pygame.init()
size = width, height = 1920, 1080
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("ball.png")
rectball = ball.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
rectball.move()
screen.fill(black)
screen.blit(ball, rectball)
pygame.display.flip()
You will notice that on line 11 the parameters are unfilled. This is because I was going to make it coords + 1.
If I am correct I think it is rect.left or rect.top that gives it. Another method is to create another rectangle and check if the rectangle is in that.

I am trying to insert a sprite image into pygame window and it wont pop up

(i followed a yt tut so idk) my code: idk if i put code in wrong place
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
player_sprite = pygame.image.load("will smith.png") .convert_alpha
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
pygame.display.update()
pygame.quit()
quit()
not really sure why it does this, im new to python so idk
There are multiple things that needs to fixed. First of all to you actually need to blit the image on the screen.
while not exit:
for event in pygame.event.get():
#...[block of code]
display.fill(white)
display.blit(player_sprite, (100,100)) # BLIT IMAGE TO SCREEN
.convert_alpha() is a method and you need to call it after you initialize pygame and set the video mode.
You can read more about convert_alpha() here
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
#converting it after init and setting game mode
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Here's the final working code
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# print(event)
display.fill(white)
display.blit(player_sprite, (100,100))
pygame.display.update()
pygame.quit()
quit()
You have to blit the image in the application loop, after clearing the display and before updating the display:
display.blit(player_sprite, (100, 100))
convert_alpha is function. You missed the parentheses
player_sprite = pygame.image.load("will smith.png").convert_alpha
player_sprite = pygame.image.load("will smith.png").convert_alpha()
Also, you cannot call a pygame function before you initialize pygame. pygame.init() should be the very first pygame function called.
Complete example:
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
display = pygame.display.set_mode((850,850))
pygame.display.set_caption('Conners Game')
player_sprite = pygame.image.load("will smith.png").convert_alpha()
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
print(event)
display.fill(white)
display.blit(player_sprite, (100, 100))
pygame.display.update()
pygame.quit()
quit()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()

Rectangle not being drawn [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 1 year ago.
When running this code:
import pygame,time
GREEN = (30, 156, 38)
WHITE = (255,255,255)
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
time.sleep(3)
Pygame shows a black screen for 3 seconds, but doesn't draw a rectangle.
I am using Atom using script package to run code
You have to implement an application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage
import pygame
GREEN = (30, 156, 38)
WHITE = (255,255,255)
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
# applicaition loop
run = True
while run:
# limit frames per second
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear display
screen.fill(WHITE)
# draw objects
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))
# update display
pygame.display.flip()
pygame.quit()
exit()
Note, you must do the event handling. See pygame.event.get() respectively pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
Update the screen with:
pygame.display.update()
at the end of your code you have posted.
You have to update the screen like that:
pygame.display.flip()
to render what you just drew.
Your code should look like this:
import pygame
import time
pygame.init()
GREEN = (30, 156, 38)
WHITE = (255,255,255)
screen = pygame.display.set_mode((640, 480))
# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0,0,100,100))
# show that to the user
pygame.display.flip()
time.sleep(3)
Off-topic: You should also get the events to allow the user to close the window:
import pygame
from pygame.locals import QUIT
import time
pygame.init()
GREEN = (30, 156, 38)
WHITE = (255, 255, 255)
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock() # to slow down the code to a given FPS
# draw on screen
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (0, 0, 100, 100))
# show that to the user
pygame.display.flip()
start_counter = time.time()
while time.time() - start_counter < 3: # wait for 3 seconds to elapse
for event in pygame.event.get(): # get the events
if event.type == QUIT: # if the user clicks "X"
exit() # quit pygame and exit the program
clock.tick(10) # limit to 10 FPS
# (no animation, you don't have to have a great framerate)
Note that you must put all of this into a game loop if you want to repeat it like a classic game.

pygame.error: video system not initialize, using Repl.it

I get this error when I try to run my pygame code pygame.error: video system not initialized. I'm using Repl.it and am attempting to create an aiming game which can track accuracy and in which you only have 3 lives.
import pygame
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # Here we exit the Loop and execute what after
pygame.quit()
# Play Surface
width = 1080
height = 720
playSurface = pygame.display.set_mode((width, height))
pygame.display.set_caption('Aim Practice')
# Colors
red = pygame.Color(0, 0, 0)
blue = pygame.Color(255, 255, 255)
Image of most of the code
https://repl.it/join/dppwnpin-isa__paz (You can view the full code here!)
The issue is that you event loop is running before anything is initialised. As #zenofpython says in his answer, the calls to prepare the window must come before the main event loop.
Your main event loop is first, and nothing is setup to run.
Just moving the code around will fix it:
import pygame
# FIRST, HANDLE ALL THE INITIALISATION OF PYGAME, FONTS, MIXER etc.
# Play Surface
width = 1080
height = 720
playSurface = pygame.display.set_mode((width, height))
pygame.display.set_caption('Aim Practice')
# Colors
red = pygame.Color(0, 0, 0)
blue = pygame.Color(255, 255, 255)
# ... AND THE REST
# MAIN LOOP
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # Here we exit the Loop and execute what after
playSurface.fill( blue ) # fill the screen
pygame.display.flip() # flush all the drawing operations to the window
fpsController.tick_busy_loop(60) # clamp the max-FPS
pygame.quit()
You should use pygame.display.set_mode before running your event loop. pygame.event.get won't work if you haven't created a window.

Python + Pygame; screen.fill() crashing window

Whenever I try to include "screen.fill(insert value here)" to my pygame code, the window instantly crashes. I can comment it out and my code works just fine. I'm really not sure why.
I've tried moving the line around, using other people's code in part - no luck.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
win.blit(img,(450, 150))
running = True
while running:
screen.fill(0, 0, 0)
pygame.display.update()
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
When I run the code with that line in it, the window opens and then it instantly closes.
If you are trying to fill the screen with black, the color must be passed as a tuple:
win.fill((0, 0, 0))
Also, you never assign screen, maybe you intended to use win?
The doc for Surface.fill.
The color parameter to fill() has to be either a single grayscale value or a tuple of 3 RGB or 4 RGBA components. So it has to be either:
win.fill(0)
or
win.fill((0, 0, 0))
Further you've to blit() the image in the main loop. To continuously draw the scene in the main application loop, you've to:
handle the events
clear the window
draw the scene (blit the image)
update the display
Furthermore I recommend to use tick() of pygame.time.Clock rather than pygame.time.delay() tpo control the frames per second.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
running = True
while running:
clock.tick(60)
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# clear the display
win.fill(0)
# draw the scene
win.blit(img,(450, 150))
# update the display
pygame.display.update()
pygame.quit()

Categories