2016-07-21 4 views
-3

私はこの避難民のゲームを持っていて、動作しません。私はフロントを開始する画面を開くことができますが、その後私はそれがクラッシュし、フリーズを開始するために入力を押す。私は単にIDLEの代わりにpython.exeから実行しようとしましたが、その場合はポップアップしてからすぐに終了します。私はそれを実行しようとした最初の数回いくつかのエラーがポップアップしましたが、何が間違っている可能性があることを示すエラーはありません。ただ応答しなくなります。私はここで間違って何をしていますか?パイガムは開けたときにもクラッシュを続けます

import pygame, random, sys 
from pygame.locals import * 


def startGame(): 
    if event.type == K_ENTER: 
     if event.key == K_ESCAPE: 
      sys.exit() 
     return 

def playerCollision(): 
    for a in aliens: 
     if playerRect.colliderect(b['rect']): 
      return True 
    return False 

pygame.init() 

screen = pygame.display.set_mode((750,750)) 
clock = pygame.time.Clock() 
pygame.display.set_caption('Dodge the Aliens') 

font = pygame.font.SysFont(None, 55) 

playerImage = pygame.image.load('') 
playerRect = playerImage.get_rect() 
alienImage = pygame.image.load('') 

drawText('Dodge the Aliens!', font, screen, (750/3), (750/3)) 
drawText('Press ENTER to start.', font, screen, (750/3) - 45, (750/3) + 65) 
pygame.display.update() 

topScore = 0 
while True: 
    aliens = [] 
    score = 0 
    playerRect.topleft = (750 /2, 750 - 50) 
    alienAdd = 0 
    while True: 
     score += 1 
     pressed = pygame.key.get_pressed() 
     if pressed[pygame.K_LEFT]: x -=3 
     if pressed[pygame.K_RIGHT]: x += 3 
     if pressed[pygame.K_ESCAPE]: sys.exit() 
    alienAdd += 1 

    if alienAdd == addedaliens: 
     aliendAdd = 0 
     alienSize = random.randint(10, 40) 
     newAlien = {'rect': pygame.Rect(random.randint(0, 750 - alienSize), 0 -alienSize, alienSize, alienSize), 'speed': random.randint(1, 8), 'surface':pygame.transform.scale(alienImage, (alienSize, alienSize)), } 
     aliens.append(newAlien) 
    for a in aliens[:]: 
     if a['rect'].top > 750: 
      aliens.remove(a) 
    screen.fill(0,0,0) 
    drawText('Score %s' % (score), font, screen, 10, 0) 
    screen.blit(playerImage, playerRect) 
    for a in aliens: 
     screen.blit(b['surface'], b['rect']) 
    pygame.display.update() 


    if playerCollision(playerRect, aliens): 
     if score > topScore: 
      topScore = score 
     break 

    clock.tick(60) 

    drawText('Game Over!', font, screen, (750/3), (750/3)) 
    drawText('Press ENTER To Play Again.', font, screen, (750/3) - 80, (750/3) + 50) 
    pygame.display.update() 
    startGame() 

はここではいくつか インポートpygameのを修正した後、私の新しいコードをだ、pygame.localsからランダムに、SYS はインポート* alienimg = pygame.image.load( 'C:\ Python27 \ alien.png') playerimg = pygame.image.load(「C:\ Python27 \ spaceship.png」)

def playerCollision(): # a function for when the player hits an alien 
    for a in aliens: 
     if playerRect.colliderect(b['rect']): 
      return True 
    return False 

def screenText(text, font, screen, x, y): #text display function 
    textobj = font.render(text, 1, (255, 255, 255)) 
    textrect = textobj.get_rect() 
    textrect.topleft = (x,y) 
    screen.blit(textobj, textrect) 

