2016-07-06 5 views
-2

私は2人の戦闘ゲームを作っています。私が試して「v」を押すたびに、私のゲームを撃つ鍵はクラッシュして動作しないようです。私はパイゲームには新しく、私が行くにつれて学習しています。どんな助けもありがとうございます。ありがとう!私はゲームをしようとするたびにゲームがクラッシュする

import pygame 

pygame.init() 

#Sets up 8 bit colours 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
lightblue = (180,235,255) 
grassgreen =(20,200,50) 

#Sets up pygame window 
gameDisplay = pygame.display.set_mode((1000,600)) 
pygame.display.set_caption('Block Fighter') 



#Variables 
gameExit = False 
x = 0 
y = 0 
x_change = 0 
y_change = 0 
isShooting = False 

clock = pygame.time.Clock() 

class Player: 
    def __init__(self, x_change, y_change, x, y): 
     self.x_change = 0 
     self.y_change = 0 
     self.x= 50 
     self.y= 480 

    def Left(self, x_change): 
     self.x_change = -5 
    def Right(self, x_change): 
     self.x_change =5 
    def Jump(self, y_change): 
     if (self.y == 480): 
      self.y_change = -70 
      return 
p1 = Player(x_change, y_change, x, y) 

class Bullet: 
    def __init__(self, x, y, Player, x_change): 
     self.x = p1.x 
     self.y = p1.y 
     self.x_change = 0 
    def Show(self, x_change, x , y, Player): 
     pygame.draw.rect(gameDisplay, black , [p1.x,p1.y,5,5]) 
     self.x_change = 10 
b1 = Bullet(x, y, Player, x_change) 

#Main Game Loop 
while not gameExit: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      gameExit = True 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_a: 
       p1.Left(x_change) 
      if event.key == pygame.K_d: 
       p1.Right(x_change) 
      if event.key == pygame.K_w: 
       p1.Jump(y_change) 
      if event.key == pygame.K_v: 
       b1.Show(x, y, x_change, Player) 
       isShooting = True 
     while isShooting: 
      pygame.draw.rect(gameDisplay, black, [b1.x, b1.y , 5,5]) 
      if b1.x > 1000: 
       pass 
     if event.type == pygame.KEYUP: 
      if event.key == pygame.K_a or event.key == pygame.K_d: 
       p1.x_change = 0 

    if p1.x <= 0: 
     p1.x = 0 
    if p1.x >= 500: 
     p1.x = 500 

    if p1.y > 480: 
     p1.y_change = 0 
     p1.y = 480 
    if p1.y < 480: 
     p1.y_change = 5 

    p1.x += p1.x_change 
    p1.y += p1.y_change 
    b1.x += b1.x_change 
    gameDisplay.fill(lightblue) 
    pygame.draw.rect(gameDisplay, grassgreen, [1000,600,-1000,-100]) 
    pygame.draw.rect(gameDisplay, red, [p1.x,p1.y,20,20]) 
    pygame.display.update() 

    clock.tick(40) 

pygame.quit() 
quit() 
+0

エラーは何ですか? – Ares

+0

尋ねる前に何をデバッグしましたか? –

答えて

0

isShootingがTrueに設定された後:)をisShootingながら、プログラムは(whileループで立ち往生ように見えます。ある条件が満たされている場合(例えば、vキーが離された場合)、isShootingをFalseに戻すためにwhileループの条件が必要なようです。

0

ちょうどwhileループを取り除く。 while isShootingを削除してください。プレイヤーはゲームループごとに最大で1回撮影する必要があります。それ以上であれば、射撃の効果を見ることはできません。さらに、無限ループを作成します。

もう1つの注意点として、スタックオーバーフローへようこそ!まだお持ちでない場合は、hereツアーをご覧ください。

+0

ありがとう、私はこのバグを明日(Worcester(UK)に遅れている)しようとしますが、助けてくれてありがとう –

関連する問題