MoviePy preview showing besides screen blit - python

I've got a problem.
I'm creating the Game Menu which i want to be:
background as video file (using MoviePy)
buttons on the video
The problem is video is showing, but the button1 not.
Please help...
from moviepy.editor import *
import pygame
from pygame import mixer
import os
import cv2
print(os.getcwd())
# Initialization the PyGame
pygame.init()
# Create the screen
WIN = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# Menu Background
background = pygame.image.load('Assets/background.png')
button1 = pygame.image.load('Assets/button.png')
# Background SOUND
mixer.music.load('Assets/mainmenumusic.mp3')
mixer.music.set_volume(0.2)
mixer.music.play(-1)
# Game TITLE and ICON
pygame.display.set_caption("Game Launcher") # title of the game
icon = pygame.image.load('Assets/icon.png') # define the icon variable
pygame.display.set_icon(icon) # set the window icon to the icon variable
# FPS Lock
FPS = 60
clip = VideoFileClip('Assets/particles.mp4', audio=False)
# Game loop
clock = pygame.time.Clock()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
WIN.blit(button1, (960, 540))
clip.preview(fullscreen=True)
pygame.display.update()

The button is not showing because you blit it before the background video so the video covers it. You can just try with:
clip.preview(fullscreen=True)
WIN.blit(button1, (960, 540))

Related

Pygame - adding background image

I am new to python and created a small game. I want to add a background image, but is is not working. The image is not displayed.
i tried it with the following code:
background = pygame.image.load("images\\background.png")
screen.blit(background,(0,0))
The code in the question does not really illustrate the problem. But if the code is generating the error:
background = pygame.image.load('images/background.png')
pygame.error: Couldn't open images/background.png
Then it is simply that PyGame can't find the specified image file.
However I expect that your code is simply not "flushing" the updates to the window / screen with a call to pygame.display.flip()
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
### MAIN
pygame.init()
SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), SURFACE )
pygame.display.set_caption("Background Image")
background_image = pygame.image.load('images/grass.png')
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# Movement keys
keys = pygame.key.get_pressed()
if ( keys[pygame.K_UP] ):
print("up")
#elif ...
# Update the window, but not more than 60fps
window.blit( background_image, ( 0,0 ) )
pygame.display.flip() # <-- Flush drawing ops to the screen
# Clamp FPS
clock.tick_busy_loop( 60 )
pygame.quit()
Note the 4th-last line.
Take a look at this page on StackOverflow. It has multiple solutions that you can use. I'll write one of them here.
You can make an image the size of your game window, then before anything else is displayed, blit it to (0,0).
bg = pygame.image.load("background.png")
#INSIDE OF THE GAME LOOP
gameDisplay.blit(bg, (0, 0))
#OTHER GAME ITEMS
I hope this helps you!

pygame not displaying my image

i started learning pygame and i followed some tutorials to make simple hello world project and it works but when i do it my self trying to display my image on the window nothing happen!
this is my code
__author__ = 'mohammed'
import sys
import pygame
import color
# -----------setup------------------------
pygame.init() # start pygame
screensize = (800, 600) # variable that we will use to declare screen size
screen = pygame.display.set_mode(screensize) # set the screen size
pad = pygame.image.load('2.png')
x = 100
y = 100
# -----------setup------------------------
# -------------------main loop------------
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(pad, (x, y))
screen.fill(red)
pygame.display.update()
i am importing my file that contain colors and their rgb :
red = (255, 0, 0)
It looks like you're filling the screen after the image is drawn, covering the image. Try switching the order of the rows:
screen.blit(pad, (x, y))
and
screen.fill(red)

How To Have A Logo Flash Before The Game Starts?

Is there any way that before my game starts i could show a logo of some sort? For example when Sonic The Hedgehog first starts you seethe SEGA logo before the actual game starts! If this is possible it would be great!
Thanks!
at the begining of your game, just blit your logo.
import pygame
import time
import os, sys
print 'Splash load...'
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((500,80),pygame.NOFRAME)
background = pygame.Surface(screen.get_size())
background.fill((2,24,244))
screen.blit(background, (0,0))
screen.blit(pygame.font.Font('pala.ttf', 72).render('Loading...', 1, (255,255,255)), (90,10))
pygame.display.update()
time.sleep(5)
its as easy as that

