2016-07-15 2 views
0

私はPythonで新しいので、私のコードの問題はおそらく愚かなものです。 プレイヤーのイメージが動いている直後に、IDLEにエラーメッセージなしでパイゲームのウィンドウがクラッシュしています。pygameクラッシュ直後のアクション

imはPython 2.7を使用しています。 は、ここでは、コードです:

import pygame,sys 
from pygame.locals import * 

pygame.init() 
dis=pygame.display.set_mode((1084,638),0,32) 
pygame.display.set_caption('ledders and snakes') 

FPS=30 
fpsClock=pygame.time.Clock() 
White=(255,255,255) 
img=pygame.image.load('smal.gif') 
bg = pygame.image.load("under.gif") 

cax=150 
cay=150 
di='right' 
flag=True 

while flag: 
    dis.blit(bg, (0, 0)) 
    if di=='right': 
     cax+=10 
     cay-=10 
     if cax==280: 
      di='down' 
    elif di=='down': 
     cay+=10 
     cax+=10 
     if cax==410: 
      flag=False 


    dis.blit(img,(cax,cay)) 

    for event in pygame.event.get(): 
     if event.type==QUIT: 
      pygame.quit() 
      sys.exit() 

    pygame.display.update() 
    fpsClock.tick(FPS) 
+0

コマンドラインから実行すると機能しますか? IDLE(これはTkでもあります)上で実行されているTkベースのアプリケーションでは、時には問題があります。 – cdarke

答えて

0

私はあなたのプログラムを見て、flag = Falseif cax == 410に設定することが問題です。そのため、数秒後に条件がTrueになるため、プログラムが終了します。しかし、そこに考慮すべき多くのものがあるので、私はいくつかの変更(ないプログラムではなく名前で)作られた '=='として、または(0 'のようにコンマの後に事業者の間にスペースを入れて、また

import pygame 
import sys 
from pygame.locals import * 

pygame.init() 
SIZE = WIDTH, HEIGHT = 1084, 638 # Have a reference to WIDTH and HEIGHT. 
screen = pygame.display.set_mode(SIZE, 0, 32) # 'screen' is the convention and describes the variable better than 'dis'. 
pygame.display.set_caption('ledders and snakes') 

FPS = 30 
clock = pygame.time.Clock() # Use lowercase for variables. 
WHITE = (255, 255, 255) # Use CAPS for constants. 
img = pygame.Surface((32, 32)) # Used these two lines because I don't have your image. 
img.fill((0, 0, 255)) 
bg = pygame.Surface((32, 32)) # Used these two lines because I don't have your image. 
bg.fill((255, 0, 0)) 

cax = 150 # Since I don't know your program I can't tell if these are describable variables or not. 
cay = 150 
direction = 'right' # 'di' isn't describable enough. 
running = True # 'flag' seems like an indescribable variable. 

while running: 
    screen.blit(bg, (0, 0)) 
    if direction == 'right': 
     cax += 10 
     cay -= 10 
     if cax == 280: 
      direction = 'down' 
    elif direction == 'down': 
     cay += 10 
     cax += 10 
     if cax == 410: 
      running = False 

    screen.blit(img, (cax, cay)) 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    pygame.display.update() 
    clock.tick(FPS) 

を、0) '(0,0)の代わりに。変数名には小文字を使用し、単語はアンダースコアで区切ります。 PEP8に従ってください。ただし、それを壊すのが理にかなっていない限り。

+0

そして、プログラムが数秒後にループから抜け出すのを知っていますが、それはちょうどクラッシュした直後です。 – roni280

+0

@ roni280プログラムがクラッシュすることはありませんが、コードが不足して終了します。 whileループから抜けて終了します。クラッシュした場合はエラーになります。 –

0

私はちょうどあなたのプログラムの上に脱脂、私は(うまくいけば私はありません)間違っていないよ場合は、CAXに10を追加し続ける、結果はあなたのプレイヤーがいるだろう位置に到達したということですあなたのwhileループでちょうど26反復後にフラグをfalseに設定します。 これはかなり速く起こります。

+0

しかし、ループを終了した直後にクラッシュするのはなぜですか? – roni280

+0

あなたのwhileループは、画面をブリッジして更新しています。 – marcb20012

+0

いいえループ中、blittingおよび更新はありませんので、あなたのprogrammがまだ開かれているとは思わないでください。備考ありがとう! – marcb20012

関連する問題