이 문제에 대한 다른 기사를 읽었지만 여전히 이해할 수 없습니다. 버튼을 누른 상태에서 한 번만 누르면 내 버튼이 실행 되기만하면됩니다. 나는 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)
BTW :'event.type == pygame.QUIT'에'quit' 대신'pygame.QUIT'가 필요합니다. – furas
BTW : [PEP 8 스타일 가이드 (파이썬 코드)] (https : // www. python.org/dev/peps/pep-0008/) - 변수에'lower_case' 이름을 사용하십시오. 'background' 대신에'background','endRound' 대신에'end_round', ettc. – furas
'print()'를 사용하여 변수에있는 값과 실행 된 코드 부분을 확인하십시오. 아마도 예상 한 것과 다를 수 있습니다. – furas