pygame only displays grey window on screen - python

I have been trying to use pygame to show a game scene, but it seems to have stopped working for me: it displays nothing more than a grey box in any screen I create until pygame shuts down, then what should be on the screen flashes briefly before it exits. For instance, the minimal code below displays a grey screen for 5 seconds, then flashes black quickly and quits:
import pygame as pg
import time
pg.init()
sc = pg.display.set_mode((600,600))
sc.fill((0,0,0))
pg.display.flip()
time.sleep(5)
This seems to be an issue with the screen display and not pygame itself, since I can make surfaces and save them to image files with pg.image.save(), and they look fine there.
This is the first time I've tried using pygame to display on the screen since I upgraded to OSX 10.12. This is using Python 2.7, and pygame 1.9.3 (downloaded from pip, but also an issue with the dev 1.9.4 version from bitbucket). All of the sdl dependencies are at the latest version (according to homebrew).
However, I'm now lost for how to do more troubleshooting, since I don't know enough about the innards of how pygame communicates with the display. Is there anything else I can do to help find the root of this problem and correct it?
Thank you!

You need a game loop that continually does the following:
calls pygame.display.flip() -- this will update the screen with new changes
calls pygame.event.get() -- this is the most important because otherwise, the event queue will not get flushed and the OS will assume your program is being unresponsive. You can't have things like sleep(5) in PyGame.

Related

Python turtle screen freezing after updating to Monterey 12.5.1

I'm just trying to do some of my 100 days of code homework, but since my OS upgraded last night, whenever I try to run a turtle screen (the same code that worked yesterday), the window freezes and I have to shut it down.
Everything ( to my knowledge ) is upgraded and updated in the virtual environment and my computer. ( I use homebrew ). Same freezing screen when code is ran in Idle,pycharm, visual studio code, etc.
Googling around it seems to me an OS issue, has anyone found a workaround? I've tried everything I can find to fix it, but nothing is working. I can't even get an error code because the window freezes and I have to force it to close.
Should I just go back to the last update when everything was working fine?
I use a Mac with Monterey, working on the 100 days challenge and have the same problem.
I did not try to use Turtle with other OS.
I solved half of the problem, by going into Python-Preferences-iPhython Console-Graphics and changing graphics backend to Tkinter.
Now the page does not freeze anymore at the beginning, it opens the screen, executes the code and then when I close the screen I find another frozen screen behind which I can exit only by Force Quit.

Tkinter interfering with pygame on OSX (abort trap 6)

I am working with OSX Yosemite. Here is my code:
http://pastebin.com/AWGu37z4
Here is my input file (to run it, just do python myscript.py myinput.txt):
http://pastebin.com/ueaeY0Ss
When I press the s button, a TK window should appear to show an text input box. If I cut and paste the code to just show the window and run it on its own, it works fine. However, if I run it in the middle of the simulation with the pygame window open and running, I get the following error (abort trap 6):
http://pastebin.com/TCwUxjCM
I have no idea on what this could be, nor on how to solve it.
I had the same problem and finally got my program to work by changing the order: the pygame loop has to start after the tk window/mainloop.
I am using macosx mojave 10.14.3 and python3.7 with pygame 1.9.6

Where does the turtle (and its screen) appear in scipy/anaconda/spyder (2.7)?

I'm using SciPy/Anaconda/Spyder and when I put in:
import turtle
from turtle import Turtle
turtle.getscreen()
turtle.showturtle
and run it nothing happens. Whereas in IDLE when the script is run a new screen appears with a "turtle" (the turtle being a right pointing arrow head) in the middle of it.
Where does the "turtle screen appear" in SciPy/Anaconda/Spyder?
To make this work you need to:
Select the IPython Console instead of the Python one
Enter this command there: %gui tk
Run your provided code (it works for me on Linux).
Note: If you are on Windows, unfortunately there is a bug in Anaconda that prevents people to use the turtle module. This bug is not related to the module itself but to the graphical toolkit it uses to create the turtle animations.

trouble with pygame in python version 3.3

I have successfully used pygame in the past, but now it won't work.
my source is
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
using this in the shell works fine and creates a pygame window, but it will not work when making a module. it says pygame is not a package.
You have two different versions of Python installed (Python(x,y) and the python.org version), and pygame is only installed on one of them. When you run the interpreter through IDLE or the command line for the version that has pygame associated with you, you can import pygame and use it just fine. However, in Windows Explorer, .py files are associated with the other version of Python, that does not have pygame installed, so when you try to run a file by double-clicking on it the wrong Python interpreter starts, and it can't import pygame because it's not installed for that version.
To fix this, all you need to do is re-associate .py files with the correct version of Python. Right-click on a .py file, select Properties, and in the dialog box that comes up find where it says something like "Opens with:" and click the Change button. Browse to C:\Python33 and select python.exe, and you should be all set. Make sure the option "Always use the selected program to open this kind of file" is set, then click OK.
I'm on XP right now, so the process might be slightly different depending on which version of Windows you're running, but it should be fairly similar.

pygame for python on virtual (VMWare) Windows XP on Mac OS10.6.8

I'm running Python 2.7 on Windows XP as a virtual machine on my Macbook (OS 10.6.8) using VMWare.
I'm going through the python/pygame video tutorials at thenewboston.com and am running the following code:
bif="bg.jpg"
mif="ball.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1196,733),0,32)
background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
The program (which displays a background jpg and a ball that follows your mouse cursor) is running as expected bar one problem, which is that the ball doesn't follow the cursor as expected, rather it moves seemingly randomly around the edge of the screen.
Is this because the x,y co-ordinates being returned by the get_pos, get_width, and get_height functions are unexpected data due to the fact that I'm running a virtual machine?
Thanks.
It's not because you're working on a virtual machine in the general sense. I just tested the code out on a Mac running 10.7 using a Windows 7 virtual machine in Parallels with PyGame + the Enthought Python Distribution.
The code works fine on that system, with my modest little soccer ball following the mouse. It may have something to do with how VMWare feeds the mouse coordinates, but it's not all virtual machines.
I don't know much about the behaviour on virtual machines so I can't really help you there. But it might help you to say that I did try your code on my Linux machine and the program do work as expected with the image following the mouse.
So, given that, you are probably right that there's something strange happening with the mouse methods when running in the virtual machine.

Categories