【问题标题】:Pygame mouse button work on one clickPygame鼠标按钮一键工作
【发布时间】:2016-12-29 18:41:28
【问题描述】:

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

【问题讨论】:

  • 顺便说一句:你需要pygame.QUIT 而不是quit in event.type == pygame.QUIT
  • 顺便说一句:见PEP 8 -- Style Guide for Python Code - 使用lower_case 变量名称 - 即。 background 而不是 Backgroundend_round 而不是 endRound,等等。
  • 使用 print() 检查变量中的值以及执行的代码部分 - 可能不是您所期望的。
  • 感谢 furas 的帮助

标签: python button while-loop click pygame


【解决方案1】:

你的按钮可以工作......但是你在移动敌人时遇到了问题,而且看起来按钮不起作用。

你移动敌人直到你得到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:。它更具可读性。

【讨论】:

  • 非常感谢这对我帮助很大
猜你喜欢
  • 1970-01-01
  • 2012-06-24
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多