2018-09-04 Day12-pygame小游戏

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=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 indexr2:
                                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小游戏)