2016-12-29 8 views
0

이 문제에 대한 다른 기사를 읽었지만 여전히 이해할 수 없습니다. 버튼을 누른 상태에서 한 번만 누르면 내 버튼이 실행 되기만하면됩니다. 나는 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

BTW :'event.type == pygame.QUIT'에'quit' 대신'pygame.QUIT'가 필요합니다. – furas

+1

BTW : [PEP 8 스타일 가이드 (파이썬 코드)] (https : // www. python.org/dev/peps/pep-0008/) - 변수에'lower_case' 이름을 사용하십시오. 'background' 대신에'background','endRound' 대신에'end_round', ettc. – furas

+1

'print()'를 사용하여 변수에있는 값과 실행 된 코드 부분을 확인하십시오. 아마도 예상 한 것과 다를 수 있습니다. – furas

답변

0

귀하의 버튼을 작동 ...하지만 당신은 이동 적으로 문제가있는 버튼이 작동하지 않는 것 같습니다. 당신이 hit == 6을 얻고 다시 버튼을 클릭하면 다음 hit6 그래서 hit == 6 적을 이동 종료 이미까지

당신은 원수를 이동하고 당신은 그것을 볼 수 없습니다.

그래서 당신은

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

필요하거나 라운드 종료 할 때 확인하는 다른 요소를 사용합니다.


것은 당신이 목록 enemies에서 제거하지 않고 다시 버튼을 클릭하면이 목록에 새 원수를 추가하고 목록에 더 많은 원수가 원수를 이동 종료 될 때. len(enemies)을 확인하십시오. 즉. 당신은 intro = True 대신 intro = 1 사용할 수 있습니다 다시

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)) 
BTW

사용하기 전에

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

그래서 명확한 목록입니다. 그리고 while intro == 1: 대신 while intro:입니다. 더 읽기 쉽습니다.

+0

고맙습니다. 제게 많은 도움을 주셨습니다. –

관련 문제