Creating a Buzzer System using python - python

We are having a holiday party at work, and are trying to set up a Family Feud game. With that, I'm trying to program a buzzer system. I am very new to pygame so maybe there is a better approach to what I'm trying to do.
I've written the following code which works to some degree. Right now, it recognizes the button and displays the picture as it should; however, it is recognizing all button presses where I only want it to recognize the first until after it is reset. For example, the left side buzzes in first, I want their picture to be displayed - then if the right team buzzes in after, I want that button to be ignored. Then if a third (reset) button is pushed, it resets back to the beginning to begin tracking for the first button pushed again. Any help would be greatly appreciated!
import pygame
import pdcurses
#import RPi.GPIO as GPIO
import image
import time
import clock
from pygame import mixer
from pygame.locals import *
displayWidth = 1600
displayHeight = 1200
pygame.init()
#mixer.init()
#pygame.display.init()
screen = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption('Family Feud')
pygame.display.update()
def reset():
global screen
kids = pygame.image.load("kids.jpg")
screen.blit(kids, (0,0))
pygame.display.update()
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_LEFT:
leftBuzzer = pygame.image.load("ice cream.jpg")
screen.blit(leftBuzzer,(0,0))
pygame.display.update()
if event.key == K_RIGHT:
rightBuzzer = pygame.image.load("snowman.jpg")
screen.blit(rightBuzzer,(0,0))
pygame.display.update()
if event.key == K_q:
pygame.quit()
if event.key == K_r:
reset()

You could add an alreadyPressed boolean and for each buzzer press have an if statement check alreadyPressed before displaying anything.

Related

pygame sprite not moving across screen when updates

I am trying to move my sprite across the screen when the window starts.... It moves for a second than stops for a while than it starts back up again. its not giving me an error.... So im really not sure whats going on here.... anyway here is the code.... any info is needed!
thanks,
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
sky_surface = pygame.image.load("bg_desert.png")
snail_surface = pygame.image.load("snailWalk1.png")
snail_x_pos = 600
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
snail_x_pos -=1
screen.blit(sky_surface,(0,0))
screen.blit(snail_surface,(snail_x_pos,350))
pygame.display.update()
Your loop will only execute when there are events. When there are no events, you don't move. If you wiggle your mouse in front of the screen, for example, you'll see pretty continuous movement.
You need to use pygame.time.set_timer to force an event for you to update.
It looks like the final 4 lines you have written are designed to run on each cycle of the for loop, however they will only run when an event occurs, because they are indented a block too far.
Your new code should look like this:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
sky_surface = pygame.image.load("bg_desert.png")
snail_surface = pygame.image.load("snailWalk1.png")
snail_x_pos = 600
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
snail_x_pos -=1
screen.blit(sky_surface,(0,0))
screen.blit(snail_surface,(snail_x_pos,350))
pygame.display.update()

why wont the image blit?

