在 class 中使用碰撞列表
Use collidelist in class
我创建了一个 class 来创建矩形并将它们放入列表中。我不希望它们发生碰撞,所以我使用了 collidelist 但它不是 working.rectangles 仍在碰撞。
我还希望矩形在击中特定点时向下移动并改变 x 位置,
我可以这样做,但我不确定它是否会阻止 collidelist 工作
查看下面的代码以获得更多说明。
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
x = random.randrange(10,900)
y = random.randrange(10,900)
sy = 10
w = random.randrange(40,90)
h = random.randrange(40,90)
rectangles =[]
class Rectangle:
def __init__(self ,color ,x,y,w,h):
self.c = color
self.x = x
self.y = y
self.w = w
self.h = h
self.rect =pygame.Rect(self.x ,self.y ,self.w ,self.h)
def draw(self):
pygame.draw.rect(display , self.c ,(self.x ,self.y ,self.w,self.h))
self.y += sy
if self.y > height:
self.y = -25
self.x = random.randint(10,900)
return self.rect
return self.rect
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c ,r_x,r_y,r_w,r_h)
rectangles.append(rectangle)
while not run:
display.fill(a)
p =pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles:
if rectangle.rect.collidelist(rectangles) > -1:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
collidelist()
evaluates the collisions between a singe pygame.Rect
个对象和 pygame.Rect
个对象的列表。
从 class 中删除属性 .x
、.y
、.w
和 .h
,但添加新方法更新:
class Rectangle:
def __init__(self, color, x, y, w, h):
self.c = color
self.rect = pygame.Rect(x, y, w, h)
def update(self):
self.rect.y += sy
if self.rect.y > height:
self.rect.y = -25
self.rect.x = random.randint(10,900)
def draw(self):
pygame.draw.rect(display, self.c, self.rect)
在碰撞测试之前,您必须生成 pygame.Rect
个对象的列表。由于每个矩形都在列表中,因此碰撞测试总是会找到至少一个矩形(它本身)。使用 collidelistall()
并测试碰撞矩形的数量是否小于 2:
while not run:
# [...]
for rectangle in rectangles:
rectangle.update()
rectlist = [r.rect for r in rectangles]
if len(rectangle.rect.collidelistall(rectlist)) < 2:
rectangle.draw()
无论如何,我建议创建不相交的矩形。初始化时:
rectangles = []
rectlist = []
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
create_new = True
while create_new:
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c, r_x,r_y,r_w,r_h)
create_new = rectangle.rect.collidelist(rectlist) > -1
rectangles.append(rectangle)
rectlist.append(rectangle.rect)
而在classRectangle
的方法update
中:
class Rectangle:
# [...]
def update(self, rectangles):
self.rect.y += sy
if self.rect.y > height:
rectlist = [r.rect for r in rectangles if r != self]
self.rect.y = -25
self.rect.x = random.randint(10,900)
while self.rect.collidelist(rectlist) > -1:
self.rect.x = random.randint(10,900)
完整示例:
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
sy = 10
class Rectangle:
def __init__(self, color, x, y, w, h):
self.c = color
self.rect = pygame.Rect(x, y, w, h)
def update(self, rectangles):
self.rect.y += sy
if self.rect.y > height:
rectlist = [r.rect for r in rectangles if r != self]
self.rect.y = -25
self.rect.x = random.randint(10,900)
while self.rect.collidelist(rectlist) > -1:
self.rect.x = random.randint(10,900)
def draw(self):
pygame.draw.rect(display, self.c, self.rect)
rectangles = []
rectlist = []
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
create_new = True
while create_new:
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c, r_x,r_y,r_w,r_h)
create_new = rectangle.rect.collidelist(rectlist) > -1
rectangles.append(rectangle)
rectlist.append(rectangle.rect)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles[:]:
rectangle.update(rectangles)
display.fill(a)
p = pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for rectangle in rectangles:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
我创建了一个 class 来创建矩形并将它们放入列表中。我不希望它们发生碰撞,所以我使用了 collidelist 但它不是 working.rectangles 仍在碰撞。
我还希望矩形在击中特定点时向下移动并改变 x 位置,
我可以这样做,但我不确定它是否会阻止 collidelist 工作
查看下面的代码以获得更多说明。
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
x = random.randrange(10,900)
y = random.randrange(10,900)
sy = 10
w = random.randrange(40,90)
h = random.randrange(40,90)
rectangles =[]
class Rectangle:
def __init__(self ,color ,x,y,w,h):
self.c = color
self.x = x
self.y = y
self.w = w
self.h = h
self.rect =pygame.Rect(self.x ,self.y ,self.w ,self.h)
def draw(self):
pygame.draw.rect(display , self.c ,(self.x ,self.y ,self.w,self.h))
self.y += sy
if self.y > height:
self.y = -25
self.x = random.randint(10,900)
return self.rect
return self.rect
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c ,r_x,r_y,r_w,r_h)
rectangles.append(rectangle)
while not run:
display.fill(a)
p =pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles:
if rectangle.rect.collidelist(rectangles) > -1:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
collidelist()
evaluates the collisions between a singe pygame.Rect
个对象和 pygame.Rect
个对象的列表。
从 class 中删除属性 .x
、.y
、.w
和 .h
,但添加新方法更新:
class Rectangle:
def __init__(self, color, x, y, w, h):
self.c = color
self.rect = pygame.Rect(x, y, w, h)
def update(self):
self.rect.y += sy
if self.rect.y > height:
self.rect.y = -25
self.rect.x = random.randint(10,900)
def draw(self):
pygame.draw.rect(display, self.c, self.rect)
在碰撞测试之前,您必须生成 pygame.Rect
个对象的列表。由于每个矩形都在列表中,因此碰撞测试总是会找到至少一个矩形(它本身)。使用 collidelistall()
并测试碰撞矩形的数量是否小于 2:
while not run:
# [...]
for rectangle in rectangles:
rectangle.update()
rectlist = [r.rect for r in rectangles]
if len(rectangle.rect.collidelistall(rectlist)) < 2:
rectangle.draw()
无论如何,我建议创建不相交的矩形。初始化时:
rectangles = []
rectlist = []
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
create_new = True
while create_new:
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c, r_x,r_y,r_w,r_h)
create_new = rectangle.rect.collidelist(rectlist) > -1
rectangles.append(rectangle)
rectlist.append(rectangle.rect)
而在classRectangle
的方法update
中:
class Rectangle:
# [...]
def update(self, rectangles):
self.rect.y += sy
if self.rect.y > height:
rectlist = [r.rect for r in rectangles if r != self]
self.rect.y = -25
self.rect.x = random.randint(10,900)
while self.rect.collidelist(rectlist) > -1:
self.rect.x = random.randint(10,900)
完整示例:
import pygame
import random
from pygame.locals import *
import time
pygame.init()
a = 255,255,255
b = 0,0,0
c = 80,255,0
d = 0,125,125
r1 = (b,c,d)
r = random.choice(r1)
p_x = 500
p_y = 1399
width = 500
height = 1890
display = pygame.display.set_mode((width,height))
title = pygame.display.set_caption("Game")
clock = pygame.time.Clock()
run = False
exit_game = False
sy = 10
class Rectangle:
def __init__(self, color, x, y, w, h):
self.c = color
self.rect = pygame.Rect(x, y, w, h)
def update(self, rectangles):
self.rect.y += sy
if self.rect.y > height:
rectlist = [r.rect for r in rectangles if r != self]
self.rect.y = -25
self.rect.x = random.randint(10,900)
while self.rect.collidelist(rectlist) > -1:
self.rect.x = random.randint(10,900)
def draw(self):
pygame.draw.rect(display, self.c, self.rect)
rectangles = []
rectlist = []
for count in range(5):
r_c = random.randint(0,255) , random.randint(0,255) , random.randint(0,255)
create_new = True
while create_new:
r_x = random.randint(10,900)
r_y = random.randint(10,79)
r_w = random.randint(60,100)
r_h = random.randint(40,80)
rectangle = Rectangle(r_c, r_x,r_y,r_w,r_h)
create_new = rectangle.rect.collidelist(rectlist) > -1
rectangles.append(rectangle)
rectlist.append(rectangle.rect)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
p_x -= 60
if event.key == pygame.K_p:
p_x += 60
for rectangle in rectangles[:]:
rectangle.update(rectangles)
display.fill(a)
p = pygame.draw.rect(display,c,(p_x ,p_y ,56,56))
for rectangle in rectangles:
rectangle.draw()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()