I just get the general "finished with exit code 0" Not sure why its not printing the screen anymore. What would cause this?
If anyone has any input as to why this would be, i'd very much appreciate it so i can move forward & finish the project!
From what ive read, its hard to go on without knowing what actually is causing the "none-error"
import sys
import pygame
from settings import Settings
from ship import Ship
class Alieninvasion:
"""overall class to manage game assets & behaviors"""
def __init__(self):
"""initialize the game & create game resources"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
self.screen = pygame.display.set_mode((1000, 600)) # the self.screen portion makes it available in all methods in the class
pygame.display.set_caption("Alien Invasion!!!") # the self.settings & self.screen can possibly replace the
# screen & bg_color that doesn't include the settings syntax
self.ship = Ship(self)
# set the background color
self.bg_color = (230, 230, 230)
def run_game(self):
"""starting the main loop for the game"""
while True:
self._check_events()
self.update_screen()
# redraw the screen during each pass through the loop
self.screen.fill(self.bg_color)
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
def _check_events(self):
"""responds to keypresses & mouse events"""
for event in pygame.event.get():
if event.type == pygame.QUIT():
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key ==pygame.K_RIGHT:
# move the ship to the right
self.ship.rect.x += 1
# makes the most recently drawn screen visible. Continually updates the display to show new game position elements and hide old ones.
pygame.display.flip()
def update_screen(self):
"""update images on the screen & flip to a new screen"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
pygame.display.flip()
if __name__ == 'main':
# make a game instance and run the game.
ai = Alieninvasion()
ai.run_game()
"""the run_game as part of the if block ensures it only runs if the file is called directly"""
Related
Hi Im getting this error : "SyntaxError: Invalid Syntax"
& C:/Users/unhackerguard/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/unhackerguard/Documents/Alien invasion/alien_invasion.py"
File "<stdin>", line 1
& C:/Users/unhackerguard/AppData/Local/Programs/Python/Python39/python.exe
"c:/Users/unhackerguard/Documents/Alien invasion/alien_invasion.py"
(there is a carrot under the &) when Y am trying to my code, me and friend have to debug with no solution in site and tried to google to the best of my ability.
import sys
import pygame
# file imports
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 resoures"""
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)
# Set Background color.
self.bg_color= (230,230,230)
def run_game(self):
"""Start the main loop for the game"""
while True:
self._check_events
self.ship.update
self._update_screen
def _check_events(self):
# responds to keypresses and mouse events.
for event in pygame.event.get():
if event.type== pygame.QUIT:
sys.exit()
elif event.type== pygame.KEYDOWN:
if event.key== pygame.K_RIGHT:
self.ship.moving_right= True
elif event.key== pygame.K_LEFT:
self.ship.moving_left= True
elif event.type== pygame.KEYUP:
if event.key== pygame.K_RIGHT:
self.ship.moving_right= False
elif event.type== pygame.K_LEFT:
self.ship.moving_left= False
print(event)
def _update_screen(self):
# Redraw the screen during each pass though the loop
self.screen.fill(self.bg_color)
self.ship.blitme()
"""update images on the screen, and flip to the new screen"""
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()```
```import pygame
class Ship:
""" a class to manage ship."""
def __init__(self, ai_game):
"""initialize the ship and set its starting positition."""
self.screen= ai_game.screen
self.screen_rect= ai_game.screen.get_rect()
#load the ship image and get its rect.
self.image= pygame.image.load('C:/Users/unhackerguard/AppData/Local/Programs/Python/Python39/Lib/site-packages/pygame/examples/data/ship.bmp')
self.rect= self.image.get_rect()
# start each new ship at the bottom center of the screen.
self.rect.midbottom= self.screen_rect.midbottom
#movent flag
self.moving_right= False
self.moving_left= False
def update(self):
"""update the ships posotion based on the movent flag"""
if self.moving_right:
self.rect.x+= 1
if self.moving_left:
self.rect.x-= 1
def blitme(self):
"""draw the ship at its current location"""
self.screen.blit(self.image, self.rect)```
```class Settings:
""" A class to store all the settings for alien invasion"""
def __init__(self):
"""Initialize the game settings"""
# Screen Settings
self.screen_width= 1200
self.screen_height= 800
self.bg_color= (230,230,230)
```
I've written my code and whenever I run it, I expect for it to open up the screen with the background but instead nothing happens at all. I don't know where I went wrong or what I did wrong.
My code:
import os.path
import sys
import pygame
from settings import Settings
class Slingshot_Adventures:
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption('Slingshot Adventures')
self.bg_color = (0, 0, 0)
bg = pygame.image.load_basic(os.path.join('Final/bg.bmp'))
def run_game(self):
while True:
# Watch for keyboard and mouse events.
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.bg_color)
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = Slingshot_Adventures()
ai.run_game
You have set the local variable bg in the constructor of the class Slingshot_Adventures, but you did not set the attribute self.bg:
bg = pygame.image.load_basic(os.path.join('Final/bg.bmp'))
self.bg = pygame.image.load_basic(os.path.join('Final/bg.bmp'))
class Slingshot_Adventures:
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption('Slingshot Adventures')
self.bg_color = (0, 0, 0)
#put your path to the image here
self.image = pygame.image.load(r"path/to/your/image.png")
def run_game(self):
while True:
# Watch for keyboard and mouse events.
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.bg_color)
self.screen.blit(self.image, (0, 0))
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = Slingshot_Adventures()
ai.run_game()
At the moment, I am learning from the Python Crash Course Intro Book By Eric Matthes. I am on the section about making a game with Pygame. The second they added keyup and keydown my code stopped letting me move the ship right and left. Beforehand I was able to move the ship without the additional functions.
I have tried looking on the web. I have tried messing around with the code to fit the situation. I, however, have not tried another IDE. I apologize in advance if this is not properly formatted, I am a noob to this website. Thanks!
===================
alien_invaion.py
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
# Initialize pygame, settings, and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings. screen_height))
pygame.display.set_caption("Alien Invasion")
# Make a ship.
ship = Ship(ai_settings, screen)
# Start the main loop of the game.
while True:
gf.check_events(ship)
ship.update
gf.update_screen(ai_settings, screen, ship)
run_game()
ship.py
import pygame
class Ship():
def __init__(self, ai_settings, screen):
"""Initialize the ship and set its starting position."""
self.screen = screen
self.ai_settings = ai_settings
# Load the ship image and get is rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# Store a decimal value for the ship's center.
# Movement flags
self.moving_right = False
def update(self):
"""Update the ship's position based on the movement flag."""
# Update the ship's center value, not rect.
if self.moving_right:
self.rect.centerx += 1
# Update rect object from self.center.
def blitme(self):
"""Draw the ship at its current location.""
self.screen.blit(self.image, self.rect)
settings.py
class Settings():
"""A class to store all settings for Alien Invasion"""
def __init__(self):
"""Initialize the game's settings."""
# Screen Settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (0, 23, 233)
# Ship settings
game_functions.py
import sys
import pygame
def check_events(ship):
"""respond to keypresss's and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ship.moving_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
ship.moving_right = False
def update_screen(ai_settings, screen, ship):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_color)
ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
No error messages... I expect the ship to be able to move when I hold the left and right keys down and the ship to stop moving when I release said keys.
You missed the parentheses when the update method is called:
ship.update
ship.update()
I am doing the Alien Invasion project in the Python Crash Course book. When I test the code to see if the ship comes on screen the screen starts then shuts down.
I have been looking through the code for hours now without finding out why.
The game:
import sys
import pygame
from settings import Settings
from ship import Ship
def run_game():
# Initialize pygame, settings, and screen object
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# Make a ship
ship = Ship(screen)
# Set background color
bg_color = (230, 230, 230)
# Start the main loop for the game
while True:
# Watch for keyboard and mouse events
for event in pygame.event.get():
if event == pygame.quit():
sys.exit()
# Redraw the screen during each pass through the loop
screen.fill(ai_settings.bg_color)
ship.blitme()
# Make most recently drawn screen visible
pygame.display.flip()
run_game()
Settings:
class Settings():
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
Ship:
import pygame
class Ship():
def __init__(self, screen):
self.screen = screen
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
self.screen.blit(self.image, self.rect)
This is the error that comes up
"C:\Users\My Name\Desktop\Mapper\Python'\Scripts\python.exe" "C:/Users/My Name/Desktop/Mapper/Python/Projects/alien invasion/alien_invasion.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:/Users/My Name/Desktop/Mapper/Python/Projects/alien invasion/alien_invasion.py", line 37, in <module>
run_game()
File "C:/Users/My Name/Desktop/Mapper/Python/Projects/alien invasion/alien_invasion.py", line 30, in run_game
screen.fill(ai_settings.bg_color)
pygame.error: display Surface quit
Process finished with exit code 1
The line
if event == pygame.quit():
doesn't do what you expect it to do. pygame.quit() is a function and it uninitialize all pygame modules. The function returns None and so the condition fails. The code runs through and crashes at the next instruction which tries to access a pygame module.
Change it to:
if event.type == pygame.QUIT:
The .type property of a pygame.event.Event object contains the event type identifier. pygame.QUIT is an enumerator constant which identifies the quit event. See the documentation of pygame.event.
I'm working on a Ping Pong game in Pygame. Having some trouble getting the paddles to display on screen within a class. I have a feeling either my constructor init method is incorrect (although not throwing up any errors) or the display colour is overwriting the paddles.
Here's my code.
Program.py
import sys
import pygame
from settings import Settings
from paddles import Paddles
import game_functions as gf
def run_game():
# Initialise pygame, settings and screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption('Ping Pong')
# Start the main loop for the game.
while True:
gf.check_events()
gf.update_screen(ai_settings,screen)
#Make paddles
paddles = Paddles(screen)
run_game()
paddles.py
import pygame
import sys
class Paddles():
def __init__(self, screen):
self.screen = screen
self.paddle_l = pygame.draw.rect(screen, (255, 255, 255), [15, 250, 10, 100])
self.paddle_r = pygame.draw.rect(screen, (255, 255, 255), [780, 250, 10, 100])
def paddles(self):
pass
settings.py
class Settings():
"""A class to store all settings for Ping Pong"""
def __init__(self):
"""Initialise the game's settings."""
# Screen settings
self.screen_width = 800
self.screen_height = 600
self.bg_colour = (0,0,0)
game_functions.py
import sys
import pygame
def check_events():
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def update_screen(ai_settings, screen):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_colour)
pygame.display.flip()
Like I said I'm not getting any errors, the game window just opens to a black background. The code worked when I put it inside a normal function but not a class.
What am I doing wrong? How do I get the paddles to display?
Is there a reason you're setting paddles outside of the "main loop?"
Moving paddles into your update_screen method gets them showing, at the very least. However, this is newing up a new Paddles object each time update_screen is called.
def update_screen(ai_settings, screen):
"""Update images on the screen and flip to the new screen."""
# Redraw the screen during each pass through the loop.
screen.fill(ai_settings.bg_colour)
paddles = Paddles(screen)
pygame.display.flip()