python入门学习记录之旅,pygame实现简单动画游戏:大球吃小球

引言:

文章python示例主要是跟着CSDN博客专家:骆昊来学习python开发的,有兴趣的可以学习一下

骆昊:<<https://blog.csdn.net/jackfrued>>

python在(GUI)图形界面的开发也有相当不错的框架,如默认的tkinter,wxPython,PyQt,pygame,本文主要介绍pygame的用法pygame专注于多媒体应用的开发,如:电子游戏,图像,声音,视频,碰撞检测,事件处理上都有不错的支持,pygame的基础是基于SDL库,SDL是一套由C开发的跨平台多媒体库,被广泛运用在多媒体应用开发上,pygame的开发者可以不再被底层的开发语言束缚,可以更专注的游戏功能和逻辑方面。

一:

主要实现:

1,开启事件监听,监听鼠标点击事件,获取点击的x,y轴,分配随机颜色,随机移动的位置和速度

2,将所有球放入容器,遍历容器里的球,在屏幕进行绘制(draw)

3,移动球体,遍历所有球,若当前球体比相邻球体大则吃掉,增长当前球体的大小,本例的碰撞检测(可根据俩个球的x,y轴位置相减是否<=俩球的半径之和)来实现。

4,进行球体移动的位置是否大于或小于屏幕位置的判断来实现球体的自然循环。

2:代码示例

#颜色
class Color():

    RED=(255,0,0)
    GREEN=(255,255,0)
    BULE=(0,0,255)
    #随机颜色
    @staticmethod
    def randintColor():
        r = randint(0,255)
        g = randint(0,255)
        b = randint(0,255)
        return r,g,b
#球类
class Ball(object):

    #初始化球类
    def __init__(self,x,y,sx,sy,radius,color=Color.RED):
        self.x=x #x
        self.y=y #y
        self.sx=sx #每次移动的x
        self.sy=sy #每次移动的y
        self.radius=radius #半径
        self.color=color #颜色
        self.alive=True  #是否存活球
    #移动
    def move(self,screen):
            self.x += self.sx
            self.y +=self.sy
            # x轴
            if self.x - self.radius <= 0 or \
                    self.x + self.radius >= screen.get_width():
                    if self.x-self.radius<=0:
                        self.x+=self.radius-self.x+1
                    else:
                        self.x += -(self.x+self.radius-screen.get_width()+1)
                    self.sx = -self.sx
            #y轴
            if self.y - self.radius <= 0 or \
                    self.y + self.radius >= screen.get_height():
                if self.y - self.radius <= 0:
                    self.y += self.radius-self.y+1
                else:
                    self.y +=-(self.y+self.radius-screen.get_height()+1)
                self.sy = -self.sy



    #吃掉
    def eat(self,other):
        if self.alive and other.alive and self!=other:
            dx,dy = self.x-other.x,self.y-other.y
            distance = sqrt(dx**2 + dy**2)
            if distance<self.radius+other.radius and self.radius>other.radius:
                other.alive = False
                self.radius = self.radius + int(other.radius*0.146)
    #绘图
    def draw(self,screen):
        pygame.draw.circle(screen,self.color,(self.x,self.y),self.radius,0)


def main():
    # 定义装球的容器
    Balls = []
    #初始化导入 pygame模块
    pygame.init()
    #初始化窗口大小
    screen = pygame.display.set_mode((1024,768))

    #窗口标题
    pygame.display.set_caption('大球吃小球')
    running = True
    #开启事件循环
    while running:
        #从消息队列中 获取消息并处理
        for e in pygame.event.get():
            if e.type ==pygame.QUIT:
                running = False
            if e.type==pygame.MOUSEBUTTONDOWN and e.button==1:
                # 获取鼠标点击的位置
                x,y = e.pos
                radius = randint(0,100) #随机大小
                sx,sy = randint(-10,10),randint(-10,10) #随机移动位置
                color = Color.randintColor()            #随机颜色
                ball = Ball(x,y,sx,sy,radius,color)
                #将球添加到容器
                Balls.append(ball)
        screen.fill((255,255,255))
        #取出容器的球
        for b in Balls:
            if b.alive:
                b.draw(screen)
            else:
                Balls.remove(b)
        pygame.display.flip()
        pygame.time.delay(30) #每隔50毫秒刷新窗口的位置
        for b in Balls:
            b.move(screen)
            #检测有没有吃到其他球
            for b1 in Balls:
                b.eat(b1)


if __name__ == '__main__':
   main()

结尾:本文的pygame的示例就到这里,感兴趣的同学可以再深度学习一下其他pygame模块进行一些小游戏开发

猜你喜欢

转载自blog.csdn.net/msdengxw/article/details/82012389