初学pygame,模拟桌面水泡

import pygame as pg
import sys
import random
a=2
b=3
c=6
d=8
e=5
f=4
g=7
h=3
pg.init()
size=width,height=1500,900
bg=(255,255,255)
screen=pg.display.set_mode(size)
pg.display.set_caption("水泡")
image=pg.image.load('2.png')
image2=pg.image.load('3.png')
image3=pg.image.load('4.png')
image4=pg.image.load('5.png')
screen_rect=screen.get_rect() 
position=image.get_rect(centerx=600,centery=200)
position2=image2.get_rect(centerx=400,centery=300)
position3=image3.get_rect(centerx=300,centery=400)
position4=image4.get_rect(centerx=500,centery=600)

while True:
#     speed=[-2,1]
    for event in pg.event.get():
        if event.type==pg.QUIT:
            sys.exit()
    position=position.move(a,b)
    position2=position2.move(c,d)
    position3=position3.move(e,f)
    position4=position4.move(g,h)
    if position.left<0:
#         image=pg.transform.flip(image,True,False)
        a=random.randint(1,5)
    elif position.right>width:
        a=-random.randint(1,5)
    if position2.right>width:
        c=-random.randint(1,5)
    elif position2.left<0:
        c=random.randint(1,6)
    if position3.right>width:
        e=-random.randint(1,7)
    elif position3.left<0:
        e=random.randint(2,7)
    if position4.right>width:
        g=-random.randint(3,8)
    elif position4.left<0:
        g=random.randint(3,8)      
    if position.top<0:
        b=random.randint(1,5)      
    elif position.bottom>height:
        b=-random.randint(1,5)
    if position2.top<0:
        d=random.randint(1,5)
    elif position2.bottom>height:
        d=-random.randint(1,5)
    if position3.top<0:
        f=random.randint(1,5)
    elif position3.bottom>height:
        f=-random.randint(1,5)
    if position4.top<0:
        h=random.randint(1,5)
    elif position4.bottom>height:
        h=-random.randint(1,5)               
#     b=random.randint(1,3)
    flag= pg.Rect.colliderect(position,position2)
    if flag==True:
        a=-a
        b=-b
        c=-c
        d=-d
    flag2=pg.Rect.colliderect(position,position3)
    if flag2==True:
        a=-a
        b=-b
        e=-e
        f=-f
    flag3=pg.Rect.colliderect(position,position4)
    if flag3==True:
        a=-a
        b=-b
        g=-g
        h=-h
    flag4=pg.Rect.colliderect(position2,position3)
    if flag4==True:
        c=-c
        d=-d
        e=-e
        f=-f
    flag5=pg.Rect.colliderect(position2,position4)
    if flag5==True:
        c=-c
        d=-d
        g=-g
        h=-h
    flag6=pg.Rect.colliderect(position3,position4) 
    if flag6==True:
        e=-e
        f=-f
        g=-g
        h=-h           
    screen.fill(bg)
    screen.blit(image,position)
    screen.blit(image2,position2)
    screen.blit(image3,position3)
    screen.blit(image4,position4)
    pg.display.flip()
    pg.time.delay(10)
 

没有封装,写的很烂

    def move(x,y,z):
        position=z.move(x,y)
        if position.left<0:
            x=random.randint(1,5)
        elif position.right>width:
            x=-random.randint(1,5)
        if position.top<0:
            y=random.randint(1,5)      
        elif position.bottom>height:
            y=-random.randint(1,5)
        return x,y
    def getposition(x,y,z):
            position=z.move(x,y)  
            return position

用上面方法进行封装

发布了22 篇原创文章 · 获赞 5 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 终极编程指南 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览