How to open camera with pygame in Windows?

I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6

How to detect resizeable window state in pygame and de-maximize it in Linux?

I have an application built in python with use of pygame that initially displays a login screen at a set size which is not RESIZABLE and then when user logs into the game the saved settings are being used to transform the window size. The window is turned into RESIZABLE after login. If user logs out the window is changed back to the initial size without RESIZABLE flag. Everything is OK as long as the user logs out from normal window, but when user hits the maximize button and then logs out in some distros the window still stays maximized and the login screen is being painted in a top left corner of the window.
And here comes the question, is there a way of detecting whether the window has been maximized so I can de-maximize it before sizing down?
I couldn't find anything that would help me with this in the pygame docs or anywhere online. I have found a way of getting a "handle" to the window by using:
pygame.display.get_wm_info()['window']
but not sure where to take it from here.
The way I set the sizes:
self.screen = pygame.display.set_mode((800, 480)) #login screen
self.screen = pygame.display.set_mode(user_saved_size, pygame.RESIZABLE) #game screen
get_wm_info()['wmwindow'] gives you windowID in Windows Manager (X.org) but it is "outside" of PyGame. Maybe with python library Xlib you could do something.
EDIT:
I tried example in Setting the window dimensions of a running application to change terminal size and it works but it don't change PyGame window size. I tried xlib to get PyGame window caption and it works but I could not set PyGame window caption.It seems PyGame doesn't respect new caption.
I use this code to test PyGame window caption - it can get caption but it can't set caption.
import sys
import pygame
from pygame.locals import *
import Xlib
import Xlib.display
WIDTH, HEIGHT = 1500, 300
pygame.init()
screen = pygame.display.set_mode((800,600),0,32)
print "wm_info:", pygame.display.get_wm_info()
print " window:", pygame.display.get_wm_info()['window']
print "fswindow:", pygame.display.get_wm_info()['fswindow']
print "wmwindow:", pygame.display.get_wm_info()['fswindow']
display = Xlib.display.Display()
root = display.screen().root
#windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
#print "Xlib windowID:", windowID
#window = display.create_resource_object('window', windowID)
window = display.create_resource_object('window', pygame.display.get_wm_info()['window'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib window get_wm_name():", window.get_wm_name()
window = display.create_resource_object('window', pygame.display.get_wm_info()['fswindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib fswindow get_wm_name():", window.get_wm_name()
window = display.create_resource_object('window', pygame.display.get_wm_info()['wmwindow'])
window.configure(width = WIDTH, height = HEIGHT)
print "Xlib wmwindow get_wm_name():", window.get_wm_name()
print
print "Xlib wmwindow set_wm_name(hello world of xlib)"
window.set_wm_name("hello world of xlib")
display.sync()
print "Xlib wmwindow get_wm_name():", window.get_wm_name()
# --------------
fpsClock = pygame.time.Clock()
RUNNING = True
while RUNNING:
for event in pygame.event.get():
if event.type==QUIT:
RUNNING = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
RUNNING = False
fpsClock.tick(25)
# --------------
pygame.quit()
sys.exit()
I use this code to change window size - it works in terminal and DreamPie (python shell):
# https://unix.stackexchange.com/questions/5999/setting-the-window-dimensions-of-a-running-application
WIDTH, HEIGHT = 1500, 300
import Xlib
import Xlib.display
display = Xlib.display.Display()
root = display.screen().root
windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
window = display.create_resource_object('window', windowID)
window.configure(width = WIDTH, height = HEIGHT)
display.sync()
#windowIDs = root.get_full_property(display.intern_atom('_NET_CLIENT_LIST'), Xlib.X.AnyPropertyType).value
#for windowID in windowIDs:
# window = display.create_resource_object('window', windowID)
# name = window.get_wm_name() # Title
# pid = window.get_full_property(display.intern_atom('_NET_WM_PID'), Xlib.X.AnyPropertyType) # PID

Categories