How to make my pygame game window resizeable? - python

When I run this code the resizeable option is available but when I click on it to maximize the screen, there is this weird white outline that appears. How would I fix it?.
import pygame
pygame.init()
screen = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()

You have to recreate the display Surface in the VIDEORESIZE event (see pygame.event):
import pygame
pygame.init()
screen = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.VIDEORESIZE:
screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
pygame.display.update()

Related

the background color of my program is only visable when I close the app icon

I have an app icon open but I can' see a white background in my application window untill I close the app. What do I do?
import pygame
# Internationalizing Pygame
pygame.init()
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load()
pygame.display.set_icon(icon)
# player
playerIMG = pygame.image.load()
playerX = 370
playerY = 30
screen = pygame.display.set_mode((1200, 600))
def player():
screen.blit(playerIMG, (playerX, playerY))
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
player()
pygame.display.update()
I don't know what to do.
It is a matter of Indentation you have to draw the scene and update the display in the application loop:
running = True
while running: # RGB
screen.fill((222, 222, 222))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
player()
pygame.display.update()

Pygame - window opens but fill() doesn't work and no events detected

I'm creating a simple pygame program (PyGame 1.9.6 on Python 3.7), but some of the code inside my while loop doesn't seem to work. When I run the program, the window opens, but the screen doesn't fill with black, nor does the window close when I press "x"
import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
In the console, "tick" appears like normal and "event detected" appears after pressing any keys or mouse click. I don't get any errors when I run it.
If event == pygame.QUIT: should be if event.type == pygame.QUIT:
I usually use that event like this:
if event.type == pygame.QUIT:
pygame.quit()
quit()
Just as #Telan said
if event==pygame.QUIT:
should be
if event.type==pygame.QUIT:
However to properly shutdown pygame, pygame.quit() is important to shutdown pygame modules which is the reverse of pygame.init()
While sys.exit() is used to properly shutdown the main python program.
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
Full code is shown below. Enjoy!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()

Pygame Window not Responding but no Error

I recently started learning pygame but I've had an error which shows pygame not responding after some time. It would really help if someone could find my bug! Here's my code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen,BLUE,(200,150,100,50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
From my limited knowledge of pygame, it is likely that you set running = True and did a while True: statement, causing too much memory to be used and the script to crash after some time.
You need a proper termination that uses pygame.quit(), followed by sys.exit(). Setting running = False does not terminate the script.
Try this code:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
sys.exit()
Reference
Try adding a pygame.quit() at the end of your code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen, BLUE, (200, 150, 100, 50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
pygame.quit()

video system not initialized

When I run the following code I get an error. Most of the other similar posts talks about pygame.quit()
sys.exit() which I have, but still see the error
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
is_playing= True
while is_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
pygame.quit()
sys.exit()
The error is
Traceback (most recent call last):
File "C:/Users/.............................", line 10, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
pygame.quit() should be out of the loop:
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
is_playing= True
while is_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
pygame.quit()
sys.exit()
Otherwise after the first iteration of the loop, pygame.quit() would have been called and on the second iteration the error would have occured.
You have to care about the Indentation
While pygame.quit() has to be done after the main application loop (as mentioned in another answer and the comments), drawing the scene and the update of the display has to be done in the main application loop rather than the event loop:
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
# application loop
is_playing= True
while is_playing:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
#<--| INDENTATION
# draw the scene
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
#<--| INDENTATION
# quit pygame
pygame.quit()
sys.exit()
Note, the event loop is executed once for each event, but the application loop is executed once for each frame.

pygame error - the code is not working

This code is not working for me:
import pygame
pygame.init()
red = (255,0,0)
gamedisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("Snake")
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gamedisplay.fill(red)
pygame.display.update
pygame.quit()
quit
It doesn't give me a red background.
You are not updating the screen. Fix this line:
pygame.display.update
to
pygame.display.update()

Categories

Resources