大球吃小球

同學(xué)的,抄來的(先保存,回去看思路,避免以后忘記了不知道怎么做)

原作者: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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、快捷鍵 ctr+b 執(zhí)行ctr+/ 單行注釋ctr+c ...
    o_8319閱讀 6,043評論 2 16
  • 作業(yè): 02.學(xué)生管理系統(tǒng)一部分 2,小結(jié)pygame 基本結(jié)構(gòu) import pygameif name == ...
    Toto_fa49閱讀 366評論 0 4
  • 今天周六本來想和孩子們睡到自然醒的,可是兒子早醒了,我們起床吃過飯,又洗完衣服,帶著孩子們?nèi)ダ夏锛彝媪?,因?yàn)?..
    A寶_8f50閱讀 226評論 0 0
  • 路上、有光的地方我對著你,你看著前面或看著手機(jī)。一刻間,我知道我的出現(xiàn)會有那朵烏云,遮住了你一小片陽光,但是你仍舊...
    無鋒哲子閱讀 189評論 0 0
  • 自律就是改變的行為習(xí)慣。 自律什么時(shí)候都不晚,也不慢,只要現(xiàn)在開始,從心開始,去成為自己想成為的自己。 我們的消極...
    鞋說歪道閱讀 2,015評論 0 0

友情鏈接更多精彩內(nèi)容