Я прочитал другие статьи по этой проблеме, и я до сих пор не понимаю их. Я просто хочу, чтобы моя кнопка выполнялась, когда она нажималась один раз, когда мне нужно удерживать ее. У меня есть кнопка в цикле while, и в первый раз вокруг нее отлично работает, но затем во второй раз она не работает. Мой код здесь. Спасибо за любую помощь, поскольку мой код плохо написан, поскольку я очень новичок, и это трудно понять никому, кроме меня.Pygame кнопка мыши работает одним щелчком мыши
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)
BTW: вам нужно 'pygame.QUIT' вместо' quit' в 'event.type == pygame.QUIT' – furas
BTW: см. [PEP 8 - Руководство по стилю для кода Python] (https: // www. python.org/dev/peps/pep-0008/) - используйте имена 'lower_case' для переменной - то есть. 'background' вместо' Background', 'end_round' вместо' endRound', ettc. – furas
используйте 'print()', чтобы проверить, какие значения у вас есть в переменных и какая часть кода выполнена - вероятно, это не то, что вы ожидаете. – furas