I have some code that creates a button and code creating some text that is displayed on-screen. The goal is to have the button when clicked add 1 to the variable and have the changed reflected on screen (ie button is clicked the value on screen goes from 0 to 1 and then 1 to 2 and so on).
The button function:
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None): #example of color param for line 61/63
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if click[0] ==1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter)) # print to check that the button actually changes the value
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = textBox(message, mediumText)
textRect.center = ( (x + (w/2)), y+(h/2))
gameDisplay.blit(textSurf, textRect)
The logic I have so far
gameDisplay.fill(white)
pygame.display.update()
clickCounter = str(clickCounter)
textObject(clickCounter, black, mediumText, 200, 200)
closeGame = False
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
pygame.display.update()
clock.tick(20)
gameDisplay.fill(white)
pygame.display.update()
#logicloop()
The button function does appear to be changing the value of clickCounter but it is not reflected on screen. My guess was that there is a missing blit command or screen update, though nothing i've tried has worked.
For whatever reason when I had this block of code set up as a function that is then called, it ran without the gameDisplay.fill(white) or any of the other on screen elements appearing, how would I go about setting it up as a function to make adding more to the logic easier?
All of the code:
import pygame
pygame.init()
displayWidth = 700
displayHeight = displayWidth
gameDisplay = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()
black = (0, 0, 0)
brightBlue = (0, 0, 225)
blue = (0, 0, 255)
white = (255, 255, 255) #predef colors to make temp rgb value coding easier
closeGame = False
mediumText = pygame.font.Font("freesansbold.ttf", 70) #initalize font
clickCounter = 0
#def fontSize (pxsize):
#pygame.font.Font("freesandsbold.ttf", pxsize)
def textObject (text, color, font, x, y):
storedGenerate = font.render(text, 1, ((color)))
gameDisplay.blit(storedGenerate, (x,y))
def textBox(text, font): #purely for returning rectangle around font// used for btn function only, tObject for displaying text
textSurface = font.render(text, True, black) #swap black for rgb later
return textSurface, textSurface.get_rect()
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None): #example of color param for line 61/63
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
global clickCounter
clickCounter = 0
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if click[0] ==1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
#smallText = pygame.font.Font("freesansbold.ttf", 20) # constant has been declared, try deleting line when done w/ proj
smallText = pygame.font.SysFont('Times New Roman', 20)
textSurf, textRect = textBox(message, smallText)
textRect.center = ( (x + (w/2)), y+(h/2))
gameDisplay.blit(textSurf, textRect)
gameDisplay.fill(white)
pygame.display.update()
clickCounter = str(clickCounter)
textObject(clickCounter, black, mediumText, 200, 200)
closeGame = False
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
pygame.display.update()
clock.tick(20)
gameDisplay.fill(white)
pygame.display.update()
#logicloop()
pygame.quit()
quit()
The first issue is, that the number value stored in clickCounter is converted to a string.
Delete:
lickCounter = str(clickCounter)
In the function button, the value of clickCounter is continuously set 0. Delete this, too:
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None):
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
global clickCounter
# clickCounter = 0 <------ delete
# [...]
You missed to draw the changed text in the main loop. Before the text is drawn the "old" text has to be cleared. Continuously redraw the entire scene in the main loop of the application.
This means the display has to be filled by the background color and the text and the button have to be redrawn:
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
gameDisplay.fill(white)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
textObject(str(clickCounter), black, mediumText, 200, 200)
pygame.display.update()
clock.tick(20)
Related
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()
I have a variable clickCounter that increases when the mouse button is clicked (when clicked on a button) and held. How do I stop the value from increasing when the button is held and only when it is clicked?
Button function (would it be easier to convert it to a class?):
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None): #example of color param for line 61/63
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
global clickCounter
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if click[0] ==1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
smallText = pygame.font.Font("freesansbold.ttf", 20) # constant has been declared, try deleting line when done w/ proj
textSurf, textRect = textBox(message, smallText)
textRect.center = ( (x + (w/2)), y+(h/2))
gameDisplay.blit(textSurf, textRect)
main loop
closeGame = False
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
gameDisplay.fill(white)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
textObject(str(clickCounter), black, mediumText, 200, 200)
pygame.display.update()
clock.tick(20)
All code:
import pygame
pygame.init()
displayWidth = 700
displayHeight = displayWidth
gameDisplay = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()
black = (0, 0, 0)
brightBlue = (0, 0, 225)
blue = (0, 0, 255)
white = (255, 255, 255) #predef colors to make temp rgb value coding easier
closeGame = False
mediumText = pygame.font.Font("freesansbold.ttf", 70) #initalize font
clickCounter = 0
#def fontSize (pxsize):
#pygame.font.Font("freesandsbold.ttf", pxsize)
def textObject (text, color, font, x, y):
storedGenerate = font.render(text, 1, ((color)))
gameDisplay.blit(storedGenerate, (x,y))
def textBox(text, font): #purely for redturning rectangle around font// used for btn function only, tObject for displaying text
textSurface = font.render(text, True, black) #swap black for rgb later
return textSurface, textSurface.get_rect()
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None): #example of color param for line 61/63
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
global clickCounter
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if click[0] ==1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
smallText = pygame.font.Font("freesansbold.ttf", 20) # constant has been declared, try deleting line when done w/ proj
textSurf, textRect = textBox(message, smallText)
textRect.center = ( (x + (w/2)), y+(h/2))
gameDisplay.blit(textSurf, textRect)
closeGame = False
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
gameDisplay.fill(white)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
textObject(str(clickCounter), black, mediumText, 200, 200)
pygame.display.update()
clock.tick(20)
gameDisplay.fill(white)
pygame.display.update()
#logicloop()
pygame.quit()
quit()
My inital thought was to not use pygame.mouse.get_pressed() and instead use mousebutton down but was unable to get it to function properly. Any help is appreciated.
Evaluate the MOUSEBUTTONDOWN event instead of pygame.mouse.get_pressed. The event occurs a single time, when the mouse button is pressed.
Store the mouse button which was pressed to a variable mousebutton:
while not closeGame: # this keeps the window from shutting down
mousebutton = 0
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
elif thing.type == pygame.MOUSEBUTTONDOWN:
mousebutton = thing.button
Pass the variable to the function button:
button(mousebutton, "Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
Evaluate if the left button was pressed in the fucntion:
def button(mousebutton, message, x, y, w, h, activeRGB, inactiveRGB, action=None):
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
global clickCounter
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if mousebutton == 1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
# [...]
The .button attribute of the MOUSEBUTTONDOWN event returns 1 for the left button, 2 for the middle button, 3 fro the right button respectively 4 when the mouse wheel is rolled up, and 5 when the mouse wheel is rolled down.
I am writing a code for a paint app, and I want to have multiple brushes. Only issue right now is that with this code here, the brush works fine, but it only works when the cursor is above the actual icon. Here is the code:
def paintScreen():
intro = True
gameDisplay.fill(cyan)
message_to_screen('Welcome to PyPaint', black, -300, 'large')
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.draw.rect(gameDisplay, white, (50, 120, displayWidth - 100, displayHeight - 240))
button('X', 20, 20, 50, 50, red, lightRed, action = 'quit')
icon(airbrushIcon, white, 50, displayHeight - 101, 51, 51, white, grey, 'airbrush')
pygame.display.update()
def icon(icon, colour, x, y, width, height, inactiveColour, activeColour, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:#if the cursor is over the button
pygame.draw.rect(gameDisplay, activeColour, (x, y, width, height))
gameDisplay.blit(icon, (x, y))
if click[0] == 1 and action != None:
if action == 'quit':
pygame.quit()
quit()
if action == 'pencil':
pencil()
if action == 'airbrush':
airbrush()
if action == 'calligraphy':
calligraphy()
if action == 'erase':
pencil()
else:
pygame.draw.rect(gameDisplay, inactiveColour, (x, y, width, height))
gameDisplay.blit(icon, (x, y))
def airbrush(brushSize = 3):
airbrush = True
cur = pygame.mouse.get_pos() #cur[0] is x location, cur[1] is y location
click = pygame.mouse.get_pressed()
if click[0] == True:
if cur[0] > 50 < displayWidth - 50 and cur[1] > 120 < displayHeight - 120:
#the area of the canvas is x(50, width-50) y(120, width-120)
pygame.draw.circle(gameDisplay, black, (cur[0] + random.randrange(brushSize), cur[1] + random.randrange(brushSize)), random.randrange(1, 5))
clock.tick(60)
I recognise that the issue is with the function only being called when the cursor is above the icon, but I don't know where to move the action statements, or how to change them.
You want to make it so when the user clicks on the paint icon a variable is set. So instead of:
def icon(icon, colour, x, y, width, height, inactiveColour, activeColour, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:#if the cursor is over the button
pygame.draw.rect(gameDisplay, activeColour, (x, y, width, height))
gameDisplay.blit(icon, (x, y))
if click[0] == 1 and action != None:
if action == 'quit':
pygame.quit()
quit()
if action == 'pencil':
pencil()
if action == 'airbrush':
airbrush()
if action == 'calligraphy':
calligraphy()
if action == 'erase':
pencil()
else:
pygame.draw.rect(gameDisplay, inactiveColour, (x, y, width, height))
gameDisplay.blit(icon, (x, y))
You can change this code to simply toggle paint on and off for:
def icon(icon, colour, x, y, width, height, inactiveColour, activeColour, paint_on, action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y and click[0] == 1: # if the cursor is over the button and they clicked
if paint_on == True:
paint_on = False
else:
paint_on = True
return paint_on
Obviously in your case since you have multiple tools you would have to create different toggles for each tool inside this function, but I'm trying to keep it simple and show an example for just a single paint tool.
Now that you have a toggle that will change the variable "paint_on" upon clicking the icon, you can check for just a regular mouse click
def regular_click(colour, x, y, width, height, inactiveColour, activeColour, action = None):
cur = pygame.mouse.get_pos()
click = pygame.get_pressed()
if cur[1] > y and click[0] == 1 and paint_on == True: # if cursor is beneath the tool bar (I'm assuming your tool bar is at the top)
pygame.draw.rect(gameDisplay, activeColour, (x, y, width, height))
Then adding this function to your main while True loop:
def paintScreen():
intro = True
gameDisplay.fill(cyan)
message_to_screen('Welcome to PyPaint', black, -300, 'large')
paint_on = False
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.draw.rect(gameDisplay, white, (50, 120, displayWidth - 100, displayHeight - 240))
button('X', 20, 20, 50, 50, red, lightRed, action = 'quit')
paint_on = icon(airbrushIcon, white, 50, displayHeight - 101, 51, 51, white, grey, paint_on, 'airbrush')
regular_click(paint_on)
pygame.display.update()
So all up this code works as follows:
Upon user clicking the icon, it changes the variable "paint_on" to it's opposite (so on if off or off if on), then when they click anywhere, it checks whether this variable is on, and if the curso is not in the tool bar, and if both of these are met, then it draws.
This is how you would do this. I can not guarantee that this code works as I have never used pygame myself, but I know this is the best way of doing this kind of thing, unless there is some sort of in built function.
I'm trying to make a pause function I've tested this pause function separately and it seems to be working fine although there is nothing that it can do at the moment but it still shows. However, when I implement it in my game it doesn't work?
I've put it in different places before the while loop and in the while loop. When it's before the while loop the pause function will show first then mess up the game. When it's put in the while loop, it wont show at all even when 'p' is pressed.
What is the issue?
import pygame
import os
import sys
pygame.init()
pygame.font.init()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0, 0)
clock = pygame.time.Clock()
FPS = 60
DS = pygame.display.set_mode((900, 600))
sprite_image = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.circle(sprite_image, (90, 100, 200), [25, 25], 25)
W, H = DS.get_size()
pause = True
st = pygame.font.SysFont("comicsans ms", 20)
lf = pygame.font.SysFont("comicsans ms", 120)
def paused():
DS.fill((50, 50, 70)) # Fill the display surface.
text_surf, text_rect = text_objects('Paused', (W/2, H/2), lf)
DS.blit(text_surf, text_rect)
# The buttons are dictionaries now.
resume_button = button('Resume', 150, 450, 100, 50, WHITE, WHITE)
quit_button = button('Quit', 350, 450, 100, 50, WHITE, WHITE)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Should be pygame.quit not QUIT.
sys.exit() # Better use sys.exit() instead of quit().
elif event.type == pygame.MOUSEMOTION:
# Switch the active surface of the buttons
# if we're hovering over them.
for btn in (resume_button, quit_button):
if btn['rect'].collidepoint(event.pos):
btn['active_surface'] = btn['surface_hover']
else:
btn['active_surface'] = btn['surface']
elif event.type == pygame.MOUSEBUTTONDOWN:
# Check if the buttons were clicked.
if resume_button['rect'].collidepoint(event.pos):
return # Return to the main loop.
elif quit_button['rect'].collidepoint(event.pos):
pygame.quit()
sys.exit()
def text_objects(text, pos, font):
text_surface = font.render(text, True, BLACK)
return text_surface, text_surface.get_rect(center=pos)
def display_message(text):
lf = pygame.font.SysFont("comicsans", 120)
TextSurf, TextRect = text_objects(text, lf)
TextRect.center = ((W / 2)), ((H / 2))
DS.blit(TextSurf, TextRect)
pygame.display.update()
def button(msg, x, y, w, h, inactive_color, active_color):
"""Create a button dictionary with the images and a rect."""
text_surf, text_rect = text_objects(msg, (w/2, h/2), st)
# Create the normal and hover surfaces and blit the text onto them.
surface = pygame.Surface((w, h))
surface.fill(inactive_color)
surface.blit(text_surf, text_rect)
surface_hover = pygame.Surface((w, h))
surface_hover.fill(active_color)
surface_hover.blit(text_surf, text_rect)
return {'active_surface': surface,
'surface': surface,
'surface_hover': surface_hover,
'rect': pygame.Rect(x, y, w, h),
}
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
super(Platform, self).__init__()
self.image = pygame.Surface((w, h))
self.image.fill((90, 90, 120))
self.rect = self.image.get_rect(topleft=(x, y))
class Sprite(pygame.sprite.Sprite):
def __init__(self, pos):
super(Sprite, self).__init__()
self.image = sprite_image
self.rect = self.image.get_rect()
self.rect.center = pos
self._vx = 0
self._vy = 0
self._spritex = pos[0]
self._spritey = pos[1]
self.level = None
self._gravity = .9
def update(self):
# Adjust the x-position.
self._spritex += self._vx
self.rect.centerx = self._spritex # And update the rect.
block_hit_list = pygame.sprite.spritecollide(self, self.level, False)
for block in block_hit_list:
if self._vx > 0:
self.rect.right = block.rect.left
elif self._vx < 0:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
self._spritex = self.rect.centerx # Update the position.
# Adjust the y-position.
self._vy += self._gravity # Accelerate downwards.
self._spritey += self._vy
self.rect.centery = self._spritey # And update the rect.
# Check and see if we hit anything
if block_hit_list == pygame.sprite.spritecollide(self, self.level, False):
for block in block_hit_list:
# Reset our position based on the top/bottom of the object.
if self._vy > 0:
self.rect.bottom = block.rect.top
elif self._vy < 0:
self.rect.top = block.rect.bottom
self._spritey = self.rect.centery # Update the position.
# Stop our vertical movement
self._vy = 0
def jump(self):
self.rect.y += 3
platform_hit_list = pygame.sprite.spritecollide(self, self.level, False)
self.rect.y -= 3
# If it is ok to jump, set our speed upwards
if len(platform_hit_list) > 0 or self.rect.bottom >= H:
self._vy = -17
def go_left(self):
# Called when the user hits the left arrow.
self._vx = -3
def go_right(self):
#Called when the user hits the right arrow.
self._vx = 3
def stop(self):
# Called when the user lets off the keyboard.
self._vx = 0
sprite = Sprite([60, 60])
active_sprite_list = pygame.sprite.Group(sprite)
sprite.level = pygame.sprite.Group()
# A list with rect style tuples (x, y, width, height).
rects = [
(20, 20, 150, 20), (120, 200, 200, 20), (400, 440, 300, 20),
(40, 100, 200, 20), (320, 300, 150, 20), (520, 550, 300, 20),
(600, 100, 150, 20), (300, 200, 200, 20), (290, 440, 300, 20),
(40, 100, 200, 20), (320, 300, 150, 20), (95, 550, 300, 20),
(250,300,150,20), (350,100,150,20), (450,50,270,20), (700,185,70,20),
(650,350,350,20)
]
# You can unpack the rect tuples directly in the head of the for loop.
for x, y, width, height in rects:
# Create a new platform instance and pass the coords, width and height.
platform = Platform(x, y, width, height)
# Add the new platform instance to the sprite groups.
sprite.level.add(platform)
active_sprite_list.add(platform)
active_sprite_list.add(sprite.level)
done = False
while not done:
def paused():
DS.fill((50, 50, 70)) # Fill the display surface.
text_surf, text_rect = text_objects('Paused', (W / 2, H / 2), lf)
DS.blit(text_surf, text_rect)
# The buttons are dictionaries now.
resume_button = button('Resume', 150, 450, 100, 50, WHITE, WHITE)
quit_button = button('Quit', 350, 450, 100, 50, WHITE, WHITE)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Should be pygame.quit not QUIT.
sys.exit() # Better use sys.exit() instead of quit().
elif event.type == pygame.MOUSEMOTION:
# Switch the active surface of the buttons
# if we're hovering over them.
for btn in (resume_button, quit_button):
if btn['rect'].collidepoint(event.pos):
btn['active_surface'] = btn['surface_hover']
else:
btn['active_surface'] = btn['surface']
elif event.type == pygame.MOUSEBUTTONDOWN:
# Check if the buttons were clicked.
if resume_button['rect'].collidepoint(event.pos):
return # Return to the main loop.
elif quit_button['rect'].collidepoint(event.pos):
pygame.quit()
sys.exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
pause = True
paused()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
sprite.go_left()
pause = False
if event.key == pygame.K_RIGHT:
sprite.go_right()
pause = False
if event.key == pygame.K_UP:
sprite.jump()
pause = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and sprite._vx < 0:
sprite.stop()
pause = False
if event.key == pygame.K_RIGHT and sprite._vx > 0:
sprite.stop()
# If the player gets near the right side, shift the world left (-x)
if sprite.rect.right > W:
sprite.rect.right = W
# If the player gets near the left side, shift the world right (+x)
if sprite.rect.left < 0:
sprite.rect.left = 0
active_sprite_list.update()
DS.fill(WHITE)
# Blit the sprite's image at the sprite's rect.topleft position.
active_sprite_list.draw(DS)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
Take a look at this example. I use dictionaries for the buttons which consist of the images, the currently active image and a rect which is used for the collision detection and as the position. If the user clicks the resume button, I just return to the main game loop.
import sys
import pygame
pygame.init()
WHITE = (255, 255, 255)
GRAY = pygame.Color("gray30")
BLACK = (0, 0, 0, 0)
clock = pygame.time.Clock()
FPS = 60
DS = pygame.display.set_mode((900, 600))
W, H = DS.get_size()
# Define the fonts once when the program starts.
st = pygame.font.SysFont("comicsans ms", 20)
lf = pygame.font.SysFont("comicsans ms", 120)
def paused():
DS.fill((50, 50, 70)) # Fill the display surface.
text_surf, text_rect = text_objects('Paused', (W/2, H/2), lf)
DS.blit(text_surf, text_rect)
# The buttons are dictionaries now.
resume_button = button('Resume', 150, 450, 100, 50, WHITE, GRAY)
quit_button = button('Quit', 350, 450, 100, 50, WHITE, GRAY)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Should be pygame.quit not QUIT.
sys.exit() # Better use sys.exit() instead of quit().
elif event.type == pygame.MOUSEMOTION:
# Switch the active surface of the buttons
# if we're hovering over them.
for btn in (resume_button, quit_button):
if btn['rect'].collidepoint(event.pos):
btn['active_surface'] = btn['surface_hover']
else:
btn['active_surface'] = btn['surface']
elif event.type == pygame.MOUSEBUTTONDOWN:
# Check if the buttons were clicked.
if resume_button['rect'].collidepoint(event.pos):
return # Return to the main loop.
elif quit_button['rect'].collidepoint(event.pos):
pygame.quit()
sys.exit()
# Draw the active surfaces at the rects of the buttons.
DS.blit(resume_button['active_surface'], resume_button['rect'])
DS.blit(quit_button['active_surface'], quit_button['rect'])
pygame.display.update()
clock.tick(15)
def text_objects(text, pos, font):
text_surface = font.render(text, True, BLACK)
return text_surface, text_surface.get_rect(center=pos)
def button(msg, x, y, w, h, inactive_color, active_color):
"""Create a button dictionary with the images and a rect."""
text_surf, text_rect = text_objects(msg, (w/2, h/2), st)
# Create the normal and hover surfaces and blit the text onto them.
surface = pygame.Surface((w, h))
surface.fill(inactive_color)
surface.blit(text_surf, text_rect)
surface_hover = pygame.Surface((w, h))
surface_hover.fill(active_color)
surface_hover.blit(text_surf, text_rect)
return {'active_surface': surface,
'surface': surface,
'surface_hover': surface_hover,
'rect': pygame.Rect(x, y, w, h),
}
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused()
DS.fill((50, 50, 50))
pygame.draw.circle(DS, (0, 90, 190), (100, 100), 50)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
I'm making a game and the game has a shop which you can buy tokens and if you click the button to buy them the mouse goes crazy..
This is a function which waits for the mouse click.
def button(text, x, y, width, height, inactive_color, active_color, action):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1:
if action == "buy_slowdown":
slowdown_powerup += 1
else:
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
text_to_button(text, black, x, y, width, height)
Here is where the function is called:
def shop():
shop = True
while shop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
# BUY SLOWDOWN TOKENS
buyslowdownlabel = shopfont.render("Slowdown Tokens", 1, blue)
slowdown_string_label = smallfont.render(str(slowdown_powerup), 1, black)
gameDisplay.blit(buyslowdownlabel, [340, 190])
pygame.draw.rect(gameDisplay, grey, [380, 235, 75, 75])
gameDisplay.blit(slowdown_string_label, [410.5, 255.5])
button("Buy", 380, 320, 100, 70, dark_yellow, yellow, "buy_slowdown")
pygame.display.update()
Use event.MOUSEBUTTONDOWN instead of pygame.mouse.get_pressed() because event.MOUSEBUTTONDOWN is created only once when button changes state from not-pressed into pressed. pygame.mouse.get_pressed() is True all the time when you keep pressed - and because computer is faster than you then it can check pygame.mouse.get_pressed() thousands time when you click button.
#!/usr/bin/env python3
import pygame
# --- constants --- (UPPER_CASE names)
WHITE = (255,255,255)
BLACK = ( 0, 0, 0)
DARK_YELLOW = (200,200, 0)
YELLOW = (255,255, 0)
# --- functions ---
def button_create(text, rect, inactive_color, active_color, action):
font = pygame.font.Font(None, 40)
button_rect = pygame.Rect(rect)
text_buy = font.render(text, True, BLACK)
text_buy_rect = text_buy.get_rect(center=button_rect.center)
return [text_buy, text_buy_rect, button_rect, inactive_color, active_color, action, False]
def button_check(info, event):
text, text_rect, rect, inactive_color, active_color, action, hover = info
if event.type == pygame.MOUSEMOTION:
# hover = True/False
info[-1] = rect.collidepoint(event.pos)
elif event.type == pygame.MOUSEBUTTONDOWN:
if hover and action:
action()
def button_draw(screen, info):
text, text_rect, rect, inactive_color, active_color, action, hover = info
if hover:
color = active_color
else:
color = inactive_color
pygame.draw.rect(screen, color, rect)
screen.blit(text, text_rect)
# ---
def buy_slowdown(number=1):
global slowdown_powerup
slowdown_powerup += number
print('slowdown_powerup:', slowdown_powerup)
def buy_1():
buy_slowdown(1)
def buy_10():
buy_slowdown(10)
def buy_100():
buy_slowdown(100)
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((800,600))
screen_rect = screen.get_rect()
# - objects -
slowdown_powerup = 0
button_1 = button_create("+1", (380, 235, 75, 75), DARK_YELLOW, YELLOW, buy_1)
button_2 = button_create("+10", (480, 235, 75, 75), DARK_YELLOW, YELLOW, buy_10)
button_3 = button_create("+100", (580, 235, 75, 75), DARK_YELLOW, YELLOW, buy_100)
# - mainloop -
shop = True
while shop:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
shop = False
button_check(button_1, event)
button_check(button_2, event)
button_check(button_3, event)
# --- draws ---
screen.fill(WHITE)
button_draw(screen, button_1)
button_draw(screen, button_2)
button_draw(screen, button_3)
pygame.display.update()
# - end -
pygame.quit()