Pygame window will not open - python

I'm completely new to Python and I just started learning Pygame. I'm trying to work with drawing shapes right now, but I can't get the window to open. Idk what the problem is, here's my code:
`#import libraries
import pygame
import math
#init pygame
pygame.init()
#clock
clock = pygame.time.Clock()
#window screen
screen_height_x = 500
screen_height_y = 400
screen = pygame.display.set_mode((screen_height_x, screen_height_y))
#RGBA colors
Black = (0, 0, 0)
White = (255, 255, 255)
Blue = (0, 0, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
#program
running = True
while running:
for event in pygame.event.get():
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()`

You're immediately quitting the main loop and you also aren't drawing anything. Try this instead:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()
Disclaimer: I cannot try this out at the moment but it should get you started in the right direction.

Is this a class part of the projet or the main class ? If it is the main class, you it should contain a main function.
If this is the main function, you should declare it by adding def main(): before the pygame.init()
And add the module to exectute this as the main script under Quit
if __name__ == "__main__":
main()
Sorry, im french my english is not the best aha !

Related

PyGame background color is not changing

I cannot find, what is wrong with background, even with debugger it is not doing nothing
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start
if __name__ == "__main__":
main()
You have to call draw_window in the application loop:
#importing
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.quit()
#game start
if __name__ == "__main__":
main()
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()
When you write a function, it is just some instructions on memory, it doesn't run unless you call it. This is the case. You write a set of instructions, draw_window, but you never call it. What you need to do is before the game loop, call draw_window. So the code would be something like this.
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
draw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start :)
if __name__ == "__main__":
main()
This way the result will be a red window which is rgb(0, 0).

Why won't MOUSEBUTTONDOWN work in Pygame?

I an having an error with MOUSEBUTTONDOWN giving an error when used.
Here is the error:
if event.type == MOUSEBUTTONDOWN:
NameError: name 'MOUSEBUTTONDOWN' is not defined
Here is the code that is giving the error:
import pygame
import random
import sys
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(WHITE)
# --- Drawing code should go here
monospace = pygame.font.SysFont("Berlin Sans FB", 169)
label = monospace.render("test", 1, (0, 0, 0))
screen.blit(label, (100, 100))
button_rect = pygame.Rect(0, 0, 50, 50) # start button rectangle
abort = False
start = False
while not abort and not start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
abort = True
if event.type == MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
start = True
# draw title screen
# [...]
done = abort
while not done:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
All the other sources on Stack Overflow about this topic that I can find all talk about errors where MOUSEBUTTONDOWN already works.
Also note that I am using Pycharm.
Either pygame.MOUSEBUTTONDOWN instead of MOUSEBUTTONDOWN or from pygame.locals import *
You need only one application loop:
import pygame
from pygame.locals import *
import random
import sys
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("game")
clock = pygame.time.Clock()
monospace = pygame.font.SysFont("Berlin Sans FB", 169)
label = monospace.render("test", 1, (0, 0, 0))
button_rect = pygame.Rect(0, 0, 50, 50) # start button rectangle
abort = False
start = False
while not abort:
for event in pygame.event.get():
if event.type == pygame.QUIT:
abort = True
if event.type == MOUSEBUTTONDOWN:
if not start and button_rect.collidepoint(event.pos):
start = True
screen.fill(WHITE)
if not start:
# draw title screen
pygame.draw.rect(screen, "red", button_rect)
else:
# draw game
# [...]
screen.blit(label, (100, 100))
pygame.display.flip()
clock.tick(60)
pygame.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()
Solution found:
I did not have the needed libraries imported:
For MOUSEBUTTONDOWN to work you need to use:
from pygame.locals import *
Credit to #Rabbid76 for answering in comments.

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

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 script freezing on windows (Program not responding)

I am new to stackoverflow, I decided to join because I sometimes have problems with programming. This one is really annoying, I cant figure out why it doesn't work. Any help would be appreciated!
I get the windows "Program not responding" error message
A simple display:
import pygame
pygame.init()
BLUE = pygame.Color(0, 0, 255)
size = [1280, 720]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hangman")
done = False
clock = pygame.time.Clock()
while not done:
# Leaves the fps at 30
clock.tick(30)
screen.fill(BLUE)
pygame.display.flip()
The expected result is a blue screen, instead I get a blue screen that crashes
In your game loop handling events will prevent freezing.
import pygame
pygame.init()
BLUE = pygame.Color(0, 0, 255)
size = [1280, 720]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hangman")
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
else:
print event
# Leaves the fps at 30
clock.tick(30)
screen.fill(BLUE)
pygame.display.flip()

Categories

Resources