美文网首页
12-作业(多个球运动)

12-作业(多个球运动)

作者: 李小萌mmm | 来源:发表于2018-07-30 20:44 被阅读0次

练习:鼠标点击产生球,球碰到边界会弹回,大球碰到小球会吃掉 ,屏幕上超过10个球会结束游戏


# !/usr/bin/env/python
# .*. encoding:utf-8 -*-

import pygame
import random
from math import fabs,pi
import time




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

    #all_balls中保存多个球
    #每个球要保存: 半径,圆心坐标,颜色,x速度,y速度
    all_ball = [
        {'r':random.randint(10,20),
         'pos':(100,200),
         'color':(random.randint(0,255),random.randint(0,255),random.randint(0,255)),
         'x_speed':random.randrange(-3,3,2),
         'y_speed': random.randrange(-3,3,2),
         },
        {
            'r': random.randint(10, 20),
            'pos': (300, 300),
            'color': (random.randint(0,255),random.randint(0,255),random.randint(0,255)),
            'x_speed': random.randrange(-3,3,2),
            'y_speed': random.randrange(-3,3,2),
        }
    ]


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                ball ={
            'r': random.randint(10, 20),
            'pos': event.pos,
            'color': (random.randint(0,255),random.randint(0,255),random.randint(0,255)),
            'x_speed': random.randrange(-5,5,2),
            'y_speed': random.randrange(-5,5,2),
                }

                all_ball.append(ball)


        #刷新界面
        screen.fill((255,255,255))
        for ball_dict in all_ball:
            #取出原来的x坐标和y坐标以及他们的速度
            x ,y  = ball_dict['pos']
            x_speed = ball_dict['x_speed']
            y_speed =ball_dict['y_speed']
            x += x_speed
            y += y_speed
            pygame.draw.circle(screen,ball_dict['color'],(x,y),ball_dict['r']
                               )

            #重新更新球对应的坐标

            ball_dict['pos'] = x,y
            x, y = ball_dict['pos']
            if x+20 >= 800:
                x = 1000 - 20
                ball_dict['x_speed'] *= -1
            if x-20  <= 0:
                ball_dict['x_speed'] *= -1
            if y+20  >= 600:
                y = 800 - 20
                ball_dict['y_speed'] *= -1
            if y-20  <= 0:
                ball_dict['y_speed'] *= -1

        for No_1 in all_ball:
            for No_2 in all_ball:
                if not(No_1 ==No_2):
                    x1,y1 =No_1['pos']
                    x2,y2 =No_2['pos']
                    r1 =No_1['r']
                    r2 = No_2['r']
                    if fabs(x1-x2)<= r1+r2 and fabs(y1-y2)<=r1+r2:
                        if No_1['r']>No_2['r']:
                            No_1['r'] += int(r2/3)
                            all_ball.remove(No_2)
        sum_r =0
        for one_ball in all_ball:
            r = one_ball['r']
            s =pi*r*r
            sum_r += s

        font = pygame.font.Font('../aa.ttf', 40)
        title = font.render('剩余数量:%d' % len(all_ball), True, (0, 0, 0))
        title1 = font.render('圆的总面积:%.2f'%sum_r, True, (0, 0, 0))

        screen.blit(title, (20, 20))
        screen.blit(title1, (20, 60))
        if len(all_ball)>10:
            screen.fill((255,255,255))
            title3 = font.render('球的数量太多游戏结束', True, (0, 0, 0))
            screen.blit(title3, (100, 100))
            time.sleep(2)

        pygame.display.update()
1.png 2.png

相关文章

网友评论

      本文标题:12-作业(多个球运动)

      本文链接:https://www.haomeiwen.com/subject/ayrqvftx.html