大球吃小球

同学的,抄来的(先保存,回去看思路,避免以后忘记了不知道怎么做)

原作者:KingJX

import pygame
import random
from math import sqrt
from random import randint

def ball_color():
    return randint(0,255),randint(0,255),randint(0,255)

all_balls = []
radius = 'radius'
center = 'pos'
color = 'color'
x_speed = 'x_speed'
y_speed = 'y_speed'

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    screen.fill((255,255,255))
    pygame.display.flip()

    while True:
        pygame.time.delay(5)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:

                ball = {radius:randint(10,25), center:event.pos, color: ball_color(),x_speed:randint(-2,2),y_speed:randint(-2,2)}
                all_balls.append(ball)

        screen.fill((255,255,255))
        for ball_dict in all_balls:

            x, y = ball_dict[center]
            x_speed1 = ball_dict[x_speed]
            y_speed1 = ball_dict[y_speed]
            x += x_speed1
            y += y_speed1
            if x + ball_dict[radius] >= 800:
                x = 800 - ball_dict[radius]
                x_speed1 *= -1
            if x - ball_dict[radius]  <= 0:
                x = ball_dict[radius]
                x_speed1 *= -1
            if y + ball_dict[radius]  >= 600:
                y = 600 - ball_dict[radius]
                y_speed1 *= -1
            if y - ball_dict[radius]  <= 0:
                y = ball_dict[radius]
                y_speed1 *= -1
            a = all_balls[:]
            for index1 in all_balls:
                for index2 in all_balls:
                    if index1 == index2:
                        continue
                    else:
                        x1,y1 = index1[center]
                        x2,y2 = index2[center]
                        if sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) < index1[radius]+index2[radius]:
                            if index1[radius] < index2[radius]:
                                index2[radius] += index1[radius]
                                all_balls.remove(index1)
                            else:
                                index1[radius] += index2[radius]
                                all_balls.remove(index2)

            pygame.draw.circle(screen, ball_dict[color], (x, y), ball_dict[radius])
            ball_dict[center] = x, y
            ball_dict[x_speed] = x_speed1
            ball_dict[y_speed] = y_speed1
        pygame.display.update()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、快捷键 ctr+b 执行ctr+/ 单行注释ctr+c ...
    o_8319阅读 5,878评论 2 16
  • 作业: 02.学生管理系统一部分 2,小结pygame 基本结构 import pygameif name == ...
    Toto_fa49阅读 326评论 0 4
  • 今天周六本来想和孩子们睡到自然醒的,可是儿子早醒了,我们起床吃过饭,又洗完衣服,带着孩子们去老娘家玩了,因为...
    A宝_8f50阅读 176评论 0 0
  • 路上、有光的地方我对着你,你看着前面或看着手机。一刻间,我知道我的出现会有那朵乌云,遮住了你一小片阳光,但是你仍旧...
    无锋哲子阅读 171评论 0 0
  • 自律就是改变的行为习惯。 自律什么时候都不晚,也不慢,只要现在开始,从心开始,去成为自己想成为的自己。 我们的消极...
    鞋说歪道阅读 1,956评论 0 0