如何让按钮出现在 Pygame 圆圈上方?
How can I Make appear Button above a Pygame Circle?
我尝试了很长时间尝试将按钮放在彩色圆圈上方,但我做不到,当我这样做时按钮被圆圈覆盖,我尝试了几种方法但是 none 他们工作。
import pygame, math
from pygame import font
pygame.init()
def lerp_color(colors, value):
fract, index = math.modf(value)
color1 = pygame.Color(colors[int(index) % len(colors)])
color2 = pygame.Color(colors[int(index + 1) % len(colors)])
return color1.lerp(color2, fract)
def button(screen, position, text):
font = pygame.font.SysFont("Anton", 50)
text_render = font.render(text, 1, (0, 0, 0))
x, y, w , h = text_render.get_rect()
x, y = position
pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)
pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)
pygame.draw.rect(screen, (100, 100, 100), (x, y, w , h))
return screen.blit(text_render, (x, y))
pygame.init()
screen = pygame.display.set_mode((800, 800))
screen2 = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
b1 = button(screen,(310,200), "Faster")
variable = 1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
screen.blit(b1)
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
current_color = lerp_color(colors, value)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
print(value)
pygame.display.flip()
pygame.quit()
exit()
你必须在圆圈后面画按钮:
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
print(value)
current_color = lerp_color(colors, value)
screen.fill(0)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
button(screen,(310,200), "Faster")
pygame.display.flip()
请注意,指令 screen.blit(b1)
没有按照您的预期执行。它只会引起错误。 blit
does not return an object, it only returns the rectangular area that was affected by the operation. Therefore the button
returns a pygame.Rect
对象和 bl
只是一个矩形。
我尝试了很长时间尝试将按钮放在彩色圆圈上方,但我做不到,当我这样做时按钮被圆圈覆盖,我尝试了几种方法但是 none 他们工作。
import pygame, math
from pygame import font
pygame.init()
def lerp_color(colors, value):
fract, index = math.modf(value)
color1 = pygame.Color(colors[int(index) % len(colors)])
color2 = pygame.Color(colors[int(index + 1) % len(colors)])
return color1.lerp(color2, fract)
def button(screen, position, text):
font = pygame.font.SysFont("Anton", 50)
text_render = font.render(text, 1, (0, 0, 0))
x, y, w , h = text_render.get_rect()
x, y = position
pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)
pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)
pygame.draw.rect(screen, (100, 100, 100), (x, y, w , h))
return screen.blit(text_render, (x, y))
pygame.init()
screen = pygame.display.set_mode((800, 800))
screen2 = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
start_time = pygame.time.get_ticks()
b1 = button(screen,(310,200), "Faster")
variable = 1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
screen.blit(b1)
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
current_color = lerp_color(colors, value)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
print(value)
pygame.display.flip()
pygame.quit()
exit()
你必须在圆圈后面画按钮:
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
start_time = pygame.time.get_ticks()
colors = [(000,000,000), (255,0,0), (000,255,000), (000,000,255), (255,0,255), (000,255,255), (255,255,000), (255,000,255)]
value = (pygame.time.get_ticks() - start_time) / 1000
print(value)
current_color = lerp_color(colors, value)
screen.fill(0)
pygame.draw.circle(screen2, current_color, screen.get_rect().center, 500)
button(screen,(310,200), "Faster")
pygame.display.flip()
请注意,指令 screen.blit(b1)
没有按照您的预期执行。它只会引起错误。 blit
does not return an object, it only returns the rectangular area that was affected by the operation. Therefore the button
returns a pygame.Rect
对象和 bl
只是一个矩形。