Pygame鼠标按钮在一个cli上工作

2024-04-19 13:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我读了关于这个问题的其他文章,但我仍然不明白。我只想让我的按钮在按下一次的时候执行,而不是在我必须按下它的时候。我有一个while循环中的按钮,第一次运行良好,但第二次就不工作了。我的密码在这里。感谢您的帮助,因为我的代码写得很差,因为我是新来的,这是很难理解的任何人除了我。在

def newRound():
    pos = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    print(click)
    if 730 < pos[0] < 850 and 650 < pos[1] < 800:
        pygame.draw.rect(Background, (150,150,150), (730,650,120,50))
        if click[0] == 1:
            startGame() 

while intro == 1:            
    if endRound == True:
        Background.blit(mapImg, (0,0))
        newRound()
        text()

    if startRound == True:
        for enemy in enemies:
            enemy.update()
        Background.blit(mapImg, (0,0))
        for enemy in enemies:
            enemy.draw(Background)

包含不重要位的完整代码

^{pr2}$

Tags: 代码postruegetif按钮pygameclick
1条回答
网友
1楼 · 发布于 2024-04-19 13:28:25

你的按钮可以用。。。但你在移动敌人时遇到了问题,而且按钮似乎不起作用。在

你移动敌人直到你得到hit == 6,当你再次点击按钮时,hit已经是{},所以hit == 6结束移动敌人,你看不到它。在

所以你需要

if hit == 6:
    startRound = False
    endRound = True
    hit = 0

或者使用不同的元素来检查何时结束圆。在


当你结束移动敌人时,你不会将他们从列表中移除enemies,当你再次点击按钮时,你会向列表中添加新的敌人,并且你的列表上有越来越多的敌人。检查len(enemies)。也就是说

^{pr2}$

所以在你再次使用它之前,先把它列清楚

def createRound():
    global enemies

    enemies = []

    x = -80
    y = 210
    for e in range(6):
        x = x - 80
        enemies.append(RedEnemy(x, y, Background))
    print('enemies:', len(enemies))

顺便说一句:你可以用intro = True代替intro = 1。和while intro:而不是while intro == 1:。它更具可读性。在

相关问题 更多 >