When R is pressed. Call a function that resets all variables to default [duplicate] - python

This question already has answers here:
Pygame level/menu states
(2 answers)
Reset and restart pygame program doesn't work
(1 answer)
Closed 2 days ago.
I can't make the restart button
please help me, please, I want to make a reset button in my project, but I do not know how, I want, provided that when I press r, all these games are reset, that is, if I have a record of 1500 and press r so that the record becomes 0 again, the project tetris works with pygame, if I need the code, I can throw it off when R is pressed. Call a function that resets all variables to their default values.

You can detect when 'r' is pressed using keyboard events. Here is a small example:
import pygame
gameDisplay = pygame.display.set_mode((800, 600))
def main():
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_r:
print('r pressed!')
# Reset variables here or call a function to reset them
gameDisplay.fill((255, 255, 255))
pygame.display.update()
main()

Related

Pygame Error: video system not initialized (more info in description) [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 2 years ago.
I get same error as this person: pygame.error: video system not initialized, but I have called pg.init().
Two unexpected things happened,
link didnt appear (the background did, and they're in the same directory
after i close the pygame window i get the error mentioned in title
# -*- coding: utf-8 -*-
import pygame as pg
pg.init()
overflate = pg.display.set_mode((500,350))
pg.display.set_caption("Spill")
bg = pg.image.load("castle.jpg")
link = pg.image.load("link.png")
linkx = 0
linky = 0
overflate.blit(link,(linkx, linky))
pg.display.update()
overflate.blit(bg,(0, 0))
pg.display.update()
while True:
for e in pg.event.get():
if e.type == pg.K_w:
spillerx+=10
if e.type == pg.QUIT:
pg.quit()
pg.display.update()
You have to blit the image after drawing the background:
run = True
while run:
for e in pg.event.get():
if e.type == pg.K_w:
spillerx+=10
if e.type == pg.QUIT:
run = False
overflate.blit(bg,(0, 0))
overflate.blit(link,(linkx, linky))
pg.display.update()
pg.quit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Pygame screen crashes misterioslly, I don't idea Why [duplicate]

This question already has an answer here:
Pygame unresponsive display
(1 answer)
Closed 2 years ago.
I Was try to build a game and I was build the interfaces normally, but sundely pygame screen crashed and the pygame screen window becomes unresponsive always I run the a simple program.
import pygame
pygame.init()
pygame.display.init()
pygame.display.set_caption("Yathezz")
tela = pygame.display.set_mode((600,600))
preto = (0,0,0)
def set_telaPartida(tela,nome__jogador,pontos_totais, situacao):
tela.fill(preto)
#if(situacao == 1):
# set_telaLancaDados(tela,nome__jogador,pontos_totais)
# elif(situacao ==2):
# a = 1
# seta tela perguntando o relançamento
#elif(situacao == 3):
# b= 2
#set_telaRelancaDados
while True:
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
I have done some tests and I suspect that the problem are in this line
pygame.display.update()
But I don't have Idea what's happening. Could someone Help me???
You have to handle the events, by either pygame.event.pump() or pygame.event.get(), to keep the window responding. For instance:
run = Ture
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
set_telaPartida(tela,"Lucas",100,1)
#set_telaInst2(tela)
pygame.display.update()
See the documentation of pygame.event.pump():
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. If you are not using other event functions in your game, you should call pygame.event.pump() to allow pygame to handle internal actions.
pygame.event.get() calls pygame.event.pump(). The pygame.QUIT occurs when the close button is pressed.

Why does it say exit status 1? [duplicate]

This question already has answers here:
Pygame window freezes when it opens
(1 answer)
Pygame window not responding after a few seconds
(3 answers)
Closed 2 years ago.
I want the code to work. So that I can get a good grade of it. I have tried a lot of things so that the code can work event.
import pygame.sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.disply .set_caption(AMBERMIR)
WHITE=(255,255,255)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
if event.type == QUIT:
pygame.quit()
sys.exit()
You've to implement a main loop which is continuously running. Inside the main loop, an event loop can be handle the events.
Use pygame.event.get() to get and remove all pending events from the loop. The return value of pygame.event.get() is a list of pygame.event.Event objects.
After drawing the scene, the window has to be updated by either pygame.display.update() or pygame.display.flip():
import sys
import pygame
from pygame.locals import *
pygame.init()
AMBERMIR = "my window"
DISPLAY=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption(AMBERMIR)
WHITE=(255,255,255)
BLUE=(0,0,255)
# main loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == QUIT:
run = False # terminate main loop on QUIT
# clear display
DISPLAY.fill(WHITE)
# draw rectangle
pygame.draw.rect(DISPLAY,BLUE,(200,150,100,50))
# update display
pygame.display.update()
pygame.quit()

Python video system not intilated

Here it is, I dont know what is wrong, I looked at other answers but I still dont know what is wrong?
import pygame
pygame.init()
gameWindow = pygame.display.set_mode((1000,600));
pygame.display.set_caption("Practice")
#game starts
gameActive = True
while gameActive:
for event in pygame.event.get():
#print event
if event.type == pygame.QUIT:
gameActive = False
pygame.quit()
quit()
You have pygame.quit() in your main loop, so after one iteration through the loop you are calling pygame.quit(), which causes pygame to no longer be initialized which creates the error of not having a display surface.
Moving pygame.quit() out of the main while loop fixes the issue.

Why is this tiny pygame program freezing and doing nothing?

This program infinite loops. Does nothing. Won't take input or print anything. Ideas?
import pygame
pygame.init()
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "hi"
running = 0
The problem with your script is solely that there's no window that could capture the events.
You have to create and initialize a window with pygame.display.set_mode first.
import pygame
pygame.init()
# create a window that will capture the events
pygame.display.set_mode((200, 200))
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
print "hi"
running = 0
Try the following:
import pygame, sys
pygame.init()
pygame.mixer.init(22050, -16, 2, 3072)
pygame.mixer.music.load("Kundara_Lake-of-Dust-320.mp3")
pygame.mixer.music.play(1, 0.0)
running = 1
while(running):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.mixer.music.stop()
pygame.quit()
running = 0
From http://www.pygame.org/docs/ref/pygame.html
pygame.quit
Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue. It is safe to call this function more than once: repeated calls have no effect.
Note, that pygame.quit will not exit your program. Consider letting your program end in the same way a normal python program will end.
You are looping infinitely calling pygame.quit() due to while(1).
You need to update the screen. Try it with
screen = pygame.display.set_mode((640,360),0,32)
and in the loop, write
pygame.dispay.flip()
to close the window completely, you can use
sys.exit()
just be sure to include sys in your imports
pygame does not recognize the term 'input' or 'print'. It would be a lot simpler if it did!! Instead, to get text onto the screen, you must use 'drawText('message',font,surface,(xpos,ypos) putting your own text in 'message', your own surface name in 'surface' and the x and y co-ordinates in 'xpos' and 'ypos'.

Categories