Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame对象不遵循物理_Python_Object_Pygame_Instance - Fatal编程技术网

Python Pygame对象不遵循物理

Python Pygame对象不遵循物理,python,object,pygame,instance,Python,Object,Pygame,Instance,我正在做一个pygame作业,我必须在屏幕上添加两个矩形,让它们反弹并相互碰撞。我甚至还没有开始碰撞方面,因为当我添加两个矩形时,只有一个开始移动 import pygame import sys import random as rn pygame.init() BLACK = ( 0,0,0) WHITE = (255,255,255) BLUE = (0,0,255) RED = (255,0,0) YELLOW = (255,255,0) num_of_rects = 2 co

我正在做一个pygame作业,我必须在屏幕上添加两个矩形,让它们反弹并相互碰撞。我甚至还没有开始碰撞方面,因为当我添加两个矩形时,只有一个开始移动

import pygame
import sys
import random as rn

pygame.init()

BLACK = ( 0,0,0)
WHITE = (255,255,255)
BLUE =  (0,0,255)
RED =   (255,0,0)
YELLOW = (255,255,0)
num_of_rects = 2
colors = [BLACK, BLUE, RED, YELLOW]

def doRectsOverlap(rect1, rect2):
    for a,b in [(rect1,rect2),(rect2,rect1)]:
        if ((isPointInsideRect(a.left, a.top, b)) or (isPointInsideRect(a.left, a.bottom, b)) or (isPointInsideRect(a.right, a.top, b)) or (isPointInsideRect(a.right, a.bottom, b))):
            return True

    return False
def isPointInsideRect(x,y,rect):
    if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
        return True
    else:
        return False

class Rectangle:
    """
    Refer to the instructions if you need to modify this.
    """

    def __init__(self, color, loc):
        self.loc = loc
        self.color = color

    def my_draw(self,screen):
        pygame.draw.rect(screen, self.color, self.loc)


    def my_move(self,xoffset,yoffset):
        self.loc = [self.loc[0]+xoffset,self.loc[1]+yoffset] + self.loc[2:]


size = [300, 300]   
screen = pygame.display.set_mode(size)
pygame.display.set_caption("C200 CHANGE")

rects = []
for i in range(2):
    rects.append(Rectangle(rn.choice(colors), [rn.randint(0,200), rn.randint(0, 200), 20, 20]))




if __name__ == "__main__":
    xd = 2
    yd = 2
    while True:
        pygame.time.wait(40)

        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                pygame.quit()
                sys.exit()
        screen.fill(WHITE)

        for r in rects:
            r.my_draw(screen)

        if r.loc[0] > 280:
            xd = -rn.randint(1,3)
        if r.loc[1] > 280:
            yd = -rn.randint(1,3)
        if r.loc[0] < 10:
            xd = rn.randint(1,3)
        if r.loc[1] < 10:
            yd = rn.randint(1,3)
        r.my_move(xd,yd)

        pygame.display.flip()
导入pygame
导入系统
将随机导入为rn
pygame.init()
黑色=(0,0,0)
白色=(255255)
蓝色=(0,0255)
红色=(255,0,0)
黄色=(255255,0)
矩形的数量=2
颜色=[黑色、蓝色、红色、黄色]
def doRectsOverlap(rect1、rect2):
对于[(rect1,rect2),(rect2,rect1)]中的a,b:
如果((i点内部竖立(a.left,a.top,b))或(i点内部竖立(a.left,a.bottom,b))或(i点内部竖立(a.right,a.top,b))或(i点内部竖立(a.right,a.bottom,b)):
返回真值
返回错误
def ISPOINT(x,y,rect):
如果(x>rect.left)和(xrect.top)和(y280:
xd=-rn.randint(1,3)
如果r.loc[1]>280:
yd=-rn.randint(1,3)
如果r.loc[0]<10:
xd=rn.randint(1,3)
如果r.loc[1]<10:
yd=rn.randint(1,3)
r、 我的移动(xd,yd)
pygame.display.flip()
我发现只有矩形类的第二个实例是移动的矩形。通过添加颜色列表并指定特定的颜色,例如,第一个矩形是红色的,第二个是黑色的,只有黑色的开始移动。

这是一个问题。
if
语句需要在
for
-循环中而不是在
for
-循环后

如果名称=“\uuuuu main\uuuuuuuu”:
xd=2
yd=2
尽管如此:
pygame.时间.等等(40)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
屏幕填充(白色)
对于矩形中的r:
r、 我的画(屏幕)
#-->|压痕
如果r.loc[0]>280:
xd=-rn.randint(1,3)
如果r.loc[1]>280:
yd=-rn.randint(1,3)
如果r.loc[0]<10:
xd=rn.randint(1,3)
如果r.loc[1]<10:
yd=rn.randint(1,3)
r、 我的移动(xd,yd)
pygame.display.flip()

我猜您希望将所有这些if语句放入for循环中。换句话说,缩进if语句。我同意。这只是一个问题。预期移动矩形的代码在-for循环的
之后执行一次。因此,只移动
rects
中的最后一个矩形。将代码放入循环中,以解决问题。