This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)
Related
This question already has answers here:
How to add a background image into pygame?
(3 answers)
How to scale an image by window size?
(1 answer)
Closed 3 months ago.
I'm trying to make some game similar to Space invaders but they come around from everywhere including sides and bottom and the player can only rotate the ship to shoot lasers at the enemies. Right now, I'm trying to add a background to it to give the game some atmosphere but whatever guides I lookup or try doesn't work. Could anyone show me what I'm supposed to do to get my loaded image onto the background surface? Sorry if dumb question, I'm new to PyGame.
This here is my code so far (without my previous attempts at adding the background)
# Import the Pygame module
import pygame
# Intialize
pygame.init()
# Variables
image = pygame.image.load('Spaceship.gif')
bg = pygame.image.load('bg.gif')
# Setup
pygame.display.set_mode([1000,500])
pygame.display.set_icon(image)
pygame.display.set_caption("Space Man Type Beat")
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
# Quit
pygame.quit()
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 1 year ago.
on my mac I ran pygame and never got it working and I've done my research and found no conclusion.
The code I used to bring up some sort of screen...
import pygame
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# update game objects
# [...]
# clear display
win.fill((0, 0, 0))
# draw game objects
# [...]
# update display
pygame.display.flip()
# limit frames per second
clock.tick(60)
pygame.quit()
The python launcher logo jumps up and down for a second
and comes up with python quit unexepctedly
Process: Python [4082]
Path: /Library/Frameworks
/Python.framework/Versions/3.9/Resources/Python.app
/Contents/MacOS/Python
Identifier: org.python.python
Version: 3.9.5 (3.9.5)
Code Type: X86-64 (Native)
Parent Process: Python [4040]
Responsible: Python [4082]
User ID: 505
Any help on how I can get pygame to work?
Or how to get any screen at all?
btw i installed pygame with pip3
and pip
You aren't telling pygame to run any events, and so the window just closes and the program stops.
This question already has an answer here:
Pygame unresponsive display
(1 answer)
Closed 2 years ago.
I Was try to build a game and I was build the interfaces normally, but sundely pygame screen crashed and the pygame screen window becomes unresponsive always I run the a simple program.
import pygame
pygame.init()
pygame.display.init()
pygame.display.set_caption("Yathezz")
tela = pygame.display.set_mode((600,600))
preto = (0,0,0)
def set_telaPartida(tela,nome__jogador,pontos_totais, situacao):
tela.fill(preto)
#if(situacao == 1):
# set_telaLancaDados(tela,nome__jogador,pontos_totais)
# elif(situacao ==2):
# a = 1
# seta tela perguntando o relançamento
#elif(situacao == 3):
# b= 2
#set_telaRelancaDados
while True:
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
I have done some tests and I suspect that the problem are in this line
pygame.display.update()
But I don't have Idea what's happening. Could someone Help me???
You have to handle the events, by either pygame.event.pump() or pygame.event.get(), to keep the window responding. For instance:
run = Ture
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
See the documentation of 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. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.
pygame.event.get() calls pygame.event.pump(). The pygame.QUIT occurs when the close button is pressed.
This question already has answers here:
Unable to install pygame on Python via pip (Windows 10)
(6 answers)
Pygame 1.9.6 not loading in Python 3.8.1 [duplicate]
(1 answer)
Pygame not showing anything in the window [duplicate]
(2 answers)
Closed 2 years ago.
I have a MacOS Catalina 10.15.4 and Python 3.8.1. I finally got pygame 1.9.6 to install through the terminal with homebrew,xcode,and xquartz downloaded and imported it in my program. I'm a student and following instructions for a project so all the code has already been provided to me and says that a pygame window should show up. When I run the Python icon on my dock bounces up and down and it prints in the shell "Game console initialized
pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html". I've tried to read and mess around with what versions work with what but I'm pretty lost to what else I should be doing. Thanks for the help!
Here is my code:
print("Game console initialized")
import pygame
pygame.init()
screenSize = width,height = 240,180
display = pygame.display.set_mode(screenSize)
starImage = pygame.image.load("star.gif")
starBox = starImage.get_rect()
speed = [3,5]
keepPlaying = True
while keepPlaying:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepPlaying = False
starBox = starBox.move(speed)
if starBox.left<0 or starBox.right > width:
speed[0] = - speed[0]
if starBox.top <0 or starBox.bottom > height:
speed[1] = - speed[1]
black = 0,0,0
display.fill(black)
display.blit(starImage, starBox)
pygame.display.flip()
pygame.time.delay(100)
pygame.display.quit()
pygame.quit()
This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
pygame window closes immediatly after opening up
(1 answer)
Closed 1 year ago.
I have used Pygame with python 2.7 before but recently I 'upgraded' to python 3.2. I downloaded and installed the newest version of Pygame which is said to work with this version of python. I have, however, had this rather frustrating error on what should be a simple block of code. The code is:
import pygame, random
title = "Hello!"
width = 640
height = 400
pygame.init()
screen = pygame.display.set_mode((width, height))
running = True
clock = pygame.time.Clock()
pygame.display.set_caption(title)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False
else:
print(event.type)
clock.tick(240)
pygame.quit()
And every single time I run it I get:
17
1
4
Traceback (most recent call last):
File "C:/Users/David/Desktop/hjdfhksdf.py", line 15, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
Why am I getting this error?
if event.type == pygame.quit():
In the line above, you're calling pygame.quit() which is a function, while what you really want is the constant pygame.QUIT. By calling pygame.quit(), pygame is no longer initialized, which is why you get that error.
Thus, changing the line to:
if event.type == pygame.QUIT: # Note the capitalization
Will solve your problem.
It's important to note that pygame.quit() will not exit the program.