This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,5 +1,5 @@
#!/usr/bin/env python
"""pygame.examples.cursors
""" pygame.examples.cursors
Click a button and the cursor will change.
This example will show you:
*The different types of cursors that exist
@@ -8,30 +8,30 @@ This example will show you:
*How to make a simple button
"""
import pygame as pg
import os
import pygame
# Create a system cursor
system_cursor1 = pygame.SYSTEM_CURSOR_CROSSHAIR
system_cursor2 = pygame.SYSTEM_CURSOR_HAND
system_cursor3 = pygame.SYSTEM_CURSOR_IBEAM
system_cursor1 = pg.SYSTEM_CURSOR_CROSSHAIR
system_cursor2 = pg.SYSTEM_CURSOR_HAND
system_cursor3 = pg.SYSTEM_CURSOR_IBEAM
# Create a color cursor
surf = pygame.Surface((40, 40))
surf = pg.Surface((40, 40))
surf.fill((120, 50, 50))
color_cursor = pygame.cursors.Cursor((20, 20), surf)
color_cursor = pg.cursors.Cursor((20, 20), surf)
# Create a color cursor with an image surface
main_dir = os.path.split(os.path.abspath(__file__))[0]
image_name = os.path.join(main_dir, "data", "cursor.webp")
image = pygame.image.load(image_name)
image_cursor = pygame.cursors.Cursor(
image_name = os.path.join(main_dir, "data", "cursor.png")
image = pg.image.load(image_name)
image_cursor = pg.cursors.Cursor(
(image.get_width() // 2, image.get_height() // 2), image
)
@@ -66,16 +66,16 @@ thickarrow_strings = (
" ",
)
bitmap_cursor1 = pygame.cursors.Cursor(
bitmap_cursor1 = pg.cursors.Cursor(
(24, 24),
(0, 0),
*pygame.cursors.compile(thickarrow_strings, black="X", white=".", xor="o"),
*pg.cursors.compile(thickarrow_strings, black="X", white=".", xor="o"),
)
# Create a bitmap cursor from premade simple strings
bitmap_cursor2 = pygame.cursors.diamond
bitmap_cursor2 = pg.cursors.diamond
# Calculate if mouse position is inside circle
@@ -84,14 +84,14 @@ def check_circle(mouse_pos_x, mouse_pos_y, center_x, center_y, radius):
def main():
pygame.init()
pygame.display.set_caption("Cursors Example")
pg.init()
pg.display.set_caption("Cursors Example")
pygame.font.init()
font = pygame.Font(None, 30)
font1 = pygame.Font(None, 24)
pg.font.init()
font = pg.font.Font(None, 30)
font1 = pg.font.Font(None, 24)
bg = pygame.display.set_mode((500, 400))
bg = pg.display.set_mode((500, 400))
bg.fill((183, 201, 226))
# Initialize circles
@@ -119,17 +119,17 @@ def main():
pos_y6 = 220
pos_y7 = 140
circle1 = pygame.draw.circle(bg, (255, 255, 255), (pos_x1, pos_y1), radius1)
circle2 = pygame.draw.circle(bg, (255, 255, 255), (pos_x2, pos_y2), radius2)
circle3 = pygame.draw.circle(bg, (255, 255, 255), (pos_x3, pos_y3), radius3)
circle4 = pygame.draw.circle(bg, (255, 255, 255), (pos_x4, pos_y4), radius4)
circle5 = pygame.draw.circle(bg, (255, 255, 255), (pos_x5, pos_y5), radius5)
circle6 = pygame.draw.circle(bg, (255, 255, 255), (pos_x6, pos_y6), radius6)
circle7 = pygame.draw.circle(bg, (255, 255, 255), (pos_x7, pos_y7), radius7)
circle1 = pg.draw.circle(bg, (255, 255, 255), (pos_x1, pos_y1), radius1)
circle2 = pg.draw.circle(bg, (255, 255, 255), (pos_x2, pos_y2), radius2)
circle3 = pg.draw.circle(bg, (255, 255, 255), (pos_x3, pos_y3), radius3)
circle4 = pg.draw.circle(bg, (255, 255, 255), (pos_x4, pos_y4), radius4)
circle5 = pg.draw.circle(bg, (255, 255, 255), (pos_x5, pos_y5), radius5)
circle6 = pg.draw.circle(bg, (255, 255, 255), (pos_x6, pos_y6), radius6)
circle7 = pg.draw.circle(bg, (255, 255, 255), (pos_x7, pos_y7), radius7)
# Initialize button
button_text = font1.render("Click here to change cursor", True, (0, 0, 0))
button = pygame.draw.rect(
button = pg.draw.rect(
bg,
(180, 180, 180),
(139, 300, button_text.get_width() + 5, button_text.get_height() + 50),
@@ -137,7 +137,7 @@ def main():
button_text_rect = button_text.get_rect(center=button.center)
bg.blit(button_text, button_text_rect)
pygame.display.update()
pg.display.update()
cursors = [
system_cursor1,
@@ -150,60 +150,60 @@ def main():
]
index = 0
pygame.mouse.set_cursor(cursors[index])
pg.mouse.set_cursor(cursors[index])
was_pressed = False
clock = pygame.Clock()
pressed = False
clock = pg.time.Clock()
while True:
clock.tick(50)
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_x, mouse_y = pg.mouse.get_pos()
# Check if mouse is inside a circle to change its color
if check_circle(mouse_x, mouse_y, circle1.centerx, circle1.centery, radius1):
circle1 = pygame.draw.circle(bg, (255, 0, 0), (pos_x1, pos_y1), radius1)
circle1 = pg.draw.circle(bg, (255, 0, 0), (pos_x1, pos_y1), radius1)
else:
circle1 = pygame.draw.circle(bg, (255, 255, 255), (pos_x1, pos_y1), radius1)
circle1 = pg.draw.circle(bg, (255, 255, 255), (pos_x1, pos_y1), radius1)
if check_circle(mouse_x, mouse_y, circle2.centerx, circle2.centery, radius2):
circle2 = pygame.draw.circle(bg, (255, 127, 0), (pos_x2, pos_y2), radius2)
circle2 = pg.draw.circle(bg, (255, 127, 0), (pos_x2, pos_y2), radius2)
else:
circle2 = pygame.draw.circle(bg, (255, 255, 255), (pos_x2, pos_y2), radius2)
circle2 = pg.draw.circle(bg, (255, 255, 255), (pos_x2, pos_y2), radius2)
if check_circle(mouse_x, mouse_y, circle3.centerx, circle3.centery, radius3):
circle3 = pygame.draw.circle(bg, (255, 255, 0), (pos_x3, pos_y3), radius3)
circle3 = pg.draw.circle(bg, (255, 255, 0), (pos_x3, pos_y3), radius3)
else:
circle3 = pygame.draw.circle(bg, (255, 255, 255), (pos_x3, pos_y3), radius3)
circle3 = pg.draw.circle(bg, (255, 255, 255), (pos_x3, pos_y3), radius3)
if check_circle(mouse_x, mouse_y, circle4.centerx, circle4.centery, radius3):
circle4 = pygame.draw.circle(bg, (0, 255, 0), (pos_x4, pos_y4), radius4)
circle4 = pg.draw.circle(bg, (0, 255, 0), (pos_x4, pos_y4), radius4)
else:
circle4 = pygame.draw.circle(bg, (255, 255, 255), (pos_x4, pos_y4), radius4)
circle4 = pg.draw.circle(bg, (255, 255, 255), (pos_x4, pos_y4), radius4)
if check_circle(mouse_x, mouse_y, circle5.centerx, circle5.centery, radius4):
circle5 = pygame.draw.circle(bg, (0, 0, 255), (pos_x5, pos_y5), radius5)
circle5 = pg.draw.circle(bg, (0, 0, 255), (pos_x5, pos_y5), radius5)
else:
circle5 = pygame.draw.circle(bg, (255, 255, 255), (pos_x5, pos_y5), radius5)
circle5 = pg.draw.circle(bg, (255, 255, 255), (pos_x5, pos_y5), radius5)
if check_circle(mouse_x, mouse_y, circle6.centerx, circle6.centery, radius6):
circle6 = pygame.draw.circle(bg, (75, 0, 130), (pos_x6, pos_y6), radius6)
circle6 = pg.draw.circle(bg, (75, 0, 130), (pos_x6, pos_y6), radius6)
else:
circle6 = pygame.draw.circle(bg, (255, 255, 255), (pos_x6, pos_y6), radius6)
circle6 = pg.draw.circle(bg, (255, 255, 255), (pos_x6, pos_y6), radius6)
if check_circle(mouse_x, mouse_y, circle7.centerx, circle7.centery, radius7):
circle7 = pygame.draw.circle(bg, (148, 0, 211), (pos_x7, pos_y7), radius7)
circle7 = pg.draw.circle(bg, (148, 0, 211), (pos_x7, pos_y7), radius7)
else:
circle7 = pygame.draw.circle(bg, (255, 255, 255), (pos_x7, pos_y7), radius7)
circle7 = pg.draw.circle(bg, (255, 255, 255), (pos_x7, pos_y7), radius7)
bg.fill((183, 201, 226), (0, 15, bg.get_width(), 50))
text1 = font.render(
(f"This is a {pygame.mouse.get_cursor().type} cursor"), True, (0, 0, 0)
(f"This is a {pg.mouse.get_cursor().type} cursor"), True, (0, 0, 0)
)
text_rect1 = text1.get_rect(center=(bg.get_width() / 2, 40))
bg.blit(text1, text_rect1)
button = pygame.draw.rect(
button = pg.draw.rect(
bg,
(100, 149, 240),
(139, 300, button_text.get_width() + 5, button_text.get_height() + 50),
@@ -212,7 +212,7 @@ def main():
# Check if button was clicked and change cursor
if button.collidepoint(mouse_x, mouse_y):
button = pygame.draw.rect(
button = pg.draw.rect(
bg,
(60, 100, 255),
(
@@ -224,8 +224,8 @@ def main():
)
bg.blit(button_text, button_text_rect)
if pygame.mouse.get_pressed()[0] and not was_pressed:
button = pygame.draw.rect(
if pg.mouse.get_pressed()[0] == 1 and pressed is False:
button = pg.draw.rect(
bg,
(0, 0, 139),
(
@@ -238,18 +238,21 @@ def main():
bg.blit(button_text, button_text_rect)
index += 1
index %= len(cursors)
pygame.mouse.set_cursor(cursors[index])
pygame.display.update()
pygame.time.delay(40)
pg.mouse.set_cursor(cursors[index])
pg.display.update()
pg.time.delay(40)
was_pressed = pygame.mouse.get_pressed()[0]
if pg.mouse.get_pressed()[0] == 1:
pressed = True
elif pg.mouse.get_pressed()[0] == 0:
pressed = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
raise SystemExit
pygame.display.update()
pg.display.update()
if __name__ == "__main__":