我想用玩偶来做绞刑架
1>如果您在IDE中复制以下代码并运行,如果您单击“内部”按钮(圆圈),它将提示“游戏”窗口,它将基本消失。就像典型的绞刑游戏
2>,但如果您注释行,那么代码将停止工作,意味着按钮(圆圈)将不再消失
**有人能说出原因吗?**
import pygame , math
pygame.init()
WIDTH , HEIGHT = 900 , 600
win = pygame.display.set_mode((WIDTH , HEIGHT))
pygame.display.set_caption('Hangmane')
RUN = True
# indexing button coordinates and appending in letters(list)
letters = [ ]
RADIUS = 20
BUTTON_X = 0
BUTTON_Y = 457
for i in range(26):
BUTTON_X += 60
if i == 14:
BUTTON_X = 100
BUTTON_Y = 520
#pygame.draw.circle(win,'green',(BUTTON_X,BUTTON_Y),RADIUS,3)
letters.append([BUTTON_X, BUTTON_Y])
#drawing button(circle) in pygame window
def button():
for letter in letters:
BUTTON_X , BUTTON_Y = letter
pygame.draw.circle(win,'green',(BUTTON_X,BUTTON_Y),RADIUS,3)
# this will check and remove if button(circle) is clicked
def click_checker(coordinates):
letters.remove(coordinates)
while RUN:
win.fill('darkred') #if you comment this than code will not work as intended
button()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUN = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos_x , pos_y = pygame.mouse.get_pos()
print(pos_x , pos_y)
for letter in letters:
BUTTON_X , BUTTON_Y = letter
dis = math.sqrt((BUTTON_X-pos_x)**2 + (BUTTON_Y-pos_y)**2)
if dis < RADIUS: # this will check if distance between mouse Cursor is less than button(circle) radius
#print('clicked')
click_checker(letter)
pygame.quit()
发布于 2022-08-10 20:20:59
首先,您应该了解代码如何在while RUN:
循环中工作:
letters
列表中的x和y坐标绘制的。在每次按下按钮时,如果按下按钮,则从letters
列表中移除按钮的x和y坐标。letters
列表中没有任何内容被删除。H 210H 111
现在再转到步骤1。H 212G 213
如果您注释掉了win.fill('darkred')
,那么该窗口将不会被绘制,因此前面的按钮是而不是擦除。因此,即使它只会重画25个按钮,另一个按钮在之前的绘图中仍然是可见的。
https://stackoverflow.com/questions/73314989
复制