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. :)
Related
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()
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
I just found out about pygame.surface.scroll() and what I understand from the pygame documents that scroll() is for moving surface without the need to rebuild the background again to cover the old surface, just like pygame.rect.move_ip() but for surfaces.
Anyway, I don't know how to use it and the examples in the pygame documents are hard to me to understand as long as I am beginner and, after searching for long time, I couldn't found anything useful to understand how to use it.
Here is my code.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
screen.blit(image,(boxx,boxy))
pygame.display.update()
clock.tick(60)
EDIT: Your image and screen variables are backwards. That is also causing you some confusion I'm sure..
Your problem may is that you are trying to scroll an all black background. It is probably scrolling, and you just don't know it because the white box you used blit() to draw on the screen is stationary.
Try using something you can see scroll, like an image file. If you wanna move the white box, you can add a counter as a speed variable. Read this, then run it.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
speed = 5 # larger values will move objects faster
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
# I did modulus 720, the surface width, so it doesn't go off screen
screen.blit(image,((boxx + speed) % 720, (boxy + speed) % 720))
pygame.display.update()
clock.tick(60)
I can't say for sure the scroll function is working or not, learn to use an image as your background so you can see it moving first.
This question already has answers here:
How to load and play a video in pygame
(3 answers)
Closed 1 year ago.
I want to play a video as a background in a pygame window, and draw some images on it, like this:
http://imgur.com/a/qRiEe
However, since pygame no longer has pygame.movie module I'm wondering what could be the alternative way of playing video in pygame window.
You can use moviepy module to display videos now since pygame.movie is removed. MoviePy will display the video with pygame. Here's an example:
from moviepy.editor import VideoFileClip
import pygame
pygame.display.set_caption('My video!')
clip = VideoFileClip('myvideo.mp4')
clip.preview()
pygame.quit()
You can also visit their docs for mor explanation
You can use the cv2 module (OpenCV) that can be installed with the command prompt command:
pip install opencv-python
Then, you can run the code:
import cv2
import pygame
cap = cv2.VideoCapture('video.mp4')
success, img = cap.read()
shape = img.shape[1::-1]
wn = pygame.display.set_mode(shape)
clock = pygame.time.Clock()
while success:
clock.tick(60)
success, img = cap.read()
for event in pygame.event.get():
if event.type == pygame.QUIT:
success = False
wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "BGR"), (0, 0))
pygame.display.update()
pygame.quit()
I followed what was done in the question in the following link (How to load and play a video in pygame)
But what actually happens is a smaller window pops up with a black screen, video doesnt play.
Video is in .mpg
def game_credits():
Creds = pygame.movie.Movie("C:\Users\itzrb_000\Documents\gameCreds.mpg")
screen = pygame.display.set_mode(Creds.get_size())
Creds_screen = pygame.Surface(Creds.get_size()).convert()
Creds.set_display(Creds_screen)
Creds.play
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(Creds_screen, (0, 0))
pygame.display.update()
clock.tick(50)
Thanks for reading
For new readers, please note, that the Movie class, has been removed from pygame.
You could try the moviepy or pyglet modules. But it's not integrated to pygame.