die timer anwendung, wie sie zu sehen ist funktioniert für den einzelfall schon richtig gut (verbesserungsvorschläge wären super)
jetzt versuche ich auf den 3 buttons auch 3 verschiedene funktionen laufen zu lassen.
verschiedene laufzeiten funktionieren schonmal ABER die ausgeführte funktion ist immer die gleiche und bis jetzt ist jeder versuch gescheitert, das zu ändern.
dazu möcht ich noch sagen das ich erst ein anfänger in der programmierung bin.
Code: Alles auswählen
import pygame as pg
import sys
import gfx
import sfx
from pygame.locals import*
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
pg.init()
show = gfx.Image()
play = sfx.Sound()
clock = pg.time.Clock()
fps = 60
screen = pg.display.set_mode((1024, 768), 0, 32)
pg.display.set_caption("timed funktion")
button1 = (409, 362)
button2 = (409, 449)
button3 = (408, 537)
timer_pos = button1
lenght = height = 70
########## timer #####################
wait1 = 3
wait2 = 4
wait3 = 5
pg.time.set_timer(USEREVENT + 1, 1000)
class Timer(object): # macht timer zu einem objekt
hour = 0
min = 0
sec = 0
timerun = 0
def print_timer(self):
return print("(%.2d:%.2d:%.2d)" % (start_timer.hour, start_timer.min, start_timer.sec))
def show_timer(self): # ausgabe auf sceen
fontmy = pg.font.Font("fonts/miso.ttf", 36)
text01 = fontmy.render("Timer:" + str("%.2d:%.2d:%.2d" %
(start_timer.hour, start_timer.min, start_timer.sec)), True, white)
screen.blit(text01, timer_pos)
pg.display.update()
def inc_timer(self, x, dummy):
dummy.sec = x
if x >= 60:
dummy.min, dummy.sec = divmod(x, 60)
if dummy.min >= 60:
dummy.hour, dummy.min = divmod(dummy.min, 60)
return start_timer
def counter(self):
start_timer.sec -= 1
if start_timer.hour > 0 > start_timer.sec and start_timer.min == 0:
start_timer.hour -= 1
start_timer.min += 60
if start_timer.min > 0 > start_timer.sec:
start_timer.min -= 1
start_timer.sec += 60
dummytimer.print_timer()
if start_timer.hour == 0 and start_timer.min == 0 and start_timer.sec == 0:
print("countdown has endet -> start function")
play.click3()
Timer.timerun = 0
start_timer = Timer() # objekte der klasse Timer
load_timer = Timer()
about_timer = Timer()
dummytimer = start_timer
######### mainloop ####################
game = True
while game:
keys = pg.key.get_pressed()
for event in pg.event.get():
if Timer.timerun == 1:
if event.type == USEREVENT + 1: # counter tick
dummytimer.counter()
if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
game = False
sys.exit()
if event.type == MOUSEBUTTONDOWN:
mx, my = event.pos
if (button1[0] < mx < button1[0]+lenght) and (button1[1] < my < button1[1]+height):
if start_timer.sec == 0:
timer_pos = button1
dummytimer.inc_timer(wait1, start_timer)
dummytimer.print_timer()
Timer.timerun = 1
if (button2[0] < mx < button2[0]+lenght) and (button2[1] < my < button2[1]+height):
if start_timer.sec == 0:
timer_pos = button2
dummytimer.inc_timer(wait2, start_timer)
dummytimer.print_timer()
Timer.timerun = 1
if (button3[0] < mx < button3[0]+lenght) and (button3[1] < my < button3[1]+height):
if start_timer.sec == 0:
timer_pos = button3
dummytimer.inc_timer(wait3, start_timer)
dummytimer.print_timer()
Timer.timerun = 1
screen.blit(show.start, (0, 0))
if Timer.timerun == 1: # blit nur wenn timer aktiv
dummytimer.show_timer()
pg.display.update()
clock.tick(fps)
pg.quit()