2016-04-14 12 views
0
import pygame 
import random 

screen_width = 500 
screen_height = 500 

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


black = (0, 0, 0) 
red = (255,0,0) 
blue = (0, 0, 255) 


fps = 30 


#the size of our player square 
playerSize = 50 

#rectangles get declared with x, y, w, h 
obstacle = [0, 0, 350, 50] 
obstacle2 = [150, 0, 410, 50] 


obstacleMoveSpeed = 5 
#how fast to move obstalce down 







def check_collision(playerRect, obstacleRect): 
    #convert rectangles into x,y,w,h 
    playerX = playerRect[0] 
    playerY = playerRect[1] 
    playerWidth = playerRect[2] 
    playerHeight = playerRect[3] 

    obstacleX = obstacle[0] 
    obstacleY = obstacle[1] 
    obstacleWidth = obstacle[2] 
    obstacleHeight = obstacle[3] 

    obstacleX2 = obstacle2[0] 
    obstacleY2 = obstacle2[1] 
    obstacleWidth2 = obstacle2[2] 
    obstacleHeight2 = obstacle2[3] 



    #get the right left top and bottom 
    myRight = playerX + playerWidth 
    myLeft = playerX 
    myTop = playerY 
    myBottom = playerY + playerHeight 

    otherRight = obstacleX + obstacleWidth 
    otherLeft = obstacleX 
    otherTop = obstacleY 
    otherBottom = obstacleY + obstacleHeight 

    otherRight2 = obstacleX2 + obstacleWidth2 
    otherLeft2 = obstacleX2 
    otherTop2 = obstacleY2 
    otherBottom2 = obstacleY2 + obstacleHeight2 

    #now the collision code 
    collision = True 

    if ((myRight < otherLeft) or (myLeft > otherRight) or (myBottom < otherTop) or (myTop > otherBottom)): 
     collision = False 

    if ((myRight < otherLeft2) or (myLeft > otherRight2) or (myBottom < otherTop2) or (myTop > otherBottom2)): 
     collision = False 

    return collision 

"""def randomOb(randomize, obstacle, obstacle2): 



       if randomize == 0: 
         pygame.draw.rect(screen, red, obstacle) 
         obstacle = [0, 0, 350, 50] 
         randomize = random.randrange(0,2) 
       if randomize == 1: 
         pygame.draw.rect(screen, red, obstacle2) 
         obstacle2 = [150, 0, 410, 50] 
         randomize = random.randrange(0,2) 

       randomize = random.randrange(0,2) 
       return randomize""" 

def game_loop(): 


     randomize = random.randrange(0,2) 
     playerRect = [(screen_width/2)-(playerSize/2), 400, playerSize, playerSize] 
     #(screen_width/2)-(playerSize/2) just figures out how to center the player 
     while 1: 

       for event in pygame.event.get(): 
         if event.type == pygame.QUIT: 
           exit() 
         if event.type == pygame.KEYDOWN: 
           if event.key == pygame.K_LEFT: 
             playerRect = [(screen_width * 0.2)-(playerSize/2), 400, playerSize, playerSize] 
           if event.key == pygame.K_RIGHT: 
             playerRect = [(screen_width * 0.8)-(playerSize/2), 400, playerSize, playerSize] 
         if event.type == pygame.KEYUP: 
           if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 
             playerRect = [(screen_width/2)-(playerSize/2), 400, playerSize, playerSize] 







       #move obstacle downward 
       obstacle[1] = obstacle[1] + obstacleMoveSpeed 
       obstacle2[1] = obstacle2[1] + obstacleMoveSpeed 



       if (check_collision(playerRect, obstacle)): 
         print("COLLISION") 



       #drawing code 
       screen.fill(black) 


       #draw our player 
       pygame.draw.rect(screen, blue, playerRect) 

       pygame.draw.rect(screen, red, obstacle) 

       """randomOb(randomize, obstacle, obstacle2) 
       if obstacle[1] > 500 or obstacle2[1] > 500: 
         randomize = random.randrange(0,2) 
         randomOb(randomize, obstacle, obstacle2)""" 


       pygame.display.update() 

       clock.tick(fps) 


game_loop() 

私は自分の障害物をランダムに繰り返す必要があります。今、私は2つの障害がありますが、追加します。私はオブジェクトオリエンテーションをこのままにしておきたいと思います。私はこれをしようとするたびに、それは私の障害物を何度も呼び出すランダム化変数を呼び出すか、または障害物を呼び出すが、別の障害物と繰り返さない。Pygame、私は障害物をランダムに繰り返す必要があります

答えて

0

あなたは、ランダムにそれを行うための最も簡単な方法のような、オブジェクトが画面から外れているかどうかをチェックするあなたのゲームループで句を追加することです繰り返して、あなたと同じ障害物をしたい場合:

