How can I make a button that goes to website in pygame? - python

I have a "Credits" menu in my pygame and I'd like to make some buttons that go to certain websites.
What I mean is, when the button is clicked, it should open up, for example, GitHub (or whatever the link is).
Is there a way I can achieve this?

Implement a Button class ans use the webbrowser module to open an URL
import webbrowser
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos) and event.button == 1:
webbrowser.open(self.url)
See also Pygame mouse clicking detection and How can I open a website in my web browser using Python?
Minimal example:
import pygame
import webbrowser
class Button(pygame.sprite.Sprite):
def __init__(self, x, y, w, h, font, text, action):
super().__init__()
text_surf = font.render(text, True, (0, 0, 0))
self.button_image = pygame.Surface((w, h))
self.button_image.fill((96, 96, 96))
self.button_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.hover_image = pygame.Surface((w, h))
self.hover_image.fill((96, 96, 96))
self.hover_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
pygame.draw.rect(self.hover_image, (96, 196, 96), self.hover_image.get_rect(), 3)
self.image = self.button_image
self.rect = pygame.Rect(x, y, w, h)
self.action = action
def update(self, event_list):
hover = self.rect.collidepoint(pygame.mouse.get_pos())
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if hover and event.button == 1:
self.action()
self.image = self.hover_image if hover else self.button_image
pygame.init()
window = pygame.display.set_mode((500, 300))
clock = pygame.time.Clock()
font50 = pygame.font.SysFont(None, 50)
button1 = Button(50, 40, 200, 60, font50, "Pygame",
lambda : webbrowser.open('https://www.pygame.org/news'))
group = pygame.sprite.Group(button1)
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update(event_list)
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
exit()

Related

How do I create separate buttons in a pygame loop? [duplicate]

Pls suggest how can I change the color of the button when I pressed it, and the color of the first button will be changed to the default color when I pressed the second button.
For example, After I clicked the STRAIGHT Button, the button will become green color and when I click the LEFT button the LEFT button will change to green color and the STRAIGHT button will become the default color which is white color. Thanks in advance :)
CODE:
def draw_button(self):
global clicked
action = False
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
if button_rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
clicked = True
pygame.draw.rect(screen, self.click_col, button_rect)
elif pygame.mouse.get_pressed()[0] == 0 and clicked == True:
clicked = False
action = True
else:
pygame.draw.rect(screen, self.hover_col, button_rect)
else:
pygame.draw.rect(screen, self.button_col, button_rect)
When you draw the button, you have to set the color dependent on the global variable clicked:
def draw_button(self):
global clicked
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
hover = button_rect.collidepoint(pos)
if hover and pygame.mouse.get_pressed()[0] == 1:
clicked = not clicked
color = self.button_col
if clicked:
color = self.click_col
elif hover:
color = self.hover_col
pygame.draw.rect(screen, color, button_rect)
Anyway, that won't satisfy you, because pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down.
You have to use MOUSEBUTTONDOWN event. The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked.
If you have multiple buttons that you have to interact with each other, a single clicked status is not enough. You need a separate "clicked" state for each button. If the clicked state of 1 button becomes True, the states of the other keys must be set to False. I recommend to implement a RadioButton class for this.
See also Mouse and Sprite.
Minimal example:
repl.it/#Rabbid76/PyGame-RadioButton
import pygame
class RadioButton(pygame.sprite.Sprite):
def __init__(self, x, y, w, h, font, text):
super().__init__()
text_surf = font.render(text, True, (0, 0, 0))
self.button_image = pygame.Surface((w, h))
self.button_image.fill((96, 96, 96))
self.button_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.hover_image = pygame.Surface((w, h))
self.hover_image.fill((96, 96, 96))
self.hover_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
pygame.draw.rect(self.hover_image, (96, 196, 96), self.hover_image.get_rect(), 3)
self.clicked_image = pygame.Surface((w, h))
self.clicked_image.fill((96, 196, 96))
self.clicked_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.image = self.button_image
self.rect = pygame.Rect(x, y, w, h)
self.clicked = False
self.buttons = None
def setRadioButtons(self, buttons):
self.buttons = buttons
def update(self, event_list):
hover = self.rect.collidepoint(pygame.mouse.get_pos())
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if hover and event.button == 1:
for rb in self.buttons:
rb.clicked = False
self.clicked = True
self.image = self.button_image
if self.clicked:
self.image = self.clicked_image
elif hover:
self.image = self.hover_image
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font50 = pygame.font.SysFont(None, 50)
radioButtons = [
RadioButton(50, 40, 200, 60, font50, "option 1"),
RadioButton(50, 120, 200, 60, font50, "option 2"),
RadioButton(50, 200, 200, 60, font50, "option 3")
]
for rb in radioButtons:
rb.setRadioButtons(radioButtons)
radioButtons[0].clicked = True
group = pygame.sprite.Group(radioButtons)
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update(event_list)
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
exit()
your_button_rect = pygame.Rect(x,y,width,height)
color = (0,0,0)
pos = pygame.mouse.get_pos()
def change_button_color():
if your_button_rect.colliderect(pos):
if event.type == pygame.MOUSEBUTTONDOWN: #checks if mouse button is pressed down while the cursor is on the rectangle
color = (your_desired_Color_RGBvalue)
while True:
pygame.draw.rect(screen,color,your_button_rect)
change_button_color()

