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.
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'
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.
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 *
This question already has answers here:
how to play wav file in python?
(5 answers)
How can I play an mp3 with pygame?
(7 answers)
Closed 1 year ago.
import pygame
pygame.mixer.init()
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
When I run the code, there's no sound and the program ends in like a second. Initially I didn't have the while loop until I saw the suggestions in the answers to similar questions. The program does enter the while loop on my friend's windows system, but not on my mac, and it doesn't have any sound either even on my friend's windows system. Does anybody know how to solve it?
Works well on Ubuntu 10.04 and Pygame 1.9.1.
Some things you can try:
initialize whole pygame pygame.init()
i_4_got's suggestion (create a display) pygame.display.set_mode((200,100))
put a pause (tick) between play an get_busy
poll events inside the loop pygame.event.get()
Example:
import pygame
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.music.load("only one.mp3")
pygame.mixer.music.play(0)
clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
pygame.event.poll()
clock.tick(10)
import winsound
winsound.PlaySound(filename [as string with path if necessary], flag [integer z.B. 1 for asynchronous] )
This works fine with Windows 8 and Python 2.7.6 and *.wav files
The different flags you can check if you type winsound. and look for the autofill list.
The flags are like SND_FILENAME or similar.
If you type them into the editor like: *winsound.SND_FILENAME
you get the integer to apply in the PLaySound command from above
Have fun
Pygame sound not working fix
INFO
Try reinstalling pygame or upgrading it
I had version 1.9.1 which I upgraded to 1.9.3 by the following command
pip install pygame --upgrade
Note:
If you install it with easy_install it doesn't work . Not sure but
when I tried with easy_install it doesn't work
The code is the same as yours but i just replaced it with my music file to check it.
You can see it in the image :
In this image
.wav files seem to work well
I checked it with .mp3 files and found that it does'nt work.
You can convert .mp3 to .wav from : Converter
Or use PyMedia
If you do not want to convert it using any website.
You can also convert it with pydub.
You can download pydub from this
link
Or type this command in the command line:
pip install pydub
Usually, Pygame will not play mp3 files. You could test to see if .wav and .ogg files will play first, to make sure your code is correct (based on what you pasted, it seems to be right). I suggest converting your mp3 sounds to ogg for Pygame.
You have to add this to the end, in order for pygame to be happy:
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
Also, import sys.
it would be easier if u changed the mp3 to ogg format ,try this.. its simple
from pygame import *
pygame.init()
pygame.mixer.music.load("only one.ogg")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.3)
vote the answer if its working, if not tell me whats wrong