How to use pygame and vlc with supervisor? [duplicate] - linux

I'm having a problem. I want to load and play a video in pygame but it doesn't start. The only thing that I am seeing is a black screen. Here is my code:
import pygame
from pygame import display,movie
pygame.init()
screen = pygame.display.set_mode((1024, 768))
background = pygame.Surface((1024, 768))
screen.blit(background, (0, 0))
pygame.display.update()
movie = pygame.movie.Movie('C:\Python27\1.mpg')
mrect = pygame.Rect(0,0,140,113)
movie.set_display(screen, mrect.move(65, 150))
movie.set_volume(0)
movie.play()
Can you help me??

The pygame.movie module is deprecated and not longer supported.
If you only want to show the video you can use MoviePy (see also How to be efficient with MoviePy):
import pygame
import moviepy.editor
pygame.init()
video = moviepy.editor.VideoFileClip("video.mp4")
video.preview()
pygame.quit()
An alternative solution is to use the OpenCV VideoCapture. Install OpenCV for Python (cv2) (see opencv-python). However, it should be mentioned that cv2.VideoCapture does not provide a way to read the audio from the video file.
This is only a solution to show the video but no audio is played.
Opens a camera for video capturing:
video = cv2.VideoCapture("video.mp4")
Get the frames per second form the VideoCapture object:
fps = video.get(cv2.CAP_PROP_FPS)
Create a pygame.time.Clock:
clock = pygame.time.Clock()
Grabs a video frame and limit the frames per second in the application loop:
clock.tick(fps)
success, video_image = video.read()
Convert the camera frame to a pygame.Surface object using pygame.image.frombuffer:
video_surf = pygame.image.frombuffer(video_image.tobytes(), video_image.shape[1::-1], "BGR")
See also Video:
Minimal example:
import pygame
import cv2
video = cv2.VideoCapture("video.mp4")
success, video_image = video.read()
fps = video.get(cv2.CAP_PROP_FPS)
window = pygame.display.set_mode(video_image.shape[1::-1])
clock = pygame.time.Clock()
run = success
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
success, video_image = video.read()
if success:
video_surf = pygame.image.frombuffer(
video_image.tobytes(), video_image.shape[1::-1], "BGR")
else:
run = False
window.blit(video_surf, (0, 0))
pygame.display.flip()
pygame.quit()
exit()

You are not actually blitting it to a screen. You are also not utilizing a clock object so it will play as fast as possible. Try this:
# http://www.fileformat.info/format/mpeg/sample/index.dir
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('MELT.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()
I just got that MELT.MPG from the link provided in the comment. You should be able to simply switch out that string for your actual MPG you want to play and it will work... maybe.

As you probably know, the pygame.movie module is deprecated and no longer exists in the latest version of pygame.
An alternative would be to read in frames of the video one by one and blit them onto the pygame screen using the 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()

Related

Pygame gamepad won't run anymore with an Xbox 360 controller and an Ubuntu machine [duplicate]

This code goes into infinite loop. I cant use A button on xbox 360 controller
import pygame
from pygame import joystick
pygame.init()
joystick = pygame.joystick.Joystick(0)
pygame.joystick.init()
print("start")
while True:
if joystick.get_button(0) == 1 :
print("stoped")
break
I cant use A button on xbox 360 controller
Personnaly, I can, so this seems to be possible. You are just missing that pretty much every user input needs to be updated by pygame through pygame.event.get().
From the pygame documentation:
Once the device is initialized the pygame event queue will start receiving events about its input.
So, apparently you need to get the events in the while loop like such to make the joystick work:
import pygame
from pygame.locals import *
pygame.init()
joystick = pygame.joystick.Joystick(0)
while True:
for event in pygame.event.get(): # get the events (update the joystick)
if event.type == QUIT: # allow to click on the X button to close the window
pygame.quit()
exit()
if joystick.get_button(0):
print("stopped")
break
Also,
In the line if joystick.get_button(0) == 1, you don't need to type == 1 because the statement is already True.
You are initializing pygame.joystick twice: through the line pygame.init() and pygame.joystick.init().
You don't need to type from pygame import joystick because you already already have it in the line import pygame.
You can take this as reference and use it in your own way.
import pygame
import sys
pygame.init()
pygame.joystick.init()
clock = pygame.time.Clock()
WIDTH,HEIGHT = 500,500
WHITE = (255,255,255)
BLUE = (0,0,255)
BLUISH = (75,75,255)
YELLOW =(255,255,0)
screen = pygame.display.set_mode((WIDTH,HEIGHT))
smile = pygame.image.load("smile.jpg")
smile = pygame.transform.scale(smile,(WIDTH,HEIGHT))
idle = pygame.image.load("idle.jpg")
idle = pygame.transform.scale(idle,(WIDTH,HEIGHT))
joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.JOYBUTTONDOWN:
if event.button == 0: #press A button to smile
screen.fill(WHITE)
screen.blit(smile,(0,0))
pygame.display.update()
clock.tick(10)
elif event.type == pygame.JOYBUTTONUP:
if event.button == 0:
screen.fill(WHITE)
screen.blit(idle,(0,0))
pygame.display.update()
clock.tick(10)

What can I do to make my PyGame window respond?

I am trying to make a window open every time I run my code, but every time I run the code, it shows the window as unresponsive.
My code is as follows:
import os
import time
import sys
pygame.font.init()
WIDTH, HEIGHT = 500, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")
BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")), (WIDTH, HEIGHT))
def main():
fps = 60
clock = pygame.time.Clock()
def redraw_window():
WIN.blit(BG, (0,0))
pygame.display.update()
while True:
clock.tick(fps)
redraw_window()
for event in pygame.event.get():
if event.type == sys.exit:
sys.exit()
input()
main()
I've already tried a few thing with event.get and event.pump, I'm probably missing something very basic. Could someone tell me what else I could try?
Also, I'm new at pygame, so if anyone sees another mistake, please tell me about. I would greatly appreciate it.
The window is unresponsive because you don't handle the events by calling pygame.event.get().
While pygame.event.get() is in your code, it's never executed because
a) it's in a function you never call
b) your code blocks on the input() call
You're code should look like this:
import os
pygame.init()
WIDTH, HEIGHT = 500, 400
def main():
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("EscapeGame")
BG = pygame.transform.scale(pygame.image.load(os.path.join("GameMap1.png")).convert_alpha(), (WIDTH, HEIGHT))
fps = 60
clock = pygame.time.Clock()
while True:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
WIN.blit(BG, (0,0))
pygame.display.flip()
main()