def main(): #this is the main function that starts the game 

    pygame.init() 
    screen = pygame.display.set_mode((750,750)) 
    clock = pygame.time.Clock() 

    pygame.display.set_caption('Dodge the Aliens') 
    font = pygame.font.SysFont("monospace", 55) 


    pressed = pygame.key.get_pressed() 

    aliens = [] 
    score = 0 
    alienAdd = 0 
    addedaliens = 0 

    while True: #our while loop that actually runs the game 



     for event in pygame.event.get(): #key controls 
      if event.type == KEYDOWN and event.key == pygame.K_ESCAPE: 
       sys.exit() 
      elif event.type == KEYDOWN and event.key == pygame.K_LEFT: 
       playerRect.x -= 3 

      elif event.type == KEYDOWN and event.key == pygame.K_RIGHT: 
       playerRect.x += 3 


      playerImage = pygame.image.load('C:\\Python27\\spaceship.png').convert() # the player images 
     playerRect = playerImage.get_rect() 
     playerRect.topleft = (750 /2, 750 - 50) 
     alienImage = pygame.image.load('C:\\Python27\\alien.png').convert() #alien images 

     alienAdd += 1 

     pygame.display.update() 

     if alienAdd == addedaliens: # randomly adding aliens of different sizes and speeds 
      aliendAdd = 0 
      alienSize = random.randint(10, 40) 
      newAlien = {'rect': pygame.Rect(random.randint(0, 750 - alienSize), 0 -alienSize, alienSize, alienSize), 'speed': random.randint(1, 8), 'surface':pygame.transform.scale(alienImage, (alienSize, alienSize)), } 
      aliens.append(newAlien) 
     for a in aliens[:]: 
      if a['rect'].top > 750: 
       aliens.remove(a) #removes the aliens when they get to the bottom of the screen 

     screen.blit(screen, (0,0)) 
     screenText('Score %s' % (score), font, screen, 10, 0) 
     screen.blit(playerImage, playerRect) 
     for a in aliens: 
      screen.blit(b['surface'], b['rect']) 
     pygame.display.flip() 


     if playerCollision(playerRect, aliens): 
      if score > topScore: 
       topScore = score 
      break 


     clock.tick(60) 

     screenText('Game Over!', font, screen, (750/6), (750/6)) 
     screenText('Press ENTER To Play Again.', font, screen, (750/6) - 80, (750/6) + 50) 
     pygame.display.update() 

main() 

答えて

0

が、私はまだあなたのコードでいくつかの問題を見て、私はあなたが非常にためにあまりにも多く、一度にやろうとしていると思います始まり。できるだけシンプルにしてください。ディスプレイを作成して画像を描画してみてください:

import pygame 

pygame.init() 

display = pygame.display.set_mode((750, 750)) 
img = pygame.image.load("""C:\\Game Dev\\alien.png""") 
display.blit(img, (0, 0)) 
pygame.display.flip() 

もちろん、画像のパスを調整する必要があります。これを実行すると、明示的なエラー(別のスレッドに投稿する必要があります)が表示されるか、画面上にimgが表示されます。しかし、イベント処理がなく、メインループがないので、プログラムは応答しません。これを避けるために

は、次のようなメインループに導入できます。これは、何度も同じIMGを表示するプログラムをもたらすべきである

import sys 
import pygame 

pygame.init() 

RESOLUTION = (750, 750) 
FPS = 60 

display = pygame.display.set_mode(RESOLUTION) 
clock = pygame.time.Clock() 
img = pygame.image.load("""C:\\Game Dev\\alien.png""") 

while True: # <--- game loop 

    # check quit program 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
    # clear the screen 
    display.fill((0, 0, 0)) 
    # draw the image 
    display.blit(img, (0, 0)) 
    # update the screen 
    pygame.display.flip() 
    # tick the clock 
    clock.tick(FPS) 

を、そしてそれは、マウスを使用して適切に終了することができます。しかし、それはまだスクリプトのようなものです。誰かがこれをインポートした場合、すぐに実行されますが、これは私たちが望むものではありません。それでは、同様にその問題を修正しましょう。このようなメイン機能でそれをすべてを包む:

import sys 
import pygame 

#defining some constants 
RESOLUTION = (750, 750) 
FPS = 60 

def main(): # <--- program starts here 

    # setting things up 
    pygame.init() 
    display = pygame.display.set_mode(RESOLUTION) 
    clock = pygame.time.Clock() 
    img = pygame.image.load("""C:\\Game Dev\\alien.png""") 

    while True: # <--- game loop 

     # check quit program 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       sys.exit() 
     # clear the screen 
     display.fill((0, 0, 0)) 
     # draw the image 
     display.blit(img, (0, 0)) 
     # update the screen 
     pygame.display.flip() 
     # tick the clock 
     clock.tick(FPS) 

if __name__ == "__main__": 
    main() 

== 『メインは』場合:」それは輸入だときにプログラムが実行されないことを保証します。

こちらがお役に立てば幸いです。そして、覚えておいてください。少しずつ踏み込んで、あなたのプログラムを支配してください。必要に応じて、1行のコードのたびにprintステートメントを置いて、プログラムの内容と順序を正確に知ることもできます。

+0

あなたに@ndollに関する提案の例を挙げることをおすすめしますか? –

+0

もちろん、私に2〜3分を与えてください... – DonPolettone

+0

ありがとうございました。私はまだパイソンでは最善ではないが、私は学びたいと思っている。だから、明確にするために、whileループは私の主な機能の中にあるべきですか? – ndoll

関連する問題