2016-12-29 4 views
0

私はこの問題に関する他の記事を読んだが、まだ理解していない。私はそれを押さなければならない時ではなく、一度押すと私のボタンが実行されるようにしたい。私はしばらくループ内のボタンを持っていて、最初の回はうまくいますが、2回目にはうまくいきません。私のコードはここにあります。私のコードはあまり書き込まれていないので、私は非常に新しく、これは私以外の誰にも理解しにくいので、何か助けてくれてありがとう。重要でないビットでワンクリックで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) 
+1

BTW: 'event.type == pygame.QUIT'に' quit'の代わりに 'pygame.QUIT'が必要です。 – furas

+1

BTW:[PEP 8 - スタイルガイドfor Python Code](https:// www。 python.org/dev/peps/pep-0008/) - 変数に 'lower_case'という名前を使用します。 'background'の代わりに' background'、 'endRound'の代わりに' end_round'、ettcです。 – furas

+1

'print()'を使って、変数にどのような値が入っているか、実行されているコードの部分をチェックします。おそらく期待通りではありません。 – furas

答えて

0

あなたのボタンは動作します...しかし、あなたは敵の移動に問題があるとボタンが作動しないように見えます。あなたがhit == 6を得るまで、あなたが再びhitをボタンをクリックすると、あなたは敵を動かす

のでhit == 6端が敵を動かすすでに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 == 1:ではなく、while intro:です。それはより読みやすくなります。

+0

ありがとうございます、これは私に多くの助けになりました –

関連する問題