How do you store pygame button names in a list to use for computation? [duplicate]

Pls suggest how can I change the color of the button when I pressed it, and the color of the first button will be changed to the default color when I pressed the second button.
For example, After I clicked the STRAIGHT Button, the button will become green color and when I click the LEFT button the LEFT button will change to green color and the STRAIGHT button will become the default color which is white color. Thanks in advance :)
CODE:
def draw_button(self):
global clicked
action = False
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
if button_rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
clicked = True
pygame.draw.rect(screen, self.click_col, button_rect)
elif pygame.mouse.get_pressed()[0] == 0 and clicked == True:
clicked = False
action = True
else:
pygame.draw.rect(screen, self.hover_col, button_rect)
else:
pygame.draw.rect(screen, self.button_col, button_rect)
When you draw the button, you have to set the color dependent on the global variable clicked:
def draw_button(self):
global clicked
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
hover = button_rect.collidepoint(pos)
if hover and pygame.mouse.get_pressed()[0] == 1:
clicked = not clicked
color = self.button_col
if clicked:
color = self.click_col
elif hover:
color = self.hover_col
pygame.draw.rect(screen, color, button_rect)
Anyway, that won't satisfy you, because pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down.
You have to use MOUSEBUTTONDOWN event. The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked.
If you have multiple buttons that you have to interact with each other, a single clicked status is not enough. You need a separate "clicked" state for each button. If the clicked state of 1 button becomes True, the states of the other keys must be set to False. I recommend to implement a RadioButton class for this.
See also Mouse and Sprite.
Minimal example:
repl.it/#Rabbid76/PyGame-RadioButton
import pygame
class RadioButton(pygame.sprite.Sprite):
def __init__(self, x, y, w, h, font, text):
super().__init__()
text_surf = font.render(text, True, (0, 0, 0))
self.button_image = pygame.Surface((w, h))
self.button_image.fill((96, 96, 96))
self.button_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.hover_image = pygame.Surface((w, h))
self.hover_image.fill((96, 96, 96))
self.hover_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
pygame.draw.rect(self.hover_image, (96, 196, 96), self.hover_image.get_rect(), 3)
self.clicked_image = pygame.Surface((w, h))
self.clicked_image.fill((96, 196, 96))
self.clicked_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.image = self.button_image
self.rect = pygame.Rect(x, y, w, h)
self.clicked = False
self.buttons = None
def setRadioButtons(self, buttons):
self.buttons = buttons
def update(self, event_list):
hover = self.rect.collidepoint(pygame.mouse.get_pos())
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if hover and event.button == 1:
for rb in self.buttons:
rb.clicked = False
self.clicked = True
self.image = self.button_image
if self.clicked:
self.image = self.clicked_image
elif hover:
self.image = self.hover_image
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font50 = pygame.font.SysFont(None, 50)
radioButtons = [
RadioButton(50, 40, 200, 60, font50, "option 1"),
RadioButton(50, 120, 200, 60, font50, "option 2"),
RadioButton(50, 200, 200, 60, font50, "option 3")
]
for rb in radioButtons:
rb.setRadioButtons(radioButtons)
radioButtons[0].clicked = True
group = pygame.sprite.Group(radioButtons)
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update(event_list)
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
exit()
your_button_rect = pygame.Rect(x,y,width,height)
color = (0,0,0)
pos = pygame.mouse.get_pos()
def change_button_color():
if your_button_rect.colliderect(pos):
if event.type == pygame.MOUSEBUTTONDOWN: #checks if mouse button is pressed down while the cursor is on the rectangle
color = (your_desired_Color_RGBvalue)
while True:
pygame.draw.rect(screen,color,your_button_rect)
change_button_color()

