Python Pygame鼠标按钮一次点击即可工作

Python Pygame鼠标按钮一次点击即可工作,python,button,while-loop,click,pygame,Python,Button,While Loop,Click,Pygame,我已经读过关于这个问题的其他文章,但我仍然不理解它们。我只希望我的按钮在按下一次时执行,而不是在我必须按住它时执行。我有一个while循环中的按钮,第一次在它周围工作正常,但第二次它不工作。我的密码在这里。感谢您的帮助,因为我的代码写得很糟糕,因为我是一个非常新的人,除了我以外的任何人都很难理解 def newRound(): pos = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() print(cli

我已经读过关于这个问题的其他文章,但我仍然不理解它们。我只希望我的按钮在按下一次时执行,而不是在我必须按住它时执行。我有一个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)
def newRound():
pos=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
打印(单击)
如果730
包含不重要位的完整代码

import pygame

def text():
    font = pygame.font.SysFont("monospace", 14)
    text = font.render("Start Round", True, black)
    textpos = text.get_rect()
    textpos.center = (790,675)
    Background.blit(text, textpos)

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()          
    else:
        pygame.draw.rect(Background, (100,100,100), (730,650,120,50))

def startGame():
    global startRound, endRound, intro, whichRound
    intro = 0       
    createRound()
    intro = 1
    startRound = True
    endRound = False

def life(self):
    global hit, endRound, startRound
    if self.rect.x == 960:
        hit = hit + 1
    if hit == 6:
        startRound = False
        endRound = True

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

class RedEnemy(object):

    image1 = pygame.image.load("enemySpriteFullHealth.jpg")
    image2 = pygame.image.load("enemySpriteHalfHealth.jpg")
    image3 = pygame.image.load("enemySpriteDead.jpg")

    def __init__(self, x, y, Background):
        self.Background = Background
        self.Background_rect = Background.get_rect()
        self.rect = self.image1.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.health = 20
        self.dist_x = 2
        self.dist_y = 0

    def update(self):
        self.rect.x += self.dist_x
        self.rect.y += self.dist_y
    def draw(self, Background):
        Background.blit(self.image1, self.rect)
        life(self)

pygame.init()

width = 960
height = 720

black = (0,0,0)
lifes = 30
hit = 0
intro = 1
enemies = []
FPS = 200

endRound = True
startRound = False

clock = pygame.time.Clock()
mapImg = pygame.image.load("mapimage.jpg")
Background = pygame.display.set_mode((width, height))
Background_rect = Background.get_rect()

while intro == 1:
    for event in pygame.event.get():
        if event.type == quit:
            pygame.quit()

    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)

    pygame.display.update()
    clock.tick(FPS)
导入pygame
def text():
font=pygame.font.SysFont(“monospace”,14)
text=font.render(“开始轮”,真,黑色)
textpos=text.get_rect()
textpos.center=(790675)
Background.blit(text,textpos)
def newRound():
pos=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
打印(单击)
如果730
您的按钮工作正常。。。但你们在移动敌人时遇到了问题,看起来按钮不起作用

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

所以你需要

if hit == 6:
    startRound = False
    endRound = True
    hit = 0
或者使用不同的元素检查何时结束循环


当你们停止移动敌人时,你们不会将他们从列表中移除,当你们再次点击按钮时,你们会将新的敌人添加到列表中,列表中的敌人会越来越多。检查
len(敌人)
。即

def createRound():
    x = -80
    y = 210
    for e in range(6):
        x = x - 80
        enemies.append(RedEnemy(x, y, Background))
    print('enemies:', len(enemies))
所以,在再次使用之前,请清除列表

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
。和
而intro:
而不是
而intro==1:
。更具可读性。

BTW:您需要在
event.type==pygame.QUIT中退出
pygame.QUIT
而不是
QUIT
而不是
pygame.QUIT
BTW:请参阅-使用
lower\u case
变量名称-即
background
而不是od
background
end\code>而不是
endRound
,ettc.use
print()
检查变量中的值以及执行的代码部分-可能与您期望的不同。感谢furas的帮助非常感谢这对我帮助很大