import pygame
pygame.init()
screen = pygame.display.set_mode((800,800))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Go to bottom right of your Pycharm window,you should see Project interpreter,click interpreter settings,then double click pygames ,then click pygame in the list and check specify version box on the right,then select 2.0.0.dev6.Then click install package.Now it should work.
Related
This is the 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
When I run this code on Pycharm or on idle, I get a bouncing python rocket icon on the dock (this is on MacOS), however when I run the code line by line in the terminal, the code successfully creates a pygame window. The same code works on Pycharm on windows.
Very likely you have installed different versions of Python on your system (also see Two different Python3 and Two different Python2 installations on MacOS Catalina). Certainly Pycharm has its own Python version and the terminal uses a different Python installation. Therefor you can't run the code from the terminal. One way to solve the problem is to install Pygame via the terminal:
pip3 install pygame
This is in regards to the book "Python Crash Course 2nd Edition".
After going through the "Drawing the Ship to the Screen" section in Chapter 12, I get a black screen, instead of a grey screen, and I'm not seeing the ship show up when I run my alien_invasion.py. I have tried running Matthes' downloadable resource file for that step, and I still get a black screen. I'm running these .py files from Sublime text, but have tried using terminal to run them (I get indent errors) and python IDLE to run them (gives me a pygame module not found error, though I know pygame is installed and found by Sublime).
Here is the code for the game that should no display a grey background and a ship at the bottom of the screen, if you have the ship image:
import sys
import pygame
from settings import Settings
from ship import Ship
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game."""
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
Im using Mac OS version 10.14.6.
Any solutions for this issue, or better practices or programs that I should be writing/running these game modules in?
Anyone with a Mac that has successfully installed pygame and/or has gotten alien_invasion to work?
One reply to my reddit post about this said they had a similar issue on Mac OS but when they tried on linux it worked fine...
Is there another alternative for Mac people to use short of installing linux on a separate partition or something?
Thanks for your help!
Found a solution on Eric Matthes's github. Uff da, pygame is not stable on python 3.8.2 yet...I needed to install the dev version of pygame to run with python 3.8.2.
From Eric's github:
The stable version of Pygame has not been updated to work with Python 3.8 yet. However, there is a recent development version that works with Python 3.8. To install it, run the following command:
$ python -m pip install pygame==2.0.0.dev6
You should use the same command you use to run a Python terminal session on your system, which might be python, python3, py, python3.8, or something else.
If you’ve had any issues running Pygame on macOS, this version of Pygame should address those issues as well.
I had the same issues, black screen and no ship. What solved the issue for me was, under settings, playing with the height and width. I'm a novice at this but I found that when I centered the ship instead of bottom center, the ship barely showed up at the bottom. Eventually I was able to center it at the bottom of the screen.
I had this issue with the black screen closing immediately after. The code has the ship in folder 'images/ship.bmp'. Make sure the destination is accurate. Mine was in a slightly different location as I have a project folder for the book.
Mine was 'Python Crash Course/Alien Invasion/images/ship.bmp'
I have a small timer script that displays information through a pygame window. I can run it fine as a script. When I convert it to an .exe with pyinstaller, it still runs fine, but when I exit by pressing the "x" at the top of the window, i get an error message "Failed to execute script myscript." I assume it is a problem with my closing code.
If it matters, I am running pyinstaller with options -F (make a single file) and -w (run without a console window)
I have added new parameters to the close code as suggested in other threads here. My current exit code is listed below.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
exit()
I just want it to close cleanly without an error
Solved it, I was not importing sys correctly. exit() showed up as a keyword in IDLE, so I thought it was a basic command. I added the line "import sys" to the top and changed the closing line to "sys.exit()" and it is working as expected.
Use
import sys
#insert code here
sys.exit()
instead of
exit()
The exit() command is used to exit the python shell. See here
so i wanted to make a game...and pygame doesn't seem to be working for me. I get this error, "
AttributeError: module 'pygame' has no attribute 'init'"
and ive tried every other forum and cant find help...my game is called roll and I'm using the correct pygame. What do i do?
import pygame
pygame.init()
gamedisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('roll.io')
clock = pygame.time.Clock()
dead = False
while not dead:
for event in pygame.event.get():
if event.type == pygame.QUIT:
dead = True
print(event)
pygame.display.updat()
clock.tick(30)
pygame.quit()
quit()
I've also had some trouble installing pygame for the latest python version, mostly because all guides are either old or don't support the latest python version.
So this is what worked for me:
First download(I uploaded it for you because I can't even find the place I originally downloaded it from...) it from here then copy the file and paste it into [your python folder]/scripts, then open cmd type cd [location of your script folder] and finally type pip3 install pygame-1.9.2a0-cp35-none-win32.whl and don't try to rename it to a shorter file name because for some reason it won't work.
Have you tried including
from pygame.locals import *
I am learning Python and Pygame, and have been using PyCharm for Mac. I tried to load a png-image to a Pygame-window, but i got an error: pygame.error: File is not a Windows BMP file. I tried it with IDLE, and it worked fine. What can I do to make it work in PyCharm?. I am using Python 2.7.5
Heres my code:
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
my_ball = pygame.image.load('beach_ball.png')
screen.blit(my_ball, [50, 50])
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Try google: "pygame.error: File is not a Windows BMP file"
Here is the first answer I found using google:
FAST SOLUTION for beginners:
install Python 2.7.5
Go into Applications --> Python 2.7 --> update Shell profile
Python 2.7.5 should now be your default python. Run your application with that (in terminal) and it all works.
This is by far the easiest way to get around this problem. Hope this helps.