How can I center a text and increase the font rectangle? [duplicate]

Pls suggest how can I change the color of the button when I pressed it, and the color of the first button will be changed to the default color when I pressed the second button.
For example, After I clicked the STRAIGHT Button, the button will become green color and when I click the LEFT button the LEFT button will change to green color and the STRAIGHT button will become the default color which is white color. Thanks in advance :)
CODE:
def draw_button(self):
global clicked
action = False
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
if button_rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
clicked = True
pygame.draw.rect(screen, self.click_col, button_rect)
elif pygame.mouse.get_pressed()[0] == 0 and clicked == True:
clicked = False
action = True
else:
pygame.draw.rect(screen, self.hover_col, button_rect)
else:
pygame.draw.rect(screen, self.button_col, button_rect)
When you draw the button, you have to set the color dependent on the global variable clicked:
def draw_button(self):
global clicked
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
hover = button_rect.collidepoint(pos)
if hover and pygame.mouse.get_pressed()[0] == 1:
clicked = not clicked
color = self.button_col
if clicked:
color = self.click_col
elif hover:
color = self.hover_col
pygame.draw.rect(screen, color, button_rect)
Anyway, that won't satisfy you, because pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down.
You have to use MOUSEBUTTONDOWN event. The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked.
If you have multiple buttons that you have to interact with each other, a single clicked status is not enough. You need a separate "clicked" state for each button. If the clicked state of 1 button becomes True, the states of the other keys must be set to False. I recommend to implement a RadioButton class for this.
See also Mouse and Sprite.
Minimal example:
repl.it/#Rabbid76/PyGame-RadioButton
import pygame
class RadioButton(pygame.sprite.Sprite):
def __init__(self, x, y, w, h, font, text):
super().__init__()
text_surf = font.render(text, True, (0, 0, 0))
self.button_image = pygame.Surface((w, h))
self.button_image.fill((96, 96, 96))
self.button_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.hover_image = pygame.Surface((w, h))
self.hover_image.fill((96, 96, 96))
self.hover_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
pygame.draw.rect(self.hover_image, (96, 196, 96), self.hover_image.get_rect(), 3)
self.clicked_image = pygame.Surface((w, h))
self.clicked_image.fill((96, 196, 96))
self.clicked_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.image = self.button_image
self.rect = pygame.Rect(x, y, w, h)
self.clicked = False
self.buttons = None
def setRadioButtons(self, buttons):
self.buttons = buttons
def update(self, event_list):
hover = self.rect.collidepoint(pygame.mouse.get_pos())
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if hover and event.button == 1:
for rb in self.buttons:
rb.clicked = False
self.clicked = True
self.image = self.button_image
if self.clicked:
self.image = self.clicked_image
elif hover:
self.image = self.hover_image
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font50 = pygame.font.SysFont(None, 50)
radioButtons = [
RadioButton(50, 40, 200, 60, font50, "option 1"),
RadioButton(50, 120, 200, 60, font50, "option 2"),
RadioButton(50, 200, 200, 60, font50, "option 3")
]
for rb in radioButtons:
rb.setRadioButtons(radioButtons)
radioButtons[0].clicked = True
group = pygame.sprite.Group(radioButtons)
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update(event_list)
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
exit()
your_button_rect = pygame.Rect(x,y,width,height)
color = (0,0,0)
pos = pygame.mouse.get_pos()
def change_button_color():
if your_button_rect.colliderect(pos):
if event.type == pygame.MOUSEBUTTONDOWN: #checks if mouse button is pressed down while the cursor is on the rectangle
color = (your_desired_Color_RGBvalue)
while True:
pygame.draw.rect(screen,color,your_button_rect)
change_button_color()

