How do i program a button in python (without Tkinter)? - python

I've recently started learning Python3 and I was trying to make a game with Python3 by importing pygame. I tried to make a menu and I am having some struggles with it. I just tried to make it seem like its a button by letting the rectangle change color when you hover over it but its not working. I already tried some things but its not working. Anyhow here is the full code: hastebin link.
This is the part where I tried to make a button:
def game_intro():
intro = True
gameDisplay.fill(white)
largeText = pygame.font.Font('freesansbold.ttf', 90)
TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
mouse = pygame.mouse.get_pos()
if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
else:
pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))
smallText = pygame.font.Font('freesansbold.ttf' ,20)
textSurf, textRect = text_objects("START!", smallText)
textRect.center = ( (150+(100/2)), (450+(430/2)) )
gameDisplay.blit(textSurf, textRect)
pygame.draw.rect(gameDisplay, red, (550, 430, 100, 50))
pygame.display.update()
clock.tick(15)
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()

The problem here is that you're only doing your mouseover test once, at the start of the function. If the mouse later moves into your rectangle, it won't matter, because you never do the test again.
What you want to do is move it into the event loop. One tricky bit in PyGame event loops is which code you want to run once per event (the inner for event in… loop), and which you only want to run once per batch (the outer while intro loop). Here, I'm going to assume you want to do this once per event. So:
def game_intro():
intro = True
# ... other setup stuff
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
mouse = pygame.mouse.get_pos()
if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
else:
pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))
It looks like some of the other stuff you do only once also belongs inside the loop, so your game may still have some problems. But this should get you past the hurdle you're stuck on, and show you how to get started on those other problems.

Here is a button that should suit your needs:
class Button(object):
global screen_width,screen_height,screen
def __init__(self,x,y,width,height,text_color,background_color,text):
self.rect=pygame.Rect(x,y,width,height)
self.x=x
self.y=y
self.width=width
self.height=height
self.text=text
self.text_color=text_color
self.background_color=background_color
def check(self):
return self.rect.collidepoint(pygame.mouse.get_pos())
def draw(self):
pygame.draw.rect(screen, self.background_color,(self.rect),0)
drawTextcenter(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)
pygame.draw.rect(screen,self.text_color,self.rect,3)
Use the draw function to draw your button, and the check function to see if the button is being pressed.
Implemented into a main loop:
button=Button(x,y,width,height,text_color,background_color,text)
while not done:
for event in pygame.event.get():
if event.type==QUIT:
terminate()
elif event.type==pygame.MOUSEBUTTONDOWN:
if button.check():
#what to do when button is pressed
#fill screen with background
screen.fill(background)
button.draw()
pygame.display.flip()
clock.tick(fps)

This is what i did and it works now:
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
largeText = pygame.font.Font('freesansbold.ttf', 90)
TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
TextRect.center = ((display_width / 2), (display_height / 2))
gameDisplay.blit(TextSurf, TextRect)
mouse = pygame.mouse.get_pos()
# print(mouse)
if 150 + 100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450:
pygame.draw.rect(gameDisplay, bright_green, (150, 450, 100, 50))
else:
pygame.draw.rect(gameDisplay, green, (150, 450, 100, 50))
pygame.draw.rect(gameDisplay, red, (550, 450, 100, 50))
pygame.display.update()
clock.tick(15)
Thank you for the help #abarnert

Related

Why is pygame button not functioning?

