小球的碰撞函数问题
import pygameimport sys
import os
import math as m
os.chdir("C:\\Users\\david\\Desktop\\碰撞小球")
from random import *
from pygame.locals import *
class Ball(pygame.sprite.Sprite):
def __init__(self, image, position, speed,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = speed
self.width , self.height = bg_size, bg_size
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left > self.width :
self.rect.left = 0
elif self.rect.top > self.height:
self.rect.bottom = 0
elif self.rect.bottom <0:
self.rect.top = self.height
elif self.rect.right <0:
self.rect.left = self.width
def main():
pygame.init()
ball_image = "gray_ball.png"
bg_image = "background.png"
running = True
bg_size = width, height = 1024,511
screen = pygame.display.set_mode(bg_size)
background = pygame.image.load(bg_image).convert_alpha()
pygame.display.set_caption("nihao")
balls_copy = []
balls = []
for i in range(5):
position = randint(0, width - 100), randint(0, height - 100)
speed =
ball = Ball(ball_image, position, speed, bg_size)
balls.append(ball)
clock = pygame.time.Clock()
balls_copy = balls
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background,(0,0))
#这里是各个小球碰撞后要执行的动作,反弹
for eachball in balls:
ball_index = 0
item = balls.pop(ball_index)
for each in balls:
distance = m.sqrt(m.pow((item.rect.center - each.rect.center), 2) + m.pow(
(item.rect.center - each.rect.center), 2))
if distance <= (item.rect.width + each.rect.width) / 2:
each.speed, each.speed = - \
each.speed, -each.speed
item.speed, item.speed = - \
item.speed, -item.speed
balls = balls_copy
for each in balls:
screen.blit(each.image,each.rect)
each.move()
pygame.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()
为何代码一执行,就炸了{:9_241:} 问题出在以下这段代码中:
balls_copy = balls
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background,(0,0))
#这里是各个小球碰撞后要执行的动作,反弹
for eachball in balls:
ball_index = 0
item = balls.pop(ball_index)
for each in balls:
distance = m.sqrt(m.pow((item.rect.center - each.rect.center), 2) + m.pow(
(item.rect.center - each.rect.center), 2))
if distance <= (item.rect.width + each.rect.width) / 2:
each.speed, each.speed = -each.speed, -each.speed
item.speed, item.speed = -item.speed, -item.speed
balls = balls_copy
for each in balls:
screen.blit(each.image,each.rect)
each.move()
pygame.display.flip()
clock.tick(30)
问题在于,你在循环中使用了 balls_copy = balls ,然后在每次循环开始时将 balls 列表清空,再将 balls_copy 赋值给 balls 。这会导致 balls 列表在每次循环开始时都为空,因此在执行 for eachball in balls: 这一行代码时会报错。
要解决这个问题,你可以使用 balls_copy = balls.copy() 来创建一个 balls 列表的副本,而不是直接将 balls 赋值给 balls_copy 。这样, balls_copy 将保留 balls 列表的初始状态,而 balls 列表在循环中被修改时不会影响到 balls_copy 。
修改后的代码如下:
balls_copy = balls.copy()
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background,(0,0))
#这里是各个小球碰撞后要执行的动作,反弹
for eachball in balls:
ball_index = 0
item = balls.pop(ball_index)
for each in balls:
distance = m.sqrt(m.pow((item.rect.center - each.rect.center), 2) + m.pow(
(item.rect.center - each.rect.center), 2))
if distance <= (item.rect.width + each.rect.width) / 2:
each.speed, each.speed = -each.speed, -each.speed
item.speed, item.speed = -item.speed, -item.speed
balls = balls_copy.copy()
for each in balls:
screen.blit(each.image,each.rect)
each.move()
pygame.display.flip()
clock.tick(30)
这样修改后,你的代码应该就能正常执行了。
页:
[1]