import pygame 
import random 

screen_width = 500 
screen_height = 500 

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


black = (0, 0, 0) 
red = (255,0,0) 
blue = (0, 0, 255) 

fps = 30 


#the size of our player square 
playerSize = 50 

#rectangles get declared with x, y, w, h 
obstacle = [0, 0, 350, 50] 


obstacleMoveSpeed = 5 
#how fast to move obstalce down 


def check_collision(playerRect, obstacleRect): 
    #convert rectangles into x,y,w,h 
    playerX = playerRect[0] 
    playerY = playerRect[1] 
    playerWidth = playerRect[2] 
    playerHeight = playerRect[3] 

    obstacleX = obstacle[0] 
    obstacleY = obstacle[1] 
    obstacleWidth = obstacle[2] 
    obstacleHeight = obstacle[3] 

    #get the right left top and bottom 
    myRight = playerX + playerWidth 
    myLeft = playerX 
    myTop = playerY 
    myBottom = playerY + playerHeight 

    otherRight = obstacleX + obstacleWidth 
    otherLeft = obstacleX 
    otherTop = obstacleY 
    otherBottom = obstacleY + obstacleHeight 

    #now the collision code 
    collision = True 

    if ((myRight < otherLeft) or (myLeft > otherRight) or (myBottom < otherTop) or (myTop > otherBottom)): 
     collision = False 


    return collision 

"""def randomOb(randomize, obstacle, obstacle2): 



       if randomize == 0: 
         pygame.draw.rect(screen, red, obstacle) 
         obstacle = [0, 0, 350, 50] 
         randomize = random.randrange(0,2) 
       if randomize == 1: 
         pygame.draw.rect(screen, red, obstacle2) 
         obstacle2 = [150, 0, 410, 50] 
         randomize = random.randrange(0,2) 

       randomize = random.randrange(0,2) 
       return randomize""" 

def game_loop(): 


     randomize = random.randrange(0,2) 
     playerRect = [(screen_width/2)-(playerSize/2), 400, playerSize, playerSize] 
     screen_padding = random.randint(0, 100) 
     #(screen_width/2)-(playerSize/2) just figures out how to center the player 
     while 1: 

       for event in pygame.event.get(): 
         if event.type == pygame.QUIT: 
           exit() 
         if event.type == pygame.KEYDOWN: 
           if event.key == pygame.K_LEFT: 
             playerRect = [(screen_width * 0.2)-(playerSize/2), 400, playerSize, playerSize] 
           if event.key == pygame.K_RIGHT: 
             playerRect = [(screen_width * 0.8)-(playerSize/2), 400, playerSize, playerSize] 
         if event.type == pygame.KEYUP: 
           if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 
             playerRect = [(screen_width/2)-(playerSize/2), 400, playerSize, playerSize] 







       #move obstacle downward 
       obstacle[1] = obstacle[1] + obstacleMoveSpeed 



       if (check_collision(playerRect, obstacle)): 
         print("COLLISION") 
       if obstacle[1] > screen_height + screen_padding: 
         obstacle[1] = -50 
         screen_padding = random.randint(0, 100) 
         orientation = bool(random.getrandbits(1)) 
         if orientation: 
           obstacle[0] = 0 
         else: 
           obstacle[0] = 150 



       #drawing code 
       screen.fill(black) 


       #draw our player 
       pygame.draw.rect(screen, blue, playerRect) 

       pygame.draw.rect(screen, red, obstacle) 

       """randomOb(randomize, obstacle, obstacle2) 
       if obstacle[1] > 500 or obstacle2[1] > 500: 
         randomize = random.randrange(0,2) 
         randomOb(randomize, obstacle, obstacle2)""" 


       pygame.display.update() 

       clock.tick(fps) 


game_loop() 

これはブロックが画面の一番上に来るようにしますが、ループの周波数を変更して少し距離を置くこともあります。また、障害物の向きもランダムに変更されます

+0

例をお待ちしております!そして、これは2つの障害以上で動作しますか?あなたのお返事ありがとうございます! – Petras99

+0

あなたは、ブロックごとに個別のステートメントを作成します。だから、あなたが望むだけ多くのブロックにそれを適用することができますが、アイデアはあなたが同時に画面上にたくさんのブロックを持っているということです。 – MANA624

+0

私はパディングを追加しました(これは明らかに上部にあらかじめ定義されていなければなりません)。あなたのゲームにはいくつかのバグがあるので、あなたの方法で何をやっているのか分かりません。なぜあなたのブロックは非常にglitchyで、それを修正する方法を知っていますか?ブロックを描くことは 'if'ステートメントの内側にあることに気付きました。これは巨大なno-noです。それを修正すると、方向をランダム化するのが簡単になります。 – MANA624

関連する問題