美文网首页
day12作业

day12作业

作者: 上邪5415 | 来源:发表于2018-09-05 08:40 被阅读0次

用pygame编写多个球各自移动,两球相撞互相反弹,球碰到边界反弹,球通过点击屏幕产生。

import pygame
import random
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
pygame.display.flip()
circle_list = []
ball_list = [] #接触的球列表
while True:
    pygame.time.delay(10)
    window.fill((255, 255, 255))
    for item in circle_list:
        pygame.draw.circle(window, item['color:'], (item['circle_x:'], item['circle_y:']), 20, 0)
        item['circle_x:'] += item['x_speed:']
        item['circle_y:'] += item['y_speed:']
        if 20<item['circle_x:']<380 and 20<item['circle_y:']<580:
            for other in circle_list:
                if ((item['circle_x:']-other['circle_x:'])**2+(item['circle_y:']-other['circle_y:'])**2)**0.5<=40 and item !=other :
                    item['x_speed:'] *= -1
                    item['y_speed:'] *= -1
                    break
        if item['circle_x:']< 20 :
            item['circle_x:'] = 20
            item['x_speed:'] *= -1
        if item['circle_x:']> 380 :
            item['circle_x:'] = 380
            item['x_speed:'] *= -1
        if item['circle_y:']< 20 :
            item['circle_y:'] = 20
            item['y_speed:'] *= -1
        if item['circle_y:']> 580 :
            item['circle_y:'] = 580
            item['y_speed:'] *= -1

    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
            x, y = event.pos
            count = 0
            for item in circle_list:
                if  ((item['circle_x:']-x)**2+(item['circle_y:']-y)**2)**0.5<=40:
                    count = 1
            if count == 1:
                continue
            list1 = [-2,-1,1,2]
            x_speed = list1[random.randint(0,3)]
            y_speed = list1[random.randint(0,3)]
            if  20<=x<380 and 20<y<580:
                circle_dict = {'color:':color,'circle_x:': x, 'circle_y:': y,'x_speed:':x_speed,'y_speed:':y_speed}
                circle_list.append(circle_dict)

相关文章

  • day12 作业 2018-07-31

    -- coding: utf-8 --"""File Name: day12作业Author : ...

  • Day12卡片法积累素材

    DAY12 学习课程:《Day12:向大师偷艺,如何用纳博科夫卡片法积累一流作家的顶级素材》 今日作业:完成三张写...

  • 【2班3组】Day24,打造个人品牌的实操方法(进阶)

    [学员信息] 22+金色太阳+Day12小作业 [作业要求] 给自己找一个有机会能达到第一的细分领域。 [作业...

  • 求和函数Sumif.Sumifs

    DAY12 求和函数Sumif,Sumifs 回顾目标: 001.听课后,完成作业,及时打卡,复盘。 评估结果: ...

  • 【1班3组】+ Day12《如何提炼出牛逼的文章观点》

    【1班3组】+ Day12《如何提炼出牛逼的文章观点》 【学员信息】311-曲奇小溪-小作业7 大作业1: 可以从...

  • day12作业

    使用Pygame,做一个小游戏。鼠标点击的位置生成一个随机大小、颜色和运动方向的小球,小球碰撞后,半径大的小球吃掉...

  • day12作业

    用pygame编写多个球各自移动,两球相撞互相反弹,球碰到边界反弹,球通过点击屏幕产生。

  • Day12—作业

    pygame大球吃小球

  • day12作业

  • 作业_Day12

    声明一个电脑类属性:品牌、颜色、内存大小方法:打游戏、写代码、看视频 a.创建电脑类的对象,然后通过对象点的方式获...

网友评论

      本文标题:day12作业

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