找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4218|回复: 2

[已解决] pygame出现

5

主题

12

帖子

12

积分

贫民

积分
12
TheSkyyy 发表于 2016-12-3 14:13:57 | 显示全部楼层 |阅读模式
代码附录: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



我想画两个圆,然后两个圆碰到边界或者另一个圆速度就会取反,结果出现这个问题,实在摸不着头脑,调了一个多小时了,望指教。



回复

使用道具 举报

1419

主题

1891

帖子

291

积分

侠客

积分
291

最佳新人热心会员默默耕耘

whydo1 发表于 2016-12-3 23:00:16 | 显示全部楼层
if event.type() in (QUIT, KEYDOWN):

改为

if event.type in (QUIT, KEYDOWN):
python3.4.4, win10
回复 支持 反对

使用道具 举报

5

主题

12

帖子

12

积分

贫民

积分
12
TheSkyyy  楼主| 发表于 2016-12-4 17:09:36 | 显示全部楼层
whydo1 发表于 2016-12-3 23:00
if event.type() in (QUIT, KEYDOWN):

改为

多谢版主!!!!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表