OK, I’m very new to coding, and I am just learning Python. I figured I would start with some basic Pygame exercises to try to have something to program.
I’ve installed Python 3.4.3 and PyCharm. I also installed the Pygame executable “pygame-1.9.2a0-hg_5974ff8dae3c+.win32-py3.4.msi” from here: https://bitbucket.org/pygame/pygame/downloads
I ran the Pygame installer, and it seemed to complete without visible issues, though there were no obvious signs like new shortcuts on my desktop.
I then went here to try some basic test code involving Pygame:
http://pythonprogramming.net/pygame-python-3-part-1-intro/
So I copied the code from that example into my Pycharm, and ran it. It seems to create a blank Pycharm windows alright, but the PyCharm Code Inspector is giving me several warnings, and I really want to know why I am getting these warnings.
The first Pycharm warning is from line 5, “Cannot find reference ‘init’ in ‘__init__.py’
The next warning is line 16, “Cannot find reference ‘QUIT’ in ‘__init__.py’
The third and final warning is line 24, “Cannot find reference ‘quit’ in ‘__init__.py’
Why can’t it find these references? What’s wrong?
The code itself I paste below:
#! /usr/bin/python
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
This will work for you with no errors... import the system and do a system exit instead
#! /usr/bin/python
import pygame
import sys
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()
Related
I just spent a fair amount of time finding a 64-bit installation of pygame to use with python 3.3, (here) and now am trying to make a window. However, although the window opens up fine it does not close when it hit the x button. In fact, I have to close IDLE to close the window. I am running a 64 bit version of Win 7. Here is my code:
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
When I append
time.sleep(5)
pygame.quit()
It still doesn't close. My only guess would be that pygame.quit might go inside one of the loops, but even if that were resolved I would greatly prefer being able to close the window when I want to.
Most pygame tutorials seem to suggest exiting by calling pygame.quit() and then sys.exit(). I have personally run into problems (was on a unix system though) where this still did not close the window properly. The solution was to add pygame.display.quit() specifically before pygame.quit(). That should not be necessary as far as I can tell, and I'm afraid I don't know why that solved the issue but it did.
if you want to make pygame close when window button x is pressed, put the code like this:
from sys import exit
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
We put exit() after pygame.quit(), because pygame.quit() makes the system exit and exit() closes that window.
Not sure but try this Because you code runs fine on my system after I add pygame.quit() at the end
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
try:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
except SystemExit:
pygame.quit()
Its perhaps because as Idle is made on Tkinter and thus Tkinter and Pygame main loop do not have a mutual understanding.
Your code will run very well on command prompt though.
This was the final code that worked for me on OSX whilst keeping the kernel alive on Jupyter. EDIT - it does still crash the kernel sometimes :-(
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
pygame.quit()
exit()
Also needed to downgrade ipython to get rid of some magic alias warning messages using:
conda install ipython=7.2.0
apparently that issue is due to be fixed in ipython 7.6.0
Suffered the same issues on Python 3.7.4 while running it from in IDE (Spyder 3.3.6). In my case the pygame.quit() would not completely close the program.
Nonetheless, adding quit() or exit() did the trick for me!
Add this at the top:
import sys
Add this where you need to quit:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
try using the following command:
sys.exit(0)
notice: You will need to import the sys library in order to use it.
The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.
To answer the original question: You must call pygame.quit() after breaking the main loop. One elegant solution goes as follows:
def run():
pygame.init()
while True:
# ...
for event in pygame.event.get():
# Handle other events
if event.type == pygame.QUIT:
return pygame.quit()
So, I'm trying to change the PyGame icon for a game I am working on. Whenever I run the code, it hits me with pygame.error: Unsupported image format
The pygame window also opens and closes with the code under # Setting the game icon, and it did not do that when I did not have those lines in the code.
I've searched for a good answer, but I can't find a good one. If anyone's got any suggestions I would appreciate them.
I am programming on Visual Studio Code with Python 3.10
Here is my code:
import time
import pygame
# Initializes Pygame
pygame.init()
# Game Screen Variables
background_colour = (255,255,255)
# Sets up the playscreen
screen = pygame.display.set_mode((1100,750),0,32)
pygame.display.set_caption("Dusco's Game")
screen.fill(background_colour)
pygame.display.flip()
# Setting the game icon
img = pygame.image.load('gameicon.png')
pygame.display.set_icon(img)
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit ()
Make sure that it is a PNG and that you do the path so since it is erroring try this:
img = pygame.image.load('c:/users/USERNAME/PATH_TO_IMAGE/gameicon.png')
I'm a new in PyGame and I was making a game. Until I discovered that when you were moving the window to another place of your desktop for example, the game temporarily stops. But ticks of the game still running when I used pygame.time.get_ticks().
So I made a completely new program with only the necessary code and it does the same thing.
Can someone explain me why it does it and if we can resolve this problem?
import pygame
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('The Test Program')
running = True
update_counter = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
print(str(update_counter) + " updates")
update_counter += 1
pygame.quit()
quit()
# So try to move the window across your screen and you will see that, prints will stop and they will resume when you will release the click
So someone tell me that was normal, so I think it's only on Windows but there are no solutions. I put a script that show a pause symbol on screen when the cursor leave the window to make it normal.
I recently downloaded pygame on my school pc(I have experience with it before), but when I try to open a window it does not work and are black even though I colored the window in the code.
I even copied this code:
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
But it does the same thing.
Extra:
I use Spyder with python 3.6
Thanks in advance:)
Your code work in my mac. If you are in Windows, try to restart the PC. Also, try to execute your example from a command line.
When I run a pygame program, a white square (about 15px by 15px) follows my cursor around the window. This happens on the most basic pygame program and a few others, too.
My most basic pygame program:
import pygame, sys
DISPLAYSURF = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
# --- Limit to 60 frames per second
clock.tick(30)
Why is this happening? Is it my code, my system, or Pygame?
I am running python 3.7, pygame 1.9.4, and macOS 10.13.6.
EDIT:
After running the same program on Crouton (linux) there is no square.
Where is your pygame.init() at the beginning? That might be what is causing the problem. Also you do not have a background, which shouldn't be a problem, but I would try it if nothing else works.
I tried your code on my PC and it worked fine so I'm not sure what is happening but you could try the following:
Enter the command pygame.init() on the second line
Replace pygame.display.update() with pygame.display.flip()