I have tried putting the fill color before blit but it won't work.
im using python 3.8.2, windows 10, and pygame 1.9.6.
please help
btw im following this tutorial (im new to pygame)
and here's my code
import pygame
#initialize
pygame.init()
#screen
screen = pygame.display.set_mode((800, 600))
#things
pygame.display.set_caption("space invaders 1.0")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
#player settings
playerimg = pygame.image.load('space-invaders.png')
playerx = 370
playery = 430
#define player
def player():
screen.blit(playerimg, (playerx, playery))
#loop
running = True
while running:
#rgb
screen.fill((10, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT():
running = False
player()
pygame.display.update()
#end process
pygame.quit()
exit()
ill answer any questions
(side note it crashes when I press "x" and exits when I press '-' idk why)
pygame.QUIT is not a function - it's a constant, simply an integer representing a QUIT event. You can't do if event.type == pygame.QUIT(), you need to do if event.type == pygame.QUIT, without parenthesis.
You also don't really need to call pygame.quit() and exit() at the end of the program - both pygame and the program itself will be terminated when the program reaches its end.
Apart from that your program works for me, images are showing. Make sure your image is visible over a black background and if it is and you are still having problems I'll try to help you with them.

Sound effects do not play for a few seconds after being pressed for 4 consecutive times?

Pygame sound effects do not play for a few seconds if the corresponding buttons (to play the sound effect) is pressed for four consecutive times.
I'm currently trying to experiment with different keys of the piano.
I've tried removing the pygame clock object.
I've tried making the Sound.play() into a function, as found in the code.
import pygame
pygame.init()
###LordKeys###
A5 = pygame.mixer.Sound('PianoKeys/A5.wav')
A6 = pygame.mixer.Sound('PianoKeys/A6.wav')
def A56():
A5.play()
A6.play()
###############################################
run = True
win = pygame.display.set_mode((700,700))
pygame.display.set_caption("Piano Gen")
while run:
win.fill((255,255,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
A56()
pygame.display.update()
Expected Results: the sound effect to play when the key is pressed, regardless of frequency.
You have to use the Sound function to play the sound. The sound variable you have created does not have a play() function.
Replace
A5.play()
A6.play()
with
pygame.mixer.Sound.play(A5)
pygame.mixer.Sound.play(A6)
So I decided to play the sounds on a channel and that solved the problem!
def A56():
channel1.play(A5)
channel1.play(A6)

Why does the image not show up on the screen when I blit it?

Using Python (and Pygame), I have been creating a short one-screen game and I code each part in a different window. In my Home Screen, when I blit the play button onto the screen, it doesn't appear. I am new to Python and Pygame. This is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1352,638))
pygame.display.set_caption("Termination: Part 1")
bg = True
playButton = pygame.image.load("Play Button.png")
mouse = pygame.mouse.get_pos()
def playButtonFunction():
if background == pygame.image.load("Home Screen.png"):
background.blit(playButton(533.5,278))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
bg = False
screen.blit(background,(0,0))
if bg:
background = pygame.image.load("Intro Screen.png")
else:
background = pygame.image.load("Home Screen.png")
playButtonFunction()
pygame.display.update()
As Frédéric Hamidi already said in a comment, the line
if background == pygame.image.load("Home Screen.png")
will not work as you probably expect.
You should probabkly pass a flag to that method or don't call that function at all when you don't want to show that playButton image.
Also, the line
background.blit(playButton(533.5,278))
will throw an exception, it should look like
background.blit(playButton, (533, 278))
So, change your code to
...
if bg:
background = pygame.image.load("Intro Screen.png")
else:
background = pygame.image.load("Home Screen.png")
screen.blit(background,(0,0))
if !bg:
background.blit(playButton, (533, 278))
...
Another issue that you load the images from disk every iteration of your game loop (with pygame.image.load). It's enough to load each image once.

Why does my simple pygame lag? [duplicate]

This question already has answers here:
Lag when win.blit() background pygame
(2 answers)
Closed 2 years ago.
I have been making a simple python game using pygame and after I added the feature of switching guns the game started to lag. I have no idea why it is lagging. I have tried rebooting but it didn't work. The code is really short so maybe it is just my computer, but if there is anything that may help run it faster please let me know. Here is the code:
import sys, pygame, pygame.mixer
from pygame.locals import *
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Blue Screen of Death')
#variables
x = 100
y = 200
gun_type = "gun1"
gun = pygame.image.load("gun1.png")
gun = pygame.transform.scale(gun,(500,250))
gun_sound = pygame.mixer.Sound("gun_sound.wav")
clock = pygame.time.Clock()
while 1:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:sys.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
gun_sound.play()
elif event.type == KEYDOWN and event.key == K_1:
gun = pygame.image.load("gun1.png")
gun = pygame.transform.scale(gun,(500,250))
gun_type = "gun2"
elif event.type == KEYDOWN and event.key == K_2:
gun = pygame.image.load("gun2.png")
gun = pygame.transform.scale(gun,(500,250))
gun_type = "gun2"
elif event.type == KEYDOWN and event.key == K_TAB:
if gun_type == "gun2":
gun_type = "gun2_aimed"
elif gun_type == "gun2_aimed":
gun_type = "gun2"
elif gun_type == "gun2_aimed":
gun = pygame.image.load("gun2_aimed.png")
gun = pygame.transform.scale(gun,(500,250))
#frames per second
clock.tick(60)
hallway = pygame.image.load("hallway.png")
hallway = pygame.transform.scale(hallway,(600,400))
screen.blit(hallway,(0,0))
screen.blit(gun,(mx-100,y))
pygame.display.flip()
Thanks for your help.
This is probably the most important thing you can learn in Pygame.
For many years, I've had lagging issues in Pygame. I was frustrated, and almost switched to Pyglet. My game was only running at 9 fps.
Then I found some Pygame documentation on my computer. It had some advice from David Clark, and he suggested you add .convert_alpha() at the end of all Pygame image loads. This increased my framerate to 32!
Here's the website:
https://www.pygame.org/docs/tut/newbieguide.html
I always just create a function to do it for me, so I don't have to keep typing '.convert_alpha()' too many times:
def loadify(imgname):
return pygame.image.load(imgname).convert_alpha()
Just replace pygame.image.load( to loadify( when using this function.
Have fun with Pygame!
You could try to load the images of your guns before the while loop and save a reference to them, this way you don't have to load the image on the fly every time.
Don't call pygame.image.load from your event handler.
Instead, call it on all of your resources at startup and just switch out which one you use.

Categories