I'm working on a small script in Python 2.7.9 and Pygame that would be a small display for our IT department. The idea is that there are several toggle switches that indicate our current status (in, out, etc) , some information about our program at the school, and play a short video that repeats with images of the IT staff etc. I have an older version of Pygame compiled that still allows for pygame.movie to function.
All of the parts of the script work, but when it gets to the end of the .mpg, the movie will not replay until there is an EVENT, like switching our status or moving the mouse. I have tried to define a variable with movie.get_time and call to rewind at a certain time, but the movie will not rewind (currently commented out) . Is there a way to play the movie on repeat without requiring an event, or maybe I could spoof an event after a certain length of time (note that the documentation for pygame.movie is outdated, the loops function does not work) ?
Thank you for the help!
import pygame, sys, os, time, random
from pygame.locals import *
pygame.init()
windowSurface = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
pygame.display.set_caption("DA IT Welcome Sign")
pygame.font.get_default_font()
bg = pygame.image.load('da.jpg')
in_img = pygame.image.load('in.png')
out_img = pygame.image.load('out.png')
etc_img = pygame.image.load('etc.png')
present = in_img
done = False
img = 1
clock = pygame.time.Clock()
movie = pygame.movie.Movie('wallace.mpg')
movie_screen = pygame.Surface(movie.get_size()).convert()
playing = movie.get_busy()
movie.set_display(movie_screen)
length = movie.get_length()
currenttime = movie.get_time()
movie.play()
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
if event.type == pygame.QUIT:
movie.stop()
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
img = 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_2:
img = 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_3:
img = 3
if event.type == pygame.KEYDOWN and event.key == K_w:
pygame.display.set_mode((800, 600))
if event.type == pygame.KEYDOWN and event.key == K_f:
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
if img == 1:
present = in_img
if img == 2:
present = out_img
if img == 3:
present = etc_img
if not(movie.get_busy()):
movie.rewind()
movie.play()
#movie.get_time()
#if currenttime == 25.0:
# movie.stop()
# movie.rewind()
# movie.play()
windowSurface.blit(bg, (0, 0))
windowSurface.blit(movie_screen,(550,175))
windowSurface.blit(present, (0,0))
pygame.display.flip()
You need to take the code for replaying the movie out of the for loop that gets current events. Do this for that code and any other code you want to happen continuously without waiting for an event by moving the code 4 spaces to the left.
Like so:
while not done:
for event in pygame.event.get(): #gets most recent event, only executes code below when there is an event
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
if event.type == pygame.QUIT:
movie.stop()
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
img = 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_2:
img = 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_3:
img = 3
if event.type == pygame.KEYDOWN and event.key == K_w:
pygame.display.set_mode((800, 600))
if event.type == pygame.KEYDOWN and event.key == K_f:
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#code below is out of the event for loop and thus executes whenever the while loop runs through
if img == 1:
present = in_img
if img == 2:
present = out_img
if img == 3:
present = etc_img
if not(movie.get_busy()):
movie.rewind()
movie.play()
#movie.get_time()
#if currenttime == 25.0:
# movie.stop()
# movie.rewind()
# movie.play()
windowSurface.blit(bg, (0, 0))
windowSurface.blit(movie_screen,(550,175))
windowSurface.blit(present, (0,0))
pygame.display.flip()
I have run into this problem too while trying to do things with pygame, it seems to be a common error.
Related
I am new to pygame. I've coded on Python before but never in pygame.
I've made a code which plays a sound when certain keys are clicked and now I tried to make in image moving every time that the user click with his mouse.
import pygame, os, sys
pygame.init()
screen = pygame.display.set_mode((1300,1300))
screen.fill((250,250,250))
img = pygame.image.load(os.path.join(sys.path[0],"fonddecran.v1.jpg"))
screen.blit(img, (0, 0))
pygame.display.flip()
barrel=pygame.image.load(os.path.join(sys.path[0],"barrel-man.jpg"))
pygame.display.flip()
pygame.mixer.init()
it = true
while it:
for event in pygame.event.get():
if event.type == pygame.QUIT:
it=False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 3:
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
elif event.button == 1:
screen.blit(barrel,event.pos)
pygame.display.flip()
elif event.type == pygame.KEYUP:
if event.key == ord('b'):
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
pygame.quit()
Unfortunately, the image just appears one more time every time. How can I delete it so it looks like it has moved?
To be able to "move" the image, you have to redraw the scene in each frame. Add a variable that stores the position of the image. Change the variable to the mouse position when you click the mouse:
import pygame, os, sys
pygame.init()
screen = pygame.display.set_mode((1300,1300))
img = pygame.image.load(os.path.join(sys.path[0],"fonddecran.v1.jpg"))
barrel=pygame.image.load(os.path.join(sys.path[0],"barrel-man.jpg"))
pygame.mixer.init()
barrel_pos = None
it = True
while it:
for event in pygame.event.get():
if event.type == pygame.QUIT:
it=False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 3:
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
elif event.button == 1:
barrel_pos = event.pos
elif event.type == pygame.KEYUP:
if event.key == ord('b'):
pygame.mixer.music.load(os.path.join(sys.path[0],"Mi-mineur.mp3"))
pygame.mixer.music.play()
screen.fill((250,250,250))
screen.blit(img, (0, 0))
if barrel_pos:
screen.blit(barrel, barrel_pos)
pygame.display.flip()
pygame.quit()
I want variable text = [] to append the keyboard input, and then print it.
import pygame
pygame.init()
screen = pygame.display.set_mode([600, 400])
keepGoing = True
def get_text ():
keepText = True
while keepText:
text = [] # I want to add input here
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
keys = pygame.key.name(event.key)
text.append(keys)
print (text)
if event.key == pygame.K_ESCAPE:
keepText = False
while keepGoing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
get_text ()
pygame.display.update()
pygame.quit()
How can I do this?
I think this question is what you're looking for. You'll want something like
while keepText:
text = []
text.append(raw_input('Input something from the keyboard: '))
In your question the indenting looks problematic though, and your while loop will never end.
If a pygame.KEYDOWN event occurs, you can just add its .unicode attribute to a string or append it to a list.
import pygame as pg
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
text = ''
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
text += event.unicode
print(text)
screen.fill((30, 30, 30))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
I am trying to get user keyboard input using pygame. However, the problem is that after I run my code on IDLE, the keyboard input is never read by the program, and whatever I type is shown on the shell. Same issue if I run my code on PyCharm. Any idea? Below is my code:
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == KEYDOWN and event.key == pygame.K_w:
print("Yup!")
pygame.display.flip()
I'm having the exact same issue, also on a Mac using Pycharm and python 3.6. I'm printing the events and only MouseMotion events are recorded, rather than KeyDown.
Edit: apparently it's a known issue: Window does not get focus on OS X with Python 3
This is my code, and it also should work:
while not crashed:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
crashed = True
# get current list
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
print("UP")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
print('this DOES work! :)')
elif event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif pressed[pygame.K_UP]:
print("UP")
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
pygame.display.flip()
x += x_change
gameDisplay.fill(black)
ship(x, y)
pygame.display.update()
clock.tick(60)
This code works perfectly for me
import pygame
pygame.init()
windowSize = width,height = 800,600
screen = pygame.display.set_mode(windowSize)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print("Yup!")
pygame.display.flip()
I am trying to make a fun program with a guy who opens his mouth whenever a press space. The problem is he only opens it for .1 seconds and then it closes again. I want to make it so that the mouth is open whenever i hold space.
Code:
import pygame
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Open The Mouth")
clock = pygame.time.Clock()
faceImg = pygame.image.load("face_shut.png")
faceOpenImg = pygame.image.load("face_open.png")
def face(x,y):
gameDisplay.blit(faceImg,(face_x,face_y))
def faceOpen(x,y):
gameDisplay.blit(faceOpenImg,(faceOpen_x,faceOpen_y))
faceOpen_x = (1)
faceOpen_y = (1)
face_x = (1)
face_y = (1)
programRunning = True
while programRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
The issue appears to be here:
while programRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
# Here, always drawn closed at the end of the while
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
Regardless of what's being set in the while, you are redrawing the face as closed at the end.
How about this instead:
# Drawn before the loop starts
face(face_x,face_y)
pygame.display.update()
while programRunning:
for event in pygame.event.get():
event_occurred = True
if event.type == pygame.QUIT:
programRunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
faceOpen(faceOpen_x,faceOpen_y)
pygame.display.update()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
face(face_x,face_y)
pygame.display.update()
clock.tick(60)
I am kinda stuck with a supposed to be simple code to check if the user has pressed "w" or "s".
Below you can see my code:
import pygame
pygame.init()
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
print('Forward')
elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
print('Backward')
Am I forgetting something here?
Thanks!
A window needs to be created to receive key presses, the following works.
import pygame
import sys
pygame.init()
pygame.display.set_mode((100, 100))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print('Forward')
elif event.key == pygame.K_s:
print('Backward')
This is what seems to me the most simple and understandable way to do it:
import pygame
pygame.init()
pygame.display.set_mode((300, 300))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
print('Forward')
elif event.key == pygame.K_s:
print('Backward')
Instead of using the sys.exit() method I prefer to just use pygame.quit()
it is no good to ask the gamer to keep hitting w to get the response. If you want to read the "pressed" state. You could consider the followings:
from pygame import *
import time
flag = False # The flag is essential.
DONE = False
screen = display.set_mode((500,500)) # 1180, 216
count=0
while not DONE:
event.pump() # process event queue
keys = key.get_pressed() # It gets the states of all keyboard keys.
#print("%d"%count,keys)
count+=1
if keys[ord('w')]: # And if the key is K_DOWN:
print("%d w down"%count)
if keys[ord('s')]: # And if the key is K_DOWN:
print("%d s down"%count)
time.sleep(0.1)
Try this:
import pygame
pygame.init()
key = pygame.key.get_pressed()
while True:
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == pygame.K_w:
print('Forward')
elif event.type == KEYDOWN and event.key == pygame.K_s:
print('Backward')
Sample code to understand how key press events work.
import pygame
from pygame.locals import *
def main():
pygame.init()
pygame.display.set_caption("Move Box With Arrow Keys")
wndsize = (320, 240)
display = pygame.display.set_mode(wndsize)
x = 5
y = 5
black = (0, 0, 0)
white = (255, 255, 255)
rectpos = (x, y)
rectdim = (50, 50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x += 5
print("K_RIGHT")
if event.key == pygame.K_LEFT:
x -= 5
print("K_LEFT")
if event.key == pygame.K_UP:
y -= 5
print("K_UP")
if event.key == pygame.K_DOWN:
y += 5
print("K_DOWN")
# Don't allow coords below 0
if 0 > x:
x = 5
if 0 > y:
y = 5
# Don't allow overflow
if x + rectdim[0] > wndsize[0]:
x = wndsize[0] - rectdim[0] - 5
if y + rectdim[1] > wndsize[1]:
y = wndsize[1] - rectdim[1] - 5
rectpos = (x, y)
display.fill(white)
rect = pygame.draw.rect(display, black, ((rectpos), (rectdim)))
pygame.display.update()
main()
This might work:
from pygame import *
init()
if key.get_pressed()[K_w] == True:
print('Forwards')
if key.get_pressed()[K_s] == True:
print('Backwards')`