I would like to add a background image as a step to creating a basic game but it seems that I can't find a way. The image I am trying to import is the same size as the screen and it's a bmp image.
I also have in my settings folder this line of code which I am sure but not 100% needs to be removed (self.bg_color = (230, 230,230)) if I want to blit the image in background.
import sys
import pygame
from altarboy import Altarboy
from Ship import Ship
from bullet import bullet
from bullet1 import bullet1
from settings import Settings
class SpiritualWar:
def __init__(self):
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("SpiritualWar")
self.Ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.bullet1s = pygame.sprite.Group()
self.altarboys = pygame.sprite.Group()
self._create_fleet()
…
…
def _update_screen(self):
self.screen.fill(self.settings.bg_color)
self.Ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
for bullet1 in self.bullet1s.sprites():
bullet1.draw_bullet1()
self.altarboys.draw(self.screen)
pygame.display.flip()
Possible duplicate of How to add a background image in python-pygame. I also recommend you to see Pygame: Adding a background. Next time, spend some minutes searching your question in StackOverflow before asking it. This way there will be less posts and SO will be cleaner, which helps to find solutions with ease.
I could not test the solution myself, but you can try adding the next line to the funciton __init__():
self.bg_image = pygame.image.load("player1.png")
And then changing your first line in function _update_screen():
self.screen.fill(self.settings.bg_color)
for:
self.screen.blit(self.bg_image, (0, 0))
Related
Trying to start a new pygame project. Very basic stuff. Just trying to color the screen. Currently the screen opens, and goes white. There are no errors in the console.
here is my code:
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
while True:
screen.fill((0,255,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip() # same result with .update
run_game()
id be happy if it would simply show a green screen. Any ideas what my issue could be?
Upgrading to the most recent version of pygame fixed this.
pip3 install pygame --upgrade
thanks to #rabbid76 for the solution
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Like so many on here I am trying to get the Alien Invasion game going. But I cannot get Pygame to work, it comes up when I execute the program, but it is blank, black screen, and no ship. I have tried using a previous version of Pygame (dev6 right now), but again to no avail. The code below is verbatim from the book (Python Crash Course). Here is the code:
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)
# Set the background color
self.bg_color = (230, 230, 230)
def run_game(self):
"""Start the mainloop for tha game"""
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.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recent drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance and run the game
ai = AlienInvasion()
ai.run_game()
This is the settings.py file:
class Settings:
"""A class to store all 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)
and the ship.py file:
import pygame
class Ship:
"""A class to manage the ship"""
def __init__(self, ai_game):
"""Initlialize the ship and its starting position"""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# load the ships image and get its rect
self.image = pygame.image.load('images/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
def blitme(self):
"""Draw the ship at its current location"""
self.screen.blit(self.image, self.rect)
I am running on windows 10
I am truly stumped as I have seen so many "solutions" that don't work, and the author of the book knows there is a problem, but his solution did not work for me.
It's a matter of Indentation. You have to draw the scene and to update the display in the application rather than affter the loop:
class AlienInvasion:
# [...]
def run_game(self):
"""Start the mainloop for tha game"""
while True:
# Watch for keyboard and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#-->| Indentation
# Redraw the screen during each pass.
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recent drawn screen visible.
pygame.display.flip()
I am creating a game but I am having a problem with my button class for my gui. There are no compile errors and no runtime errors either. The only problem is that on run it immediately freezes the pygame window. I don't know how to solve this.
I've tried fiddling around with the callback function (which I removed entirely) and with the update and draw loop as well but nothing seems to work.
Python 3.7.0 and Pygame 1.9.4
Button Class:
import sys
import time
import pygame
pygame.init()
class button:
def __init__(self, txt, location, bg=(255,255,255),fg=(0,0,0),size=(80,30),font_name="Times New Roman",font_size=16):
#bg is the colour of the button
#fg is the colour of the text
#location refers to the center points of the button
self.colour = bg
self.bg = bg
self.fg = fg
self.size = size
self.font = pygame.font.SysFont(font_name,font_size)
self.txt = txt
self.txt_surf = self.font.render(self.txt, 1, self.fg)
self.txt_rect = self.txt_surf.get_rect(center=[s//2 for s in self.size])
self.surface = pygame.surface.Surface(size)
self.rect = self.surface.get_rect(center=location)
def mouseover(self):
self.bg = self.colour
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
self.bg = (200,200,200)
def draw(self, screen):
self.mouseover()
self.surface.fill(self.bg)
self.surface.blit(self.txt_surf, self.txt_rect)
screen.blit(self.surface, self.rect)
Actual update/draw loop
import gui
import pygame
import sys
import time
import win32api
pygame.init()
screen = pygame.display.set_mode((400,400))
button1 = gui.button("No", (200,200))
intro = True
while intro:
screen.fill((255,255,255))
button1.draw(screen)
if win32api.GetKeyState(0x01) == -127 or win32api.GetKeyState(0x01) == -128:
if button1.rect.collidepoint(pygame.mouse.get_pos()):
intro = False
pygame.quit()
sys.exit()
pygame.display.flip()
pygame.time.wait(20)
I really just want the window to stop freezing up on run and to actually have a button press work. What it should do is immediately quit the application when you press the button in the middle. Not actually do that though.
You have to let pygame process the events in the event queue by calling pygame.event.get (or pygame.event.pump, but you should stick to use get).
Otherwise, the queue will fill up and new events will be dropped. This includes internal events that tell your OS to draw the window etc, so your window will freeze.
Also, there's no reason to use win32api to get the state of the keyboard (you can use pygame.key.get_pressed instead), but that's another topic.
I'm building a game and I'm trying to show up a little picture in the middle bottom of the screen.
I can't understand why I don't see the image at all?
This is my picture class code which in a file called devil.py:
import pygame
class Devil():
def __init__(self, screen):
"""Initialize the devil and set its starting position"""
self.screen=screen
#Load the devil image and get its rect
self.image=pygame.image.load('images/devil.bmp')
self.rect=self.image.get_rect()
self.screen_rect=screen.get_rect()
#Start each new devil at the bottom center of the screen
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
def blitme(self):
"""Draw the devil at its current location"""
self.screen.blit(self.image,self.rect)
And this is my main code which is written in another file:
import sys
import pygame
from settings import Settings
from devil import Devil
def run_game():
#Initialize pygame, settings and create a screen object.
pygame.init()
dvs_settings=Settings()
screen=pygame.display.set_mode(
(dvs_settings.screen_width, dvs_settings.screen_height))
pygame.display.set_caption("Devil vs Shitty")
#Make a devil
devil=Devil(screen)
#Start the main loop the game.
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.
screen.fill(dvs_settings.bg_color)
devil.blitme()
#Make the most recently drawn screen visible
pygame.display.flip()
run_game()
And this is my settings class in a file called 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 = 1000
self.screen_height = 600
self.bg_color=(230,230,230)
I can't find what I am doing wrong here.
I've found the problem - which is a very strange one:
When I was editing my image, I saved it as a 32bit bmp file (The default option was 24 bit, and I thought to myself "I'm using 32bit python, I think it will match better).
But when I tried to show up my image in pygame - it didn't show up.
I've tried anything. And in the end I tried to edit again the image.
Now, I saved it as a 24bit bmp and it works well!!!
My assignment is to make a game, were supposed to have multiple modules to avoid clutter in one script. I'm having an issue with import a variable from one of the modules. So far I have a settings one, and a main one. The settings is pretty simple and goes:
class Settings():
def __init__(self):
self.screen_width = 1920
self.screen_height = 1080
self.bg_color = (230, 230, 230)
Pretty simple, yet when I try to reference these variables it says "Unresolved attribute reference 'screen_width' for class 'Settings'
main goes as this:
import sys, pygame
from game_settings import Settings
def run_game():
#Initialize the game and create the window
pygame.init()
screen = pygame.display.set_mode((Settings.screen_width,Settings.screen_height), pygame.FULLSCREEN)
pygame.display.set_caption("Alien Invasion")
while True:
#Listening for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(Settings.bg_color)
pygame.display.flip()
run_game()
I thought maybe this would be a PyCharm issue, but found that it does the same thing in IDLE so what would be the correct way to import the variables?
Thanks for taking the time to read this!
You need to create an instance of your Settings class, since the attributes you set up in its __init__ method are instance attributes.
Try something like this:
def run_game():
my_settings = Settings() # create Settings instance
pygame.init()
screen = pygame.display.set_mode((my_settings.screen_width, # lookup attributes on it
my_settings.screen_height),
pygame.FULLSCREEN)
# ...
You need to create an instance of the Settings object: s = Settings()
Usage: s.bg_color, etc.
OR
Alter your Settings class like so and the properties are accessible statically:
class Settings():
screen_width = 1920
screen_height = 1080
bg_color = (230, 230, 230)
Both files have to be in the same folder and you have to create an instance of your Settings class. Then you can access the properties of your instance.
main.py:
from game_settings import Settings
s = Settings()
print(s.bg_color)
game_settings.py:
class Settings():
def __init__(self):
self.screen_width = 1920
self.screen_height = 1080
self.bg_color = (230, 230, 230)
When you run main.py, the output will be:
(230, 230, 230)