How do I implement option buttons and change the button color in PyGame?

Pls suggest how can I change the color of the button when I pressed it, and the color of the first button will be changed to the default color when I pressed the second button.
For example, After I clicked the STRAIGHT Button, the button will become green color and when I click the LEFT button the LEFT button will change to green color and the STRAIGHT button will become the default color which is white color. Thanks in advance :)
CODE:
def draw_button(self):
global clicked
action = False
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
if button_rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
clicked = True
pygame.draw.rect(screen, self.click_col, button_rect)
elif pygame.mouse.get_pressed()[0] == 0 and clicked == True:
clicked = False
action = True
else:
pygame.draw.rect(screen, self.hover_col, button_rect)
else:
pygame.draw.rect(screen, self.button_col, button_rect)
When you draw the button, you have to set the color dependent on the global variable clicked:
def draw_button(self):
global clicked
# get mouse position
pos = pygame.mouse.get_pos()
# create pygame Rect object for the button
button_rect = Rect(self.x, self.y, self.width, self.height)
# check mouseover and clicked conditions
hover = button_rect.collidepoint(pos)
if hover and pygame.mouse.get_pressed()[0] == 1:
clicked = not clicked
color = self.button_col
if clicked:
color = self.click_col
elif hover:
color = self.hover_col
pygame.draw.rect(screen, color, button_rect)
Anyway, that won't satisfy you, because pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down.
You have to use MOUSEBUTTONDOWN event. The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. The pygame.event.Event() object has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked.
If you have multiple buttons that you have to interact with each other, a single clicked status is not enough. You need a separate "clicked" state for each button. If the clicked state of 1 button becomes True, the states of the other keys must be set to False. I recommend to implement a RadioButton class for this.
See also Mouse and Sprite.
Minimal example:
repl.it/#Rabbid76/PyGame-RadioButton
import pygame
class RadioButton(pygame.sprite.Sprite):
def __init__(self, x, y, w, h, font, text):
super().__init__()
text_surf = font.render(text, True, (0, 0, 0))
self.button_image = pygame.Surface((w, h))
self.button_image.fill((96, 96, 96))
self.button_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.hover_image = pygame.Surface((w, h))
self.hover_image.fill((96, 96, 96))
self.hover_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
pygame.draw.rect(self.hover_image, (96, 196, 96), self.hover_image.get_rect(), 3)
self.clicked_image = pygame.Surface((w, h))
self.clicked_image.fill((96, 196, 96))
self.clicked_image.blit(text_surf, text_surf.get_rect(center = (w // 2, h // 2)))
self.image = self.button_image
self.rect = pygame.Rect(x, y, w, h)
self.clicked = False
self.buttons = None
def setRadioButtons(self, buttons):
self.buttons = buttons
def update(self, event_list):
hover = self.rect.collidepoint(pygame.mouse.get_pos())
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if hover and event.button == 1:
for rb in self.buttons:
rb.clicked = False
self.clicked = True
self.image = self.button_image
if self.clicked:
self.image = self.clicked_image
elif hover:
self.image = self.hover_image
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
font50 = pygame.font.SysFont(None, 50)
radioButtons = [
RadioButton(50, 40, 200, 60, font50, "option 1"),
RadioButton(50, 120, 200, 60, font50, "option 2"),
RadioButton(50, 200, 200, 60, font50, "option 3")
]
for rb in radioButtons:
rb.setRadioButtons(radioButtons)
radioButtons[0].clicked = True
group = pygame.sprite.Group(radioButtons)
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update(event_list)
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
exit()
your_button_rect = pygame.Rect(x,y,width,height)
color = (0,0,0)
pos = pygame.mouse.get_pos()
def change_button_color():
if your_button_rect.colliderect(pos):
if event.type == pygame.MOUSEBUTTONDOWN: #checks if mouse button is pressed down while the cursor is on the rectangle
color = (your_desired_Color_RGBvalue)
while True:
pygame.draw.rect(screen,color,your_button_rect)
change_button_color()

Python menu with "add image" option [duplicate]

I am a newbie to Pygame and I have created already the codes for my button but I still have a problem because I don't know how will I put an image instead of the solid color red in a rectangle. Here is my codes, hope you can help!
def button(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(screen, ac, (x, y, w, h))
if click[0] == 1 and action!= None:
if action == "continue":
quiz()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(randomList, [0, 0])
button(399, 390, 300, 50, red, brightRed, "continue")
pygame.display.update()
All you have to do is to load an image:
my_image = pygame.image.load('my_image.png').convert_alpha()
And blit it an top of the rectangle:
def button(x, y, w, h, ic, ac, img, imgon, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
rect = pygame.Rect(x, y, w, h)
on_button = rect.collidepoint(mouse)
if on_button:
pygame.draw.rect(screen, ac, rect)
screen.blit(imgon, imgon.get_rect(center = rect.center))
else:
pygame.draw.rect(screen, ic, rect)
screen.blit(img, img.get_rect(center = rect.center))
if on_button:
if click[0] == 1 and action!= None:
if action == "continue":
quiz()
image = pygame.image.load('my_image.png').convert_alpha()
imageOn = pygame.image.load('my_image_on.png').convert_alpha()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.blit(randomList, [0, 0])
button(399, 390, 300, 50, red, brightRed, image, imageOn, "continue")
pygame.display.update()
In pygame a button is nothing more than a pygame.Surface object. It is completely irrelevant whether a text or an image is on the button. I recommend to represent the buttons by pygame.sprite.Sprite objects.
See also Pygame mouse clicking detection respectively Mouse and Sprite.
Minimal example:
import pygame
class SpriteObject(pygame.sprite.Sprite):
def __init__(self, x, y, filename):
super().__init__()
img = pygame.image.load(filename).convert_alpha()
self.original_image = pygame.Surface((70, 70))
self.original_image.blit(img, img.get_rect(center = self.original_image.fill((127, 127, 127)).center))
self.hover_image = pygame.Surface((70, 70))
self.hover_image.blit(img, img.get_rect(center = self.hover_image.fill((228, 228, 228)).center))
self.image = self.original_image
self.rect = self.image.get_rect(center = (x, y))
self.hover = False
def update(self):
self.hover = self.rect.collidepoint(pygame.mouse.get_pos())
self.image = self.hover_image if self.hover else self.original_image
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
group = pygame.sprite.Group([
SpriteObject(window.get_width() // 3, window.get_height() // 3, 'Apple64.png'),
SpriteObject(window.get_width() * 2 // 3, window.get_height() // 3, 'Banana64.png'),
SpriteObject(window.get_width() // 3, window.get_height() * 2 // 3, 'Pear64.png'),
SpriteObject(window.get_width() * 2// 3, window.get_height() * 2 // 3, 'Plums64.png'),
])
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update()
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
exit()

Categories