Displaying a video in pygame - python

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.

Related

How do I start a pygame app in maximized mode on Linux?

Before I start, this is not a duplicate of How do you start Pygame window maximized?
I am on Linux which the win32gui and win32con libraries do not run on.
What I'm trying to do as stated by the title is maximize my pygame window when it is run. I do not want the game to be fullscreen by using the pygame.FULLSCREEN tag in pygame.display.set_mode(). I want it to be maximized.
My current way of getting it maximized is by creating the window with the pygame.RESIZEABLE flag which allows me to get the pygame.VIDEOEXPOSE event as shown in the example below.
import pygame, sys
SCREEN_WIDTH,SCREEN_HEIGHT = 1280,720
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE, 32, vsync=1)
objects = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.VIDEORESIZE:
SCREEN.blit(pygame.transform.scale(objects, event.dict['size']), (0, 0))
pygame.display.update()
elif event.type == pygame.VIDEOEXPOSE: # handles window minimising/maximising
SCREEN.fill((0, 0, 0))
SCREEN.blit(pygame.transform.scale(objects, SCREEN.get_size()), (0, 0))
pygame.display.update()
pygame.display.update()
The objects surface is simply a surface I draw my game objects to in order to scale them when the window is resized. If you're wondering why I'm not just setting the screen width and height to the size of the screen by getting it using system info is because that way my objects aren't scaled properly. The only way I've been able to get my objects scaled properly is by using the VIDEORESIZE and VIDEOEXPOSE events. If there is a way to automatically press the maximize button or just start maximized in pygame that would be great.
Just found this issue on the pygame github for anyone who needs this. Essentially just use the library that pygame uses to draw windows which is cross platform.
This is the code snippet on the issue which worked for me.
import sys
import pygame
import pygame._sdl2
import time
pygame.init()
screen = pygame.display.set_mode((400, 400), pygame.RESIZABLE)
window = pygame._sdl2.Window.from_display_module()
clock = pygame.time.Clock()
window.maximize()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.display.flip()
clock.tick(30)
I hope this helps anyone having similar issues.

Can't get sound or music in pygame on mac in pycharm working

I can't get pygame to work with music or sounds, I have tried this
import pygame
pygame.init()
pygame.mixer.init()
song = pygame.mixer.Sound('/Users/Me/PycharmProjects/Porting Tester/MusicTest/Hi.mp3')
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Hi")
clock = pygame.time.Clock()
song.play()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
exit()
pygame.display.update()
clock.tick(60)
pygame.quit()
but that causes an error saying:
Python(18309,0x7fff73a5f000) malloc: *** error for object 0x1004dae80: pointer being freed was not allocated ***
set a breakpoint in malloc_error_break to debug
And a return value of
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
I have tried many things but I think it's the file that is giving me these errors.
With this exact code :
import pygame
import os
pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096) # Frequency, channel size, channels, buffersize
pygame.mixer.init()
song = pygame.mixer.Sound('ts.wav')
display = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Hi")
clock = pygame.time.Clock()
song.play()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
exit()
pygame.display.update()
clock.tick(60)
pygame.quit()
I was able to play a file, ts.wav (located in the same folder as the Python file), and it functioned correctly.
The only thing I seem to have done is add pygame.mixer.pre_init(44100, 16, 2, 4096), and move the location of the sound.
If it still doesn't work, I think PyCharm may be to blame. I've used it in the past, and it was weird.
I had the same problem. The way I solved it was converting the original .wav sound files to .ogg (with VLC; online converters work, too).
Not that .wav files are generally bad: I tried a different .wav file and it worked just fine. But my .wav files were somehow bad/corrupted and came out weird from the recording device I had used to make them. Anyway, converting solved the issue.

Pygame display not responding

I'm slowly trying to get to know pygame and write my first game in it and honestly, I didn't expect problems so early. So far I've only set a display, that is supposed to be there indefinitely (I just wanted to try it out):
import pygame
pygame.init()
(width, height) = (1000, 700)
screen = pygame.display.set_mode((width, height))
while True:
pygame.display.flip()
But when the window appears it says it's "not responding". I tried deleting the loop so that display would just blink once and vanish, because programm would die immiedately after it's created, but I get the same "not responding" window. I'm using pygame 1.9.2 and python 3.5. I wonder if the trouble may be because of anaconda - the window is opened as subcart for anaconda by default.
Edit: So far I discovered that when I open it not from spyder, but just click on a file it works just fine. Is there any way to make it work by simple run and compile while in spyder or it's just how it's supposed to work?
Add this to your loop. For me the only time it isnt responding is when I click the X and this could be to do with the fact that pygame doesn't know what to do when that happens.
import sys
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
pygame.quit()
sys.exit()
#Try This
import pygame
(width, height) = (1000, 700)
screen=pygame.display.set_mode((width, height))
pygame.display.update()
while True:
for event in pygame.event.get():``
if event.type == pygame.QUIT:
pygame.quit()
quit()