Blit function not working for displaying an image

I've currently been following along with a video over pygame and creating a space invaders type of game that I will modify for my own implementation of the game. However I've gotten to the section where you have to use the blit function in order to display an image for the background of the game and it's not working for me. Once the program is executed, the window is launched but without the image displayed for the background. I believe that I have properly loaded the image and stepped into the function that will use the blit function for displaying the background image as well. Please let me know if I have overlooked anything, any help will be greatly appreciated.
import pygame
import os
import time
import random
#Setting window frame
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Space Battle")
#Image Loading
RED_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))
#Ship for player
YELLOW_SPACESHIP = pygame.image.load(os.path.join("assets", "pixel_ship_yellow.png"))
#Laser loading
YELLOW_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_yellow.png"))
RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
GREEN_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_green.png"))
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))
Here is the code for loading in the back ground image
#Loading Backgrnd Img
BG = pygame.image.load(os.path.abspath("assets/background-black.png"))
formatedBG = pygame.transform.scale(BG, (WIDTH,HEIGHT))
Here is the code for the main function and handling background display.
def window_Redraw():
WINDOW.blit(formatedBG, (0,0))
pygame.display.update()
def main():
run = True
FPS = 60
clock = pygame.time.Clock()
while run:
clock.tick(FPS)
window_Redraw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
main()
Here is a screenshot of the window after execution:
enter image description here
Your example is much more complicated that it should be. RED_SPACESHIP, BLUE_SPACESHIP... etc are irrelevant in this case and you should remove them.
You are displaying only a black background - nothing else, so it should be displayed. In order to check if everything is working, start from this example:
import pygame
# initialize the pygame module
pygame.init()
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
formatedBG = pygame.Surface((WIDTH, HEIGHT))
formatedBG.fill(pygame.color.Color(255, 0, 0, 0))
# BG = pygame.image.load(os.path.abspath("assets/background-black.png"))
# formatedBG = pygame.transform.scale(BG, (WIDTH, HEIGHT))
run = True
while run:
WINDOW.blit(formatedBG, (0, 0))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
And then uncomment the background to check whether the background changed to black color.
My previous version of pygame which was 1.9.4 was causing my initial issues with displaying the background image using blit and the problem with the window not being able to display any color with the fill function. I solved these issues by running the command 'pip install==2.0.0.dev4' to upgrade pygame. Everything seems to be working properly now.

show picture in pygame

i am trying to show an image on pygame. but it turns out pygame.error: Couldn't open aircarrier.jpeg. what does it mean? how can i solve it? here is my code
import pygame
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
aircraftcarrier = pygame.image.load("aircarrier.jpeg")
screen.blit(aircraftcarrier, [100,100])
pygame.display.update()
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
pygame.quit()
By defauly, pygame can only load uncompressed BMP images. It must be built with full image support for the pygame.image.load() function to be able to support jpg and other file formats.
You can test if pygame is built with extended image formats through the function, pygame.image.get_extended(). This returns True if extended image formats can be loaded.
You can read more about loading extended image formats here.

Pygame screen flickering

I am quite new to Python and currently trying to make my first game with Pygame. I just started and got a problem. My screen is flickering white (fill colour) to black. I tried to lower the tick (even to 1), but it didn't help (actually, it was flickering less, but still visible a lot). I tried to find any help online, but everywhere "display.update / flip used more than once per cycle" was suggested as a problem and it didn't fix mine.
I am currently running a Fedora 20 box, with Nvidia graphic card (and nouveau drivers), if the issue is connected with this.
My code:
import pygame
from pygame.locals import *
class visualisation(object):
def __init__(self):
pygame.init()
pygame.time.Clock().tick()
self.window = pygame.display.set_mode((0, 0), pygame.FULLSCREEN, 0)
pygame.mouse.set_visible(False)
pygame.display.set_caption('MY GAME')
self.background = pygame.Surface(self.window.get_size())
self.background = self.background.convert()
self.background.fill((250, 250, 250))
self.window.blit(self.background, (0, 0))
pygame.display.update()
if __name__ == '__main__':
running = True
while(running):
arcade = visualisation()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pygame.quit()
Thanks a lot in advance :)
You are calling the following functions in a loop:
pygame.init()
pygame.display.set_mode((0, 0), pygame.FULLSCREEN, 0)
If I am not mistaken this creates a new screen variable which is very bad.
You probably wanted to create a new arcade at the start of the game, instead of on each frame.
Try moving it before the loop:
running = True
arcade = visualisation()
while(running):
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False

Resources