Pygame Rectangle Doesn't Appear Unless I Tab-out and Back In - python

I've got this code:
import pygame, sys
from pygame.locals import *
pygame.init()
WHITE = (255,255,255)
class setup():
def set_resolution(x, y):
global surf
global screen
surf = pygame.Surface((x,y))
screen = pygame.display.set_mode((x,y))
pygame.display.set_caption("Lewis' Game")
pygame.draw.rect(screen, WHITE,(200,150,150,50))
def menu():
pass
setup.set_resolution(1024,768)
setup.menu()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
For some reason, the pygame.draw.rect(screen, WHITE,(200,150,150,50)) at the end of the setup function doesn't actually appear unless I show the desktop and them tab back in. I'm not sure why this is happening as it's never happened before. I should mention that I'm still learning pygame, and this code is just for practice.
Many thanks in advance.

change your while loop to include this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
pygame.display.flip() # pygame.display.update works as well
if you want more on the difference of these, see Difference between pygame.display.update and pygame.display.flip

Related

Keep getting an error about pygame not being initialised

I keep getting an error about system not being initialised. The error is about the pygame.display.update()
Where is this meant to go?
import pygame #IMPORTS THE PYGAME CLASSES, METHODS AND ATTRIBUTES
from pygame.locals import * #IMPORTING ALL PYGAME MODULES
pygame.init() #INITIALISING PYGAME
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("On The Run")
blue = 146,244,255 #BACKGROUND COLOUR
width = 80
height = 60
x = 200 #X-POSITION OF THE CHARACTER
y = 100 #Y-POSITION OF THE CHARACTER
player1 = pygame.image.load('assets/characterMove3.jpg') #DISPLAYING THE IMAGE ONTO THE SCREEN
player1 = pygame.transform.scale(player1,(width,height)) #SCALING THE IMAGE TO SUITABLE DIMENSIONS
WINDOW.blit(player1,(x,y))
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False #WE WILL QUIT THE GAME AS THE VARIABLE run IS NOW FALSE
pygame.quit() #IT WON'T SHOW THE MOST RECENT THING I DREW UNLESS I MANUALLY UPDATE IT
pygame.display.update()
game_loop()
The problem is that pygame.quit() is called in the application loop. pygame.quit() deinitializes all Pygame modules and crashes all subsequent Pygame API calls. pygame.quit() must be the very last Pygame API call. Call pygame.quit() after the application loop:
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pygame.quit() <-- DELETE
pygame.display.update()
pygame.quit() # <-- INSERT
game_loop()

Why Pygame window closes instantly?

I am trying to follow a pygame tutorial and the pygame window keeps closing instantly after opening. I have tried multiple different snippets of code from various places and none seem to keep the window open. i am using VScode if that makes a difference,
import pygame
pygame.init()
WIDTH, HEIGHT= 900,500
WIN=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Rock Paper Scissors")
def main():
gameRunning = True
while gameRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("QUIT")
pygame.quit()
gameRunning = False
quit()
WIN.fill(0,0,0)
pygame.display.update()
if __name__=="__main__":
main()
pygame.Surface.fill has just a single argument, which is either a RGB sequence, a RGBA sequence or a mapped color index. e.g. a tuple with the RGB color components:
WIN.fill(0,0,0)
WIN.fill( (0,0,0) )

Import loop only works twice, gives pygame.error Display surface quit

I am in a beginner python class and we are working on our final projects. For my game however I wanted to be able to restart the game within itself. I did this by using imports to launch a new script to then re-run my game. However whenever I try to do it more than once my window breaks, or the code does not import the next file. My game is kinda long so I've created a very short program to give an example of my issue.
(This being the file yin.py)
import pygame
BLACK = (0, 0, 0)
pygame.init()
SIZE = [300, 300]
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
import yang
done = True
screen.fill(BLACK)
pygame.display.flip()
clock.tick(30)
pygame.quit()
(and this file yang.py)
import pygame
WHITE = (255, 255, 255)
pygame.init()
SIZE = [300, 300]
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
import yin
done = True
screen.fill(WHITE)
pygame.display.flip()
clock.tick(30)
pygame.quit()
I've tried doing multiple things but so far I have gotten nowhere, it still is giving me display not initialized even though I have 'pygame.init()'. My teacher isn't responding to my emails and I have an encroaching deadline. Any help will be appreciated. :p
The "import" statement only imports once. If the name already exists, it doesn't import again. You should put your code into a function. Then, you can call the function as many times as you want.

How do you switch between multiple backgrounds in pygame?

I was practicing using the pygame package in python by making a graphics oriented game and in my game, I want to switch between multiple backgrounds as the user progresses through it. For the example I am posting, the first background will be a black screen with 5 statements on it and my second background will be a room. However, I am unable to figure this out because whenever I try to run the code, the game window doesn't show anything. I can post my code for the while loop below for further clarification.
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
first_line()
second_line()
third_line_1()
third_line_2()
fourth_line()
fifth_line()
sleep(10)
screen.blit(background_room, (0, 0))
pygame.display.update()
The way I would probably do it is something like
import pygame
from pygame.locals import *
class Backround:
FIVE_STATEMENTS = 0
ROOM = 1
viewport_size = (800, 600)
def main():
pygame.init()
screen = pygame.display.set_mode(viewport_size)
backround_room = pygame.Surface(viewport_size)
# load the image...
backround_statements = pygame.Surface(viewport_size)
backround_statements.fill((0, 0, 0))
# blit the five statements onto here
backround_to_render = Backround.FIVE_STATEMENTS
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if backround_to_render == Backround.FIVE_STATEMENTS:
# if the statements ever change, probably want to fill() and re-blit them on
screen.blit(backround_statements, (0,0))
elif backround_to_render == Backround.ROOM:
screen.blit(backround_room, (0,0))
else:
print("Oh no!!!!!!!")
pygame.display.flip()
if __name__ == "__main__":
main()

Pygame Accessing variables in functions

Hey I've been trying to call the pygame events in a function to reduce the clutter of my code. But I am unable to perform the process due to an error with global variables. Any help would be appreciated.
from pygame.locals import *
import pygame, sys
def color():
screen.fill((85,163,48))
def events():
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
print "left"
#sets the game map variables
tilesize = 100
map_width = 600
map_height = 300
#initialises the game screen setting it up with the game map variables
pygame.init()
screen = pygame.display.set_mode((map_width,map_height))
while True:
color()
events()
pygame.display.update()
If you're unable to access a value locally from within a function, the answer is to simply pass the value into the function. Something simple like this should suffice:
def events(ev_list):
for event in ev_list:
if event.type == QUIT:
pygame.quit()
within your game loop you'd call it with:
event_list = pygame.event.get()
events(event_list)
alternatively (less desirable) you can import a library from within the function or declare a variable global

Categories

Resources