How do I set my background as an image in Pygame? [duplicate] - python

This question already has answers here:
How to add a background image into pygame?
(3 answers)
How to scale an image by window size?
(1 answer)
Closed 3 months ago.
I'm trying to make some game similar to Space invaders but they come around from everywhere including sides and bottom and the player can only rotate the ship to shoot lasers at the enemies. Right now, I'm trying to add a background to it to give the game some atmosphere but whatever guides I lookup or try doesn't work. Could anyone show me what I'm supposed to do to get my loaded image onto the background surface? Sorry if dumb question, I'm new to PyGame.
This here is my code so far (without my previous attempts at adding the background)
# Import the Pygame module
import pygame
# Intialize
pygame.init()
# Variables
image = pygame.image.load('Spaceship.gif')
bg = pygame.image.load('bg.gif')
# Setup
pygame.display.set_mode([1000,500])
pygame.display.set_icon(image)
pygame.display.set_caption("Space Man Type Beat")
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
# Quit
pygame.quit()

Related

why is my code really slow and how can i improve it? [duplicate]

This question already has an answer here:
Pygame is running slow
(1 answer)
Closed 2 years ago.
this is my code idk why it runs so slow
why is my code running so slow plz help me!
thank you in advance
import pygame
from rgb import *
from pygame.constants import *
pygame.init()
height=700
width=1500
center=(780, 450)
ball_pos=[500,550]
sky_pos=[0,-1000]
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.update()
running=True
while running:
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if keys[K_SPACE]:
running=False
pygame.display.update()
sky=pygame.image.load("sky.jpg")
if keys[K_RIGHT]:
sky_pos[0]-=1
screen.blit(sky, sky_pos)
class Circle:
pygame.draw.circle(screen, green, ball_pos, 50)
pygame.display.update()
screen.fill(black)
pygame.quit()
I tried doing everything but nothing is working
i want the sky to move to the left with smoothness though i dont know how!
There are some issues in your code, but the most obvious thing is that you are loading the image into the application loop. pygame.image.load is very time consuming, because it has to read the images from the data store.
Load the image once before the application loop, rather than continuously in the loop:
sky=pygame.image.load("sky.jpg") # <--- ADD
running=True
while running:
# [...]
# sky=pygame.image.load("sky.jpg") <--- DELETE

blurring background in pygame [duplicate]

This question already has an answer here:
Blurring in PyGame
(1 answer)
Closed 2 years ago.
As a beginner in pygame, I am trying to build a game and want to blur the background as the game ends and show player his/her score and ask if he/she wants to play again.
I don't know how to blur the background in pygame. Can anyone help me?
Thank You.
To blur the background in PyGame, you can try using the Python Imaging Library (PIL). Here's a link which has sample code on how to do it:
stackoverflow.com/questions/30723253/blurring-in-pygame
Hope this helps!
Maybe try to insert an video that blur at the end unfortunately i dont think there is an blurring function.
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('the bluring video.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
if i find a way to blur the image ill post it. :)

Problem launching pygame on macos find nothing on the internet [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
i have no problem of syntax or any erorr its just a problems when i run the program
import pygame
pygame.init()
# generate window
pygame.display.set_caption("shooter Game")
pygame.display.set_mode((1080, 720))
running = True
while running:
# if the player close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
print("game closed")
sceenshot of the code and the 🚀 of python that not stoping to jump
You don't update the display, which you have to do with
pygame.display.update()
You can add it right after pygame.display.set_mode((1080, 720))
Try like this and tell me if it works :)

background color not changing in python in OS X

i typed the below code in my sublime text, but as this code is used by people to change the background color; i hope it would happen, but, unfortunately it didn't. i.e precisely color didn't change. i
then i started to research in stackoverflow.
some questions like this in (mac os ) has been already asked.
though, you people say that that question will answer my question, it does to some level; but I would love to get the answer of this question. Why does my Tkinter window background not change?
likewise a similar question was asked, like this; Python tkinter window background/canvas not changing colour
import sys
import pygame
def run_game():
#initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Alien Invasion")
#set the background color.
bg_color=pygame.color.Color('#FFFFF')
# start the main loop for 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 sceen during each pass through the loop
screen.fill(bg_color)
#make most recently drawn screen visible
pygame.display.flip()
run_game

image not blitting correctly in pygame [duplicate]

This question already has answers here:
Problems getting pygame to show anything but a blank screen on Macos
(10 answers)
pygame installation issue in mac os
(5 answers)
Closed 2 years ago.
I wanted to blit an image in python, a png image. But for some reason about half the image has the wrong pixel colours, it looks all messy, having random coloured pixels in it. I have made a few games on my Macbook Pro (OSX) using pygame with python 3.4, and I had no trouble when it came to blitting images. Here's the code:
import pygame
import os
w = pygame.display.set_mode((300,300))
bug = os.path.join("/Users/Snyman/Desktop/images/double-block_spm_R.png")
bug = pygame.image.load(bug).convert_alpha()
bug = pygame.transform.scale(bug,(64,64))
loop = True
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
w.fill((255,255,255))
w.blit(bug,(10,10))
pygame.display.update()
pygame.quit()
"bug" is my image

Categories