Pygame鼠标按钮一键工作

时间:2016-12-29 18:41:29

标签: python button while-loop click pygame

我已阅读有关此问题的其他文章,但我仍然不理解它们。我只是希望我的按钮在按下时执行一次,而不是在我按住它时。我有一个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)

包含不重要位的完整代码

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)

1 个答案:

答案 0 :(得分:0)

你的按钮有效...但你有移动敌人的问题,看起来按钮不起作用。

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

所以你需要

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

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

当你结束移动敌人时,你不会从列表enemies中删除它们,当你再次点击按钮时,你会添加新的敌人列表,并且列表中有越来越多的敌人。检查len(enemies)。即

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。而while intro:代替while intro == 1:。它更具可读性。