美文网首页
2018-09-04 Day12-pygame小游戏

2018-09-04 Day12-pygame小游戏

作者: Deathfeeling | 来源:发表于2018-09-05 20:36 被阅读0次
1、小球碰撞
"""__author__=星辰"""
import pygame
import random
from time import sleep
if __name__=='__main__':
    pygame.init()
    window = pygame.display.set_mode((800,600))
    window.fill((255,255,255))
    pygame.display.flip()
    list_balls=[]
    while True:

        if list_balls:
            for ball in list_balls:
                x,y=ball['pos']
                r=ball['re']
                index=list_balls.index(ball)
                if index<len(list_balls)-1:
                    while index+1<len(list_balls):
                        ball2=list_balls[index+1]
                        x2,y2=ball2['pos']
                        if (x2-x)**2+(y2-y)**2<=(2*r)**2:
                            if x>=x2:
                                ball['xs']=1
                                ball2['xs']=-1
                            if x<=x2:
                                ball['xs'] = -1
                                ball2['xs'] = 1

                            if y>=y2:
                                ball['ys']=-1
                                ball2['ys']=1
                            if y<=y2:
                                ball['ys']=1
                                ball2['ys']=-1
                        index +=1

                if x+r>=800:
                    xs = -1
                    ball['xs']=xs
                if x-r<=0:
                    xs = 1
                    ball['xs'] = xs
                if y+r>=600:
                    ys = -1
                    ball['ys']=ys
                if y-r<=0:
                    ys=1
                    ball['ys'] = ys
                x += ball['xs']
                y += ball['ys']
                ball['pos']=(x,y)
                pygame.draw.circle(window,ball['rgb'],(x,y),ball['re'],ball['width'])
            sleep(0.01)
            pygame.display.flip()

            window.fill((255,255,255))


        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit(0)
            if event.type==pygame.MOUSEBUTTONDOWN:
                dic_ball = {}
                dic_ball['rgb']=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
                dic_ball['pos']=event.pos
                dic_ball['re']=20
                dic_ball['width']=0
                dic_ball['xs']=[-1,1][random.randint(0,1)]
                dic_ball['ys']=[-1,1][random.randint(0,1)]
                # pygame.draw.circle(window,dic_ball['rgb'],dic_ball['pos'],dic_ball['re'],dic_ball['width'])
                list_balls.append(dic_ball)
                pygame.display.flip()
                sleep(0.01)
                window.fill((255,255,255))
2、接球游戏
import pygame
import random
from time import sleep
if __name__=='__main__':
    pygame.init()
    mx = 0
    my = 300
    m_width=100
    m_height=10
    window = pygame.display.set_mode((600,400))
    window.fill((255,255,255))
    pygame.draw.rect(window,(255,0,0),(mx,my,m_width,m_height),0)
    pygame.display.flip()



    xs=1
    ys=1
    x=random.randint(0,600)
    y=random.randint(0,400)


    while True:
        pygame.draw.circle(window, (255, 0, 0), (x, y), 20, 0)
        pygame.draw.rect(window, (255, 0, 0), (mx, my, m_width, m_height), 0)
        pygame.display.update()  # 可以刷新指定的范围
        x +=xs
        y +=ys
        window.fill((255,255,255))
        if mx<=(x+20)<=mx+m_width and y+20>=300 :
            if xs>0:
                xs = 1
            else:
                xs = -1
            ys = -1
        elif x+20==600 :
            xs = -1
        if (x-20)==0:
            xs = 1
        if (y-20)==0:
            ys = 1
        if (y+20)>320:
            exit(0)


        sleep(0.01)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit(0)
            if event.type == pygame.MOUSEMOTION:
                mx,my=event.pos
                mx=mx-m_width/2
                my=300

3、大球吃小球
import pygame
import random
from time import sleep
if __name__=='__main__':
    pygame.init()
    window = pygame.display.set_mode((800,600))
    window.fill((255,255,255))
    pygame.display.flip()
    list_balls=[]
    while True:

        if list_balls:
            for ball in list_balls:
                x,y=ball['pos']
                r1=ball['re']
                if r1>=150:
                    ball['re']=10
                    r1=10
                index=list_balls.index(ball)
                if index<len(list_balls)-1:
                    while index+1<len(list_balls):
                        ball2=list_balls[index+1]
                        r2 = ball2['re']
                        x2,y2=ball2['pos']
                        if (x2-x)**2+(y2-y)**2<=(r1+r2)**2:
                            if r1>r2:
                                ball['re']=int(r1+r2/2)
                                list_balls.remove(ball2)
                            if r1<=r2:
                                ball2['re'] =int(r2 + r1 / 2)
                                list_balls.remove(ball)
                        index +=1

                if x+r1>=800:
                    xs = -1
                    ball['xs']=xs
                if x-r1<=0:
                    xs = 1
                    ball['xs'] = xs
                if y+r1>=600:
                    ys = -1
                    ball['ys']=ys
                if y-r1<=0:
                    ys=1
                    ball['ys'] = ys
                x += ball['xs']
                y += ball['ys']
                ball['pos']=(x,y)
                pygame.draw.circle(window,ball['rgb'],(x,y),ball['re'],ball['width'])
            sleep(0.001)
            pygame.display.flip()

            window.fill((255,255,255))


        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                exit(0)
            if event.type==pygame.MOUSEBUTTONDOWN:
                dic_ball = {}
                dic_ball['rgb']=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
                dic_ball['pos']=event.pos
                dic_ball['re']=random.randint(10,30)
                dic_ball['width']=0
                dic_ball['xs']=[-1,1][random.randint(0,1)]
                dic_ball['ys']=[-1,1][random.randint(0,1)]
                # pygame.draw.circle(window,dic_ball['rgb'],dic_ball['pos'],dic_ball['re'],dic_ball['width'])
                list_balls.append(dic_ball)
                pygame.display.flip()
                sleep(0.01)
                window.fill((255,255,255))

相关文章

  • 2018-09-04 Day12-pygame小游戏

    1、小球碰撞 2、接球游戏 3、大球吃小球

  • 2018-09-04 day12-pygame

    总结 一.pygame的基本操作 二.显示图片 三.形变 四.显示文字 五.显示图片 六.事件 七.动画原理 八....

  • 2018-09-04 Day12-pygame模块 学习

    安装:pip install Pygame 1、初始化游戏模块 2、创建游戏窗口 set_mode((100, 2...

  • 2018-09-04

    2018-09-04 万千工品金秀 2018-09-04 00:21 · 字数 932 · 阅读 0 · 日记本 ...

  • 2018-09-04

    戴师傅 2018-09-04 2018-09-04 20:32 打开App (稻盛哲学学习会)打卡第126天 姓名...

  • 180904

    2018-09-04 #The Sense of Style The Sense of Style is not ...

  • day12-pygame

      今天主要学习了pygame的相关知识。   一、pygame的基本操作 初始化游戏模块 创建游戏窗口 让游戏一...

  • day12-pygame

    1、安装pygame并初步使用pygame a、在pyfile ---setting---project ----...

  • 柳叶湖

    柳叶湖 胡99(胡建中) 2018-09-04 柳叶湖畔柳丝纤 ...

  • 统计日志记录最少天数

    NVIDIA笔试2018-09-04思路 统计日志记录最少天数 题目: InputThe first input ...

网友评论

      本文标题:2018-09-04 Day12-pygame小游戏

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