Python Crash Course Alien Invasion issue - python

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'

Related

Why doesn't my simple python pygame program open a window?

I am trying to learn how to make a game of Snake in Python. I found a tutorial using pygame, but I'm having trouble making it work. Here's my code :
import os, pygame
os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()
dis = pygame.display.set_mode((400,300))
pygame.display.set_caption('Test')
while 1 :
pygame.display.update()
pygame.quit()
quit()
The problem is that the game window simply doesn't open. I get ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred errors but I think those are related to the sound. Other than that, no message in the console.
The code is running in a debian 11 VM inside a Windows 7 host.
Delete the os.environ["SDL_VIDEODRIVER"] = "dummy"
From this
You can use PyGame without opening a visible display (e.g. for testing, or for integrating with other frameworks that have their own displays) by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.
As the documentation states, the dummy mode is designed to create an SDL environment without opening a window. Because in this case, we do actually want to create a window, You should remove the line os.environ["SDL_VIDEODRIVER"] = "dummy". This does also mean that you don't have to import the os module.

Can't create a window with pygame on Mac

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

Playing sound in background in python while GUI is running with tkinter

I have been trying to run sound in background while App is running. I am Arch Linux and Python 3.8
I have tried playsound as like this
playsound('music.mp3', False)
but I get error saying system not supported. I have also tried pygame following way:
from pygame import mixer
mixer.init()
mixer.music.load("music.mp3")
mixer.music.play()
But I get error pygame.error: Unrecognized audio format. Is there any other way I can run music in background some task is executing on GUI with tkinter. The program will run on arch and Ubuntu.
Try this code it will run the background music continuously till the app is running
mixer.music.play(-1)
Try this:
mixer.music.play(-1)
And if you want your background music to be stopped in 10s then add this too:
gui_name.after(10000, mixer.music.stop)
Hope it will help you.

Virtualenv starting file from terminal - cant use file i started

Dears,
I learned today how to use virtualenv for my python projects. Now I started my pygame file from virtualenv via terminal "python mygame.py" and it worked, however I cannot play the actual game. When I move to the pygame window and hit any key, the output continues being displayed in the terminal. How can I switch to the actual pygame window and play the game while I am in the virtualenv?
Thank you

python error python 3.5 pygame

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 *

Categories

Resources