|
代码附录:import pygame as p #导库
from pygame.locals import *
import sys
import math as m
import random as r
p.init()
screen = p.display.set_mode((900, 900))
p.display.set_caption('碰撞检测')
# 设置两个圆初始的位置
pos1_x, pos1_y = 100, 100
pos2_x, pos2_y = 800, 800
# 速度
vel1_x, vel1_y = 1, 1
vel2_x, vel2_y = 1, 2
A, B, C = 188, 188, 188
while True:
for event in p.event.get():
if event.type() in (QUIT, KEYDOWN):
sys.exit()
screen.fill(255, 255, 255)
color = A, B, C
pos1_x += vel1_x
pos1_y += vel1_y
pos2_x += vel2_x
pos2_y += vel2_y
distance = m.sqrt((pos1_x-pos2_x)**2+(pos1_y-pos2_y)**2) #两圆心之间的距离
if pos1_x > 900 or pos1_x < 0 or distance < 120: #如果超出边界或者两圆碰撞则速度都取反
vel1_x, vel1_y = -vel1_x, -vel1_y
vel2_x, vel2_y = -vel2_x, -vel2_y
A, B, C = r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)#RGB随机
pos1 = pos1_x, pos1_y
pos2 = pos2_x, pos2_y
radius1, radius2 = 50, 50
width1, width2 = 10, 10
p.draw.circle(screen, color, pos1, radius1, width1)
p.draw.circle(screen, color, pos2, radius2, width2)
p.display.update()
错误信息:[size=16.6667px]Traceback (most recent call last): File "F:/pycharm/pygame_collision detection.py", line 17, in <module> if event.type() in (QUIT, KEYDOWN):TypeError: 'int' object is not callable
我想画两个圆,然后两个圆碰到边界或者另一个圆速度就会取反,结果出现这个问题,实在摸不着头脑,调了一个多小时了,望指教。
|
|