I have been following a tutorial from Sentdex to create a button and make it functional. I tried to change it as per my requirement. When I click on the button, I want the function(another screen) to execute. I placed a button in the function(another screen) where I can go back to the main page. But when I click on the button, it goes to the other function only when I clicked the mouse and the output is displayed just until I click the mouse. It does not go to another screen and keeps on staying at initial screen.
import pygame
window = pygame.display.set_mode((1500, 800), pygame.RESIZABLE)
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, action):
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(window, ac, (x, y, w, h))
if click[0] == 1:
action()
else:
pygame.draw.rect(window, ic, (x, y, w, h))
smallText = pygame.font.SysFont(None, 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
window.blit(textSurf, textRect)
def home_intro():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(image, (0,0))
pygame.display.set_caption("PROGRAM")
button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
pygame.display.flip()
home_intro()
pygame.quit()
quit()
I have followed everything as the tutorial. But I don't understand why it does not work. How can I fix this ?
You have to add a variable that stores the current state of the game (game_state ). Change the variable when a button is clicked and draw different scenes depending on the state of the variable:
game_state = "stop"
def start():
global game_state
game_state = "start"
def stop():
global game_state
game_state = "stop"
def home_intro():
global game_state
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(image, (0,0))
button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
if game_state == "start":
# [...]
else:
# [...]
pygame.display.flip()

How Could I Make A Function For The Start Game Button To Disable The Start Screen And Load My Main Game?

so I have a game intro here and I was wonder how can I make it so when my ***mouse clicks on the start game button to disable the the game_intro? its currently not doing anything and vid
like how could I make a function for start button when I click it, it should remove the game_intro or disable it and load my main game
this is my game intro right now
#---------------------------------------------------
def button(msg,x,y,w,h,ic,ac):
mouse = pygame.mouse.get_pos()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
if clikc[0] == 1 and action != None:
action()
else:
pygame.draw.rect(window,ic,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
#------------------------------------------------------
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
largeText = pygame.font.Font('BLOODY.ttf',115)
TextSurf, TextRect = text_objects("Stolen Hearts!", largeText)
TextRect.center = ((800/2), (800/2))
window.blit(TextSurf, TextRect)
# make the square brighter if collideded with the buttons
mouse = pygame.mouse.get_pos()
if 150+120 > mouse[0] > 150 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_green,(150,450,120,50))
else:
pygame.draw.rect(window, green,(150,450,120,50))
if 550+110 > mouse[0] > 550 and 450+50 > mouse[1] > 450:
pygame.draw.rect(window, bright_red,(550,450,110,50))
else:
pygame.draw.rect(window, red,(550,450,110,50))
# ---------------------------------------------------------------------
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Start Game", smallText)
textRect.center = ( (150+(120/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects("Quit Game", smallText)
textRect.center = ( (150+(910/2)), (450+(50/2)) )
window.blit(textSurf, textRect)
pygame.display.update()
clock.tick(15)
#----------------------------------------------------------
my full code script
You have to remove mistakes in button() and you have to use button() in game_intro()
There is too many to explain so I only show minimal working code.
Buttons works but they still are not ideal. It will have problem if new scene will have buttons in the same place - it will click it automatically - but it would need totally different code. More on GitHub
import pygame
# --- constants --- (UPPER_CASE_NAMES)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BRIGHT_RED = (255, 0, 0)
BRIGHT_GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
# --- all classes --- (CamelCaseNames)
class Player:
pass
# ... code ...
class Platform:
pass
# ... code ...
# etc.
# --- all functions --- (lower_case_names)
def text_objects(text, font):
image = font.render(text, True, BLACK)
rect = image.get_rect()
return image, rect
def button(window, 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(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(window, ic,(x,y,w,h))
image = small_font.render(msg, True, BLACK)
rect = image.get_rect()
rect.center = (x+(w/2), y+(h/2))
window.blit(image, rect)
def game_intro():
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
image, rect = text_objects("Stolen Hearts!", large_font)
rect.center = (400, 400)
window.blit(image, rect)
button(window, "Start Game", 150, 450, 120, 50, GREEN, BRIGHT_GREEN, main_game)
button(window, "Quit Game", 350, 450, 120, 50, RED, BRIGHT_RED, quit_game)
pygame.display.update()
clock.tick(15)
def main_game():
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
window.fill((255,255,255))
image, rect = text_objects("Main Game", large_font)
rect.center = (400, 400)
window.blit(image, rect)
button(window, "Intro", 550, 450, 120, 50, GREEN, BRIGHT_GREEN, game_intro)
button(window, "Quit Game", 350, 450, 120, 50, RED, BRIGHT_RED, quit_game)
pygame.display.update()
clock.tick(15)
def quit_game():
print("You quit game")
pygame.quit()
quit()
# --- main ---
pygame.init()
window = pygame.display.set_mode((800, 800))
# it has to be after `pygame.init()`
#small_font = pygame.font.Font("freesansbold.ttf", 20)
#large_font = pygame.font.Font('BLOODY.ttf', 115)
small_font = pygame.font.Font(None, 20)
large_font = pygame.font.Font(None, 115)
clock = pygame.time.Clock()
game_intro()
BTW: I also organize code in different way
all constants values in one place directly after imports and they use UPPER_CASE_NAMES (PEP8) and
all classes in one place - directly after constants and before pygame.init() - and they use CamelCaseNames (PEP8)
all functions in one place - after classes and before pygame.init() - and they uselower_case_names (PEP8)
See: PEP 8 -- Style Guide for Python Code

Pygame.mouse.get_pressed() wont detect most of my clicks

So this is my code :
import pygame
import pygame as pg
pygame.init()
displayWidth = 800
displayHeight = 600
### colour codes ###
bgColour = (245, 230, 255)
grey = (150, 150, 150)
darkGrey = (100, 100, 100)
darkBlack = (0,0,0)
clock = pygame.time.Clock()
mouse_clicked = False
display = pygame.display.set_mode((displayWidth, displayHeight))#sets height and width of screen
pygame.display.set_caption('Fashion!')
def screenDisplay(): #subroutine to display screens
display = pygame.display.set_mode((displayWidth, displayHeight))#sets height and width of screen
pygame.display.set_caption('Fashion!') #sets screen title
display.fill(bgColour)
def text_objects(text, font): #font colour
textSurface = font.render(text, True, darkBlack)
return textSurface, textSurface.get_rect()
def textDisplay(s,t,x,y): #subroutine for displaying text on screen
smallText = pygame.font.SysFont("",s) #creates front and font size
textSurf, textRect = text_objects(t, smallText) #inputs text
textRect.center = (x,y) #centres text
display.blit(textSurf, textRect) #displays test
def button(msg,s,x,y,w,h,ic,ac,action=None): #format for button
mouse = pygame.mouse.get_pos() #gets mouse position (tracks cursor)
click = pygame.mouse.get_pressed() #gets status of mouse (tracks click)
print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y: #draws rectangle for button
pygame.draw.rect(display, ac,(x,y,w,h)) #draws rectangle after colour if mouse is on area
if click[0] == 1 and action != None: #performs function on click
action()
else:
pygame.draw.rect(display,ic,(x,y,w,h)) #draws rectangle initial colour if mouse isnt on area
smallText = pygame.font.SysFont("",s) #adds text to button
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
display.blit(textSurf, textRect)
smallText = pygame.font.SysFont("",s) #adds text to button
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
display.blit(textSurf, textRect)
def menuDisplay():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screenDisplay()
button("WARDROBE",60,200,100,400,100,grey,darkGrey,page)
button("OUTFIT", 60,200,250,400,100,grey,darkGrey,page)
button("SHOPPING",60,200,400,400,100,grey,darkGrey,page)
button("QUIT", 40,300,530,200,50,grey,darkGrey,page)
pygame.display.update()
clock.tick(30)
def page():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screenDisplay()
pygame.display.update()
clock.tick(30)
menuDisplay()
It prints the get pressed but it only returns [0,0,0] until at random times (sometimes straight away sometimes after 10 clicks sometimes after like 50 clicks) it registers [1,0,0]. it has never worked even on other computers.
It didn't work when I didnt have a clock either so like that didnt change anything.
I am so sad and stressed please help 💔🙏
The issue is the call to pygame.display.set_mode in screenDisplay. Note, pygame.display.set_mode reinitialize the window and causes losing all mouse event states.
screenDisplay is called in the main application loop. It is a wast of performance and bad style to initialize the display in every frame.
Do not call screenDisplay in the main application loop, just clear the display by display.fill(bgColour) to solve the issue:
def menuDisplay():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# screenDisplay() <-- DELETE
display.fill(bgColour)
button("WARDROBE",60,200,100,400,100,grey,darkGrey,page)
button("OUTFIT", 60,200,250,400,100,grey,darkGrey,page)
button("SHOPPING",60,200,400,400,100,grey,darkGrey,page)
button("QUIT", 40,300,530,200,50,grey,darkGrey,page)
pygame.display.update()
clock.tick(30)
Do the same in page()
def page():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# screenDisplay() <-- DELETE
display.fill(bgColour)
pygame.display.update()
clock.tick(30)
I changed some things and this works:
import pygame
pygame.init()
displayWidth = 800
displayHeight = 600
### colour codes ###
bgColour = (245, 230, 255)
grey = (150, 150, 150)
darkGrey = (100, 100, 100)
darkBlack = (0,0,0)
smallText = pygame.font.SysFont("",32)
display = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption('Fashion!')
def text_objects(text, font): #font colour
textSurface = font.render(text, True, darkBlack)
return textSurface, textSurface.get_rect()
def button(msg,s,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(display, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(display, ic,(x,y,w,h))
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
display.blit(textSurf, textRect)
def menuDisplay():
display.fill(bgColour)
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
button("WARDROBE",60,200,100,400,100,grey,darkGrey,page)
button("OUTFIT", 60,200,250,400,100,grey,darkGrey,page)
button("SHOPPING",60,200,400,400,100,grey,darkGrey,page)
button("QUIT", 40,300,530,200,50,grey,darkGrey,page)
pygame.display.update()
def page():
display.fill(bgColour)
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.draw.rect(display,grey,(50,50,50,50) )
pygame.display.update()
menuDisplay()

pygame button cannot run

(English isn’t my first language, so please excuse any mistakes.)
(Thanks for everyone!!)
When I click the button"1", BE01 will appear but if I don't click, scene01Img will return.
I tried use gameExit = true in def BE01():, but it doesn't work.
pygame.init()
clock = pygame.time.Clock()
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("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def BE01():
gameDisplay.fill(white)
gameDisplay.blit(BE01Img,(0,0))
button("BACK",350,450,100,50,black,gray,game_intro)
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(introImg,(0,0))
button("START",350,450,100,50,black,gray,game_loop)
pygame.display.update()
clock.tick(15)
def quitgame():
pygame.quit()
quit()
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
gameDisplay.blit(scene01Img,(0,0))
button("1",200,450,100,50,black,gray,BE01)
pygame.display.update()
clock.tick(60)
game_intro()
game_loop()
pygame.quit()
quit()
After click the button"1", BE01 will appear and run the another program, scene01 shouldn't appear.
Every scene should have own loop and when you press button then you should go to new loop.
If you want to change elements in one loop then put old elements in some function (ie. draw_intro) and new elements in other function (ie. draw_other) - at start uses draw_intro in loop and when you press button then replace this function with draw_other
In Python you can assign function name (without ()) to variable
show = print
and later use this name (using ())
show("Hello World")
You can do the same with draw_intro, draw_other.
Inside game loop (before while)you can set
global draw
draw = draw_intro
and use it inside while
draw()
When you press button then it should replace funtion
draw = draw_other
Here full working code - I only removed all blit(image) to run it easily without images.
import pygame
black = (0,0,0)
white = (255,255,255)
gray = (128,128,128)
red = (255,0,0)
pygame.init()
gameDisplay = pygame.display.set_mode( (800,600))
clock = pygame.time.Clock()
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("comicsansms",20)
textSurf = smallText.render(msg, True, red)
textRect = textSurf.get_rect()
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def change_draw(new_draw):
global draw
draw = new_draw
def draw_BE01():
gameDisplay.fill(white)
#gameDisplay.blit(BE01Img,(0,0))
button("BACK",350,450,100,50,black,gray,lambda:change_draw(draw_intro))
def draw_intro():
gameDisplay.fill(white)
#gameDisplay.blit(introImg,(0,0))
button("START",350,450,100,50,black,gray,lambda:change_draw(draw_other))
def draw_other():
gameDisplay.fill(white)
#gameDisplay.blit(scene01Img,(0,0))
button("1",200,450,100,50,black,gray,lambda:change_draw(draw_BE01))
def quitgame():
pygame.quit()
quit()
def game_loop():
global draw
draw = draw_intro
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
EDIT:
If you didn't use lamda before then you can do it without lambda but you will need change_draw_to_intro, change_draw_to_other, change_draw_to_BE01 instead of single change_draw
import pygame
black = (0,0,0)
white = (255,255,255)
gray = (128,128,128)
red = (255,0,0)
pygame.init()
gameDisplay = pygame.display.set_mode( (800,600))
clock = pygame.time.Clock()
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("comicsansms",20)
textSurf = smallText.render(msg, True, red)
textRect = textSurf.get_rect()
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def change_draw_to_intro():
global draw
draw = draw_intro
def change_draw_to_other():
global draw
draw = draw_other
def change_draw_to_BE01():
global draw
draw = draw_BE01
def draw_BE01():
gameDisplay.fill(white)
#gameDisplay.blit(BE01Img,(0,0))
button("BACK",350,450,100,50,black,gray,change_draw_to_intro)
def draw_intro():
gameDisplay.fill(white)
#gameDisplay.blit(introImg,(0,0))
button("START",350,450,100,50,black,gray,change_draw_to_other)
def draw_other():
gameDisplay.fill(white)
#gameDisplay.blit(scene01Img,(0,0))
button("1",200,450,100,50,black,gray,change_draw_to_BE01)
def quitgame():
pygame.quit()
quit()
def game_loop():
global draw
draw = draw_intro
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw()
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()

Weird glitching out Pygame Python images

I have no idea why when the button is pressed it doesnt just go to the next function. It goes back to the other one its really weird and annoying.
It should go to the whattodo function but it does for like half a second then goes to the other. Anyone know why?
#!/usr/bin/python
import pygame
pygame.init()
screen = pygame.display.set_mode((1400,700))
pygame.display.set_caption("Anti-Docter")
titlescreen = pygame.image.load('titleframe.bmp')
boxinfo = pygame.image.load('boxinfo.bmp')
black = (0,0,0)
white = (255,255,255)
randommommycolor = (255,139,12)
Brown = (102,51,0)
Lighter_Brown = (120,59,5)
def mts(text, textcolor, x, y, fs):
font = pygame.font.Font(None,fs)
text = font.render(text, True, textcolor)
screen.blit(text, [x,y])
def buttonPlay(x,y,w,h,ic,ac):
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(screen, ac,(x,y,w,h))
if click[0] == 1:
whattodo()
else:
pygame.draw.rect(screen, ic,(x,y,w,h))
def whattodo():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(boxinfo, (0,0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(titlescreen, (0,0))
buttonPlay(580, 350, 200, 90, Brown, Lighter_Brown)
mts("Play", black, 620, 365, 80)
pygame.display.update()
Every time your main loop repeats, it calls 'buttonPlay'. There is nothing inside 'whattodo' that changes this behaviour, so 'whattodo' runs once then control flow passes back to the main loop, which just repeats 'buttonPlay'.

Categories

Resources