trouble with pygame in python version 3.3 - python

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.

Related

How can I get PyGame to work with VSCode?

I already have installed the pygame package and get this error :
ModuleNotFoundError: No module named 'pygame'
This is an issue I had for like a week now however I have found something that works with me and hopefully it works with you!
If you already have installed the pygame package and get this error :
ModuleNotFoundError: No module named 'pygame'
Try to debug your python file and hopefully the error wont appear. Also, make sure to not "Run Code" but instead "Run Python File". To do this at the top right corner should be a play button press that an select "Run Python File".
You can test your pygame with this code that will open up a blank pygame window:
import pygame
WIDTH, HEIGHT = 800, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
run = True
while run:
for event in pygame.event.get ():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if __name__ == "__main__":
main()
If the above doesn't work then try to upgrade or downgrade your python version and pygame version to:
Python: 3.7.4(32-bit)
Pygame: 1.9.6
To change python version:
Go to https://www.python.org/downloads/windows/ and scroll down till you find Python 3.7.4 and download the "Download Windows x86 executable installer"
Install pyhton
Add python to PATH
WINDOWS + R
sysdm.cpl
Advanced > Enviroment Variables > Path > Edit
Add the following to path on User Variable and System Variable
C:\Users\user\AppData\Local\Programs\Python\Python37-32\Scripts
C:\Users\user\AppData\Local\Programs\Python\Python37-32
To install pygame version 1.9.6:
WINDOWS + R
CMD
Enter the following
pip install pygame==1.9.6
4. To double check if version is correct
pip show pygame
Ensure to "Run Python File" and not "Run Code"
Hopefully this has helped!

Why pygame cannot find reference '...' in '__init__.py'?

I am trying to learn pygame and I have an issue. I use PyCharm and when I start to code, PyCharm cannot find references about pygame (I tried on VScode, it does the same). For exemple, the references 'init', 'quit', 'KEYDOWN', ... My code still work, I can execute it and no problems appear but I don't have the auto completation (this is annoying) and if it cannot find the reference, it might have a problem.
I have pygame installed, I tried to install with .whl files on python.org, with the pip command pip install pygame, with the pip command py -m pip install -U pygame --user, I tried to see if pygame worked with the command py -m pygame.examples.aliens and no problems. I tried to unintall it to re-install it but nothing work. My filename isn't 'pygame.py' and i already tried to see any solutions on youtube, openclassroom, stackoverflow, ... I'm using python 3.8.1 by the way.
If someone could help me, this is really annoying and I could not understand why it does that. Thanks you !
Here's my code test :
import pygame
pygame.init()
screen = pygame.display.set_mode((1080, 720))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if event.type == pygame.KEYDOWN:
print('A key got pressed')
And here's some screenshots :
Ironic though, Pygame does have an init() method. I face the same issue while linting. For some reasons the linter can't recognize init() as a method.
open settings and search for "python.linting.pylintArgs" (no quotes)
then press add item and paste this "--extension-pkg-whitelist=pygame" (no quotes)
it should help for linting

Python program throws error because it says it cannot find pygame even though I have imported it in the program

import sys
import pygame
def run_game():
pygame.init()
screen=pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Battle")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
I get this error:
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/test/test.py", line 3, in
import pygame
ModuleNotFoundError: No module named 'pygame'
I have installed pygame and did the import command on the cmd on windows.
How can I solve the problem?
If you have more than one python editor in your pc, make sure you installed pygame into the right python editor
This kind of problem arises because of two resiongs:
If you have more then on python version in your system and you don't set the python environment yet for default python version to use.
Some time editor not response well after installing packages or library. In this case just close the editor and restart it. Most of the time it helps to fetch the library.

No module named pygame

Why won't this work? The best solution I could find is that the name of my game can't be pygame and I changed the name, yet it still doesn't work. I'm getting this error message:
Traceback (most recent call last):
File "C:\Users\dnw13\Desktop\Python projects and notes\pygame games.py", line 1, in <module>
import pygame, sys
File "C:\Users\dnw13\Desktop\Python projects and notes\pygame.py", line 2, in <module>
from pygame.locals import *
ModuleNotFoundError: No module named 'pygame.locals'; 'pygame' is not a package
here is my code
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()
First off, if you're going to use a library called pygame, you shouldn't also have a .py file named pygame.py in your working directory. Second, pygame, unlike tkinter, is not pre-installed the way tkinter was. You will need to install it separately using one of the following methods:
1st way, using pip:
# in the console (Terminal or cmd) depending on your OS, type:
# for windows: (replace 3 with 2 if using python 2)
py -3 -m pip install pygame
# or: (just pip for python 2)
pip3 install pygame
# for Debian: (Mac OS or Unix or Linux etc.)
pip3 install pygame
# just pip for python 2
# or: (replace 3 with 2 for python 2)
python3 -m install pygame
2nd way, from pre-compiled file:
Go to the official download page. And choose your operating system to download the file. You can also follow the official installation tutorial page.
3rd way, build from source:
Follow the same method from the second method and choose the source files. Then do ./configure then make && make install in the console. This method is not recommended.
If you work in Windows (I solved it in Win 10), look at global variables:
search for System Info
choose button Environment Variables...
look at the Path variable to see if Python has the right path
if not change it :)
if necessary - restart programm(s)
It happens when you have multiple versions of Python installed
Make sure pygame is added to the correct path. It installed pygame in the wrong place and python couldn't find it. It took it from where it had installed it (it tells you were this is when you install it.) and dragged it into where i save my games and it now works.

pygame only displays grey window on screen

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.

Categories