是否可以复制矩形并且两者都处于不同的位置?

时间:2018-04-26 01:53:21

标签: python pygame

我曾尝试使用.copy()和.move_ip()复制矩形,但所有操作都是移动原始矩形而不创建两个矩形。我也尝试过两次调用pygame.rect.draw但它所做的只是移动原始图像而不留下任何东西。

import pygame
pygame.init()

dis = pygame.display.set_mode((500, 500))
x = 0
y = 500-40
height = 40
width = 100
run = True
vel = 10
clock = pygame.time.Clock()
direction = 'Right'
ran = True
game = 0
def animate(ran, x, y, height, width, direction)#Pygasm:
    if ran:
        if x < vel:
            direction = 'Right'
        elif x > 500-width-vel:
            direction = 'Left'
        if direction == 'Right':
            x += vel
        elif direction == 'Left':
            x -= vel

    dssz = pygame.draw.rect(dis, (255, 0, 0), pygame.Rect(x, y, width, height))

    return x, y, width, height, direction

while run:
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            run = False
        if ev.type == pygame.KEYDOWN:
            if ev.key == pygame.K_UP:
                game+=1
                ran = False
            if ev.key == pygame.K_DOWN:
                game = 0
                pygame.draw.rect(dis, (255,0,0), pygame.Rect(x,y,width,height))
                y -= 40
                ran = True

    dis.fill((0, 0, 0))
    keys = pygame.key.get_pressed() 
    x, y, height, width, direction = animate(ran, x, y, height, width, direction)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

1 个答案:

答案 0 :(得分:1)

很容易,使用Rect.copy 变量

myRect = pygame.Rect(0, 0, 20, 20)
myOtherRect = myRect.copy()
myOtherRect.x, myOtherRect.y = 20, 20