2016-10-07 3 views
0

初めてpygameで何かを作っています... 私は画像のいくつかのインスタンスを生成し、それらを下から上にスクロールしようとしています。何らかの理由で元のイメージを入れ、もう一方のイメージを期待どおりに入れます。私はまた、画面の一番左の1/4または一番右の四半期に画像のインスタンスを生成しようとしています。私の画像は左にしか見えません。私のgameDisplayは1150x500ピクセルです。 私のコードを修正する方法についてのヒントは非常に高く評価されます!ここパイゲームで動画を再現する

は、私が使用している方法です。

def gameHighScore(): 
    points=0 
    guesses=0 
    time=0 

    balloons= pygame.image.load("balloons.png") 

    objects=[] 

    for i in range (4): 
     xpos= random.randrange(0, round(display_width/4)) or random.randrange(round((3/4)*display_width), display_width) 
     #object generated at start height 400, random xpos with a speed -i*2 
     o=Balloons(balloons, 400, xpos ,-i*2)   
     objects.append(o) 


    gamehs=True 
    while gamehs: 
     for event in pygame.event.get(): 
      if event.type==pygame.QUIT: 
       pygame.quit() 
       quit() 
      if event.type==pygame.KEYDOWN: 
       if event.key==pygame.K_SPACE: # space for continue 
        paused=False 
        gameTimer.start() 
       elif event.key==pygame.K_q: #q for quit 
        pygame.quit() 
        quit() 



     for o in objects: 
      gameDisplay.fill(BLACK) 
      message_to_screen("HIGH SCORE", WHITE, y_displace=-100, size="large") 
      message_to_screen(" You scored " +str(points) +" points", WHITE, y_displace=0, size="medium") 
      message_to_screen("in " +str(time) +" after " +str(guesses) +" guesses", WHITE, y_displace=50, size="medium") 
      button("Play (space bar)", 300,400,250,50, GRAY, BLUE, action ="continue") 
      button("Quit (Q)", 600,400,250,50, GRAY, BLUE, action ="quit") 
     for o in objects: 
      o.move() 
      gameDisplay.blit(o.image, o.pos) 


     pygame.display.update() 
     clock.tick(30)    #static screen, no need for high fps 

そしてここでは、オブジェクトを移動したクラスです。

class Balloons: 

    def __init__(self, image, height, xpos, speed): 
     self.speed=speed 
     self.image=image 
     self.xpos=xpos 
     self.pos=image.get_rect().move(xpos, height)    #momve(x,y) change x to random 

    def move(self): 
     self.pos=self.pos.move(0, self.speed) 
     if self.pos.top< -200: 
      self.pos.bottom=800 

答えて

1

ここで起こっていくつかのものがあります。

は特に、彼らは整数であるので、
xpos= random.randrange(0, round(display_width/4)) or random.randrange(round((3/4)*display_width), display_width) 

(3/4)

がゼロと評価さ...このラインと間違っていくつかあります。しかし、より大きな問題は、あなたが使用していると思っていることをしていない orの使用です。 orは、最初の式を左にとり、Falseでない限り使用します。ランドマークピッキング0の場合はこれがFalseになるだけなので、右辺は決して評価されません。あなたは for i in range(4)に基づいて速度を割り当てている:私はあなたのイメージの一つは常に入れたまま、なぜに関して...

# pick a random position in a quarter-size of the screen. 
xpos = int(random.random() * display_width/4) 

# 50% chance of offsetting that position to the right quarter of the screen. 
if random.random() < .5: xpos += display_width * 3/4 

このような何かをするだろう。画像の1つについてiの値が0であるため、速度は0になります。

+1

Python 3の場合、 '3/4'は0に評価されません(これはクラスが継承しないためだと思いますオブジェクト)。また、if-else-statementを実行するのは明らかではありませんか?次に、値を2回割り当てる必要はありません。しかし、それはちょうど意見の問題かもしれません。そうでなければ素晴らしい答え、+1 –

関連する問題