PyGame any touchscreen

I am trying to create a simple program in which a user can move a shape on the screen using his finger (on a touchscreen).
this is my code so far:
import pygame
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((1000,500),0,32)
WHITE = (255,255,255)
blue = (0,0,255)
DISPLAY.fill(WHITE)
pygame.mouse.set_visible(False)
pygame.draw.rect(DISPLAY, blue,(480,200,50,250))
pygame.display.update()
pygame.mouse.set_pos(480, 200)
while True:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
pygame.draw.rect(DISPLAY, blue, (pos[0]-25,pos[1], 50, 250))
pygame.display. update()
DISPLAY.fill(WHITE)
main()
The problem is that when I touch the screen, nothing will happen until I move my finger. If I print the events I can see that there is no event listed until I start moving my finger, so that is probably the reason.
I want to be able to register the finger press (as an event I guess) on the screen even before it starts to move, is there anyway to do this using PyGame?
Thanks.
In case anyone is stumbling across this later, in pygame 2 there is much better touch support. You can install with
pip install pygame==2.0.0.dev6
(check the pygame github for the most recent version) And there are three new event types. pygame.FINGERDOWN, pygame.FINGERUP, and pygame.FINGERMOTION. The pygame.FINGERUP event registers the touch input right when the screen is touched instead of when the screen is released like pygame.MOUSEBUTTONDOWN
I have made a game that utilises a computer touch screen and manages the touch quite well by handling two types of events. One is pygame.MOUSEMOTION and the other one is pygame.MOUSEBUTTONDOWN. Both of them contain the attribute event.pos. Problem is, at least when it comes to my touch screen (Lenovo), that the initial touch is not registered by the event handler of pygame.
You can confirm this by printing all the events to the terminal while touching your screen. I get nothing until I release or move my finger but on release I get the event pygame.MOUSEBUTTONDOWN rapidly followed by pygame.MOUSEBUTTONUP.
I know that this doesn't really solve your problem, but it might perhaps help you in some way. Otherwise, check out Kivy. There might be a solution there.
import pygame
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((1000,500),0,32)
WHITE = (255,255,255)
blue = (0,0,255)
DISPLAY.fill(WHITE)
pygame.mouse.set_visible(False)
pygame.draw.rect(DISPLAY, blue,(480,200,50,250))
pygame.display.update()
pygame.mouse.set_pos(480, 200)
while True:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN or event.type == MOUSEMOTION:
pos = event.pos
pygame.draw.rect(DISPLAY, blue, (pos[0]-25,pos[1], 50, 250))
pygame.display. update()
DISPLAY.fill(WHITE)
main()

Window resize example from Pygame wiki not working

I took this tutorial from pygame.org which should show how to resize the window properly (image has to be supplied to it, you can use for instance my gravatar). The image should resize to the window, but this doesn't happen with me. Only one VideoResize event is created as soon as I resize the window even so slightly:
<Event(16-VideoResize {'h': 500, 'w': 501, 'size': (501, 500)})>
No other VideoResize events are created (other things like mouse movement or keypresses work). So is the tutorial wrong? Is my computer wrong? What is the proper way of doing it?
I'm running: Python 2.7.5, Pygame 1.9.1, Fedora 20, MATE 1.8.1, Toshiba Satellite.
Here's the code (slightly modified to print the event, but neither the original nor this one work):
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,500), RESIZABLE)
pic = pygame.image.load("example.png")
screen.blit(pygame.transform.scale(pic, (500,500)), (0,0))
pygame.display.flip()
done = False
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == VIDEORESIZE:
print event # show all VIDEORESIZE events
screen = pygame.display.set_mode(event.dict['size'], RESIZABLE) # A
screen.blit(pygame.transform.scale(pic, event.dict['size']), (0,0))
pygame.display.flip()
pygame.display.quit()
If I comment the line # A, then I get plenty of events, but this is the line which resizes the window.
Well I ran the tutorial example with only one change which is I used a pic of a cat called cat.png and it worked fine. The resize is working you just grab a corner and it allows me to adjust it freely with dragging. The picture fills the window whatever size I make it. Have you done other scripts with pygame successfully?

Categories