python:for循环和类

时间:2017-11-01 09:27:09

标签: python pygame

这个程序使盒子移动,但我需要让它们随机移动并且彼此独立, 通过为每个方框调用方法“rect.move()”,但我不知道该怎么做,这可以帮助我。

离。它应该如何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 个答案:

答案 0 :(得分:2)

您需要使用Rect模块随机生成random属性。我修改了你给的代码做了一些修改。

首先,我更改了draw方法,以便它只绘制xy值。

最大的变化是代码中没有大的复杂myList列表,我只是在名为Rect的列表中存储了10个myRects个对象,我认为这些对象要简单得多。< / p>

你可以通过第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()