2017-11-01 4 views
1

このプログラムはボックスを移動させますが、ランダムに移動し、お互いに無関係にする必要があります。 それぞれのボックスに対してメソッド "rect.move()"を呼び出すだけですが、それをする方法を知っているので、私を助けることができます。python:forループとクラス

ex。それはよう見てはいけないかの:https://youtu.be/D7rkcA0-BR0

import pygame 
import random 
# Define some colors 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 
RED = (255, 0, 0) 

class Rect(): 
    def __init__(self): 
     self.rectXPos = 0 
     self.rectYPos = 0 
     self.height = 0 
     self.width = 0 

     self.changeX = 0 
     self.changeY = 0 
     self.x = 0 
     self.y = 0 

    def move(self): 
     self.x += self.changeX 
     self.y += self.changeY 

    def draw(self,screen): 
     pygame.draw.rect(screen,RED,[self.x + self.rectXPos, self.y + self.rectYPos, self.height,self.width]) 



pygame.init() 


# Set the width and height of the screen [width, height] 
size = (700, 500) 
screen = pygame.display.set_mode(size) 

pygame.display.set_caption("My Game") 

# Loop until the user clicks the close button. 
done = False 
# Used to manage how fast the screen updates 
clock = pygame.time.Clock() 

myList =[] 
for i in range(10): 

    rect = Rect() 
    rect.rectXPos = random.randrange(0,700) 
    rect.rectYPos = random.randrange(0,500) 
    rect.height = random.randrange(20,70) 
    rect.width = random.randrange(20,70) 
    rect.changeX = random.randrange(-3,3) 
    rect.changeY = random.randrange(-3,3) 
    myList.append([rect.rectXPos , rect.rectYPos, rect.height, rect.width, rect.changeX, rect.changeY]) 

# -------- Main Program Loop ----------- 
while not done: 
    # --- Main event loop 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
    # --- Game logic should go here 

    # --- Screen-clearing code goes here 

    # Here, we clear the screen to white. Don't put other drawing commands 
    # above this, or they will be erased with this command. 
    # If you want a background image, replace this clear with blit'ing the 
    # background image. 
    screen.fill(WHITE) 

    for i in range(10): 

     rect.rectXPos = myList[i][0] 
     rect.rectYPos = myList[i][1] 
     rect.height = myList[i][2] 
     rect.width = myList[i][3] 
     rect.changeX = myList[i][4] 
     rect.changeY= myList[i][5] 

     rect.draw(screen) 
     rect.move() 

    # --- Drawing code should go here 

    # --- Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 

    # --- Limit to 60 frames per second 
    clock.tick(60) 

# Close the window and quit. 
pygame.quit() 

が、これはあなたが必要なものhttp://programarcadegames.com/

答えて

1

から既成のコードでは、ランダムにrandomモジュールで生成されるRect属性のです。あなたが与えたコードをいくつか変更しました。

まず、drawメソッドを変更して、xyの値を描画するようにしました。

最大の変更点は、コードに大きな複雑なmyListリストの代わりに、Rectという名前のオブジェクトをmyRectsという名前のリストに保存したことです。これははるかに簡単だと思います。

45-52行目から番号を生成すると、もう少し詳しく説明できます。 random.randrange()関数については、こちらをご覧ください:https://docs.python.org/3/library/random.html#functions-for-integers

私はこの回答があなたを助けたと思います!ご質問がありましたら、下記のコメントをお寄せください!

import pygame 
import random 
# Define some colors 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 
RED = (255, 0, 0) 

class Rect(): 
    def __init__(self): 
     self.x = 0 
     self.y = 0 

     self.height = 0 
     self.width = 0 

     self.changeX = 0 
     self.changeY = 0 


    def move(self): 
     self.x += self.changeX 
     self.y += self.changeY 

    def draw(self, screen): 
     pygame.draw.rect(screen, RED, [self.x, self.y, self.width, self.height], 0) 

pygame.init() 

size = (700, 500) 
screen = pygame.display.set_mode(size) 

pygame.display.set_caption("My Game") 

done = False 

clock = pygame.time.Clock() 

myRects = [] 

for i in range(10): 

    rect = Rect() 

    rect.x = random.randrange(0, 700) 
    rect.y = random.randrange(0, 700) 

    rect.width = random.randrange(20, 70) 
    rect.height = random.randrange(20, 70) 

    rect.changeX = random.randrange(-3, 3) 
    rect.changeY = random.randrange(-3, 3) 

    myRects.append(rect) 

print(myRects) 

while not done: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 


    screen.fill(WHITE) 
    for rect in myRects: 
     rect.draw(screen) 
     rect.move() 

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


pygame.quit() 
quit() 
関連する問題