Reputation: 11
My first few buttons work fine but my CSTWindow one only works sometimes. I am reluctant to post as I am self taught and the code wont be flawless, any advice would be much appreciated on how to make this button work all the time and not just every 10ish clicks. Parts of my code is below. Again I am self taught it is not perfect!
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.SysFont("Ariel", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
gameDisplay.blit(background, (x,y))
largeText = pygame.font.SysFont("Arial",45)
TextSurf, TextRect = text_objects("Crime Scene and Evidence Examination Game", largeText)
TextRect.center = ((display_width/2),(display_height/1.2))
gameDisplay.blit(TextSurf, TextRect)
button("Scene Selection",150,540,100,50,blue,bright_blue,game_loop)
button("Quit",550,540,100,50,red,bright_red,quitgame)
pygame.display.update()
def CSTWindow():
CSTWindow = True
while CSTWindow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print("Crime Scene")
def TutorialWindow():
tutorial = True
while tutorial:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen = pygame.display.set_mode((display_width,display_height))
screen.fill(white)
screen.blit(TutorialBG, (x,y))
button("Crime Scene",150,540,100,50,blue,bright_blue,CSTWindow)
pygame.display.update()
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(blue)
button("Tutorial",(370), (100),100, 50, lightBlue, bright_blue,TutorialWindow)
pygame.display.update()
clock.tick(60)
Upvotes: 1
Views: 43
Reputation: 27
Maybe it will work better with an image, this is what i wrote:
def button(x, y, w, h, inactive, active, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
gameDisplay.blit(active, (x, y))
if click[0] == 1 and action is not None:
action()
else:
gameDisplay.blit(inactive, (x, y))
Btw, I watched the same tutorial as you but I used images instead.
Upvotes: 1