pygame实现动态小鸟飞行 + 打包发布

学了两天的pygame,就用这个简单的程序纪念一下成果吧。

本想着做个更像游戏的东西,奈何发现用pygame做起来并不容易,所以就不打算再花费精力玩儿这个了。想做游戏还是老老实实找个游戏引擎

其实还算是有点收获的,起码对一些基本制作的流程(皮毛)有了点认识。


正题:

项目结构思维导图:

main.py

import pygame, sys, time
from pygame.locals import *
from setting import Settings 
from ball import Ball
import game_functions as gf

def __rungame__():
	pygame.init()
	gameIcon = pygame.image.load('images/bird-2.png')
	pygame.display.set_icon(gameIcon)
	pygame.display.set_caption("Fly Brid")
	#程序初始化以及设置标题和图标

	setting = Settings()
	FPSCLOCK = pygame.time.Clock()
	#设置setting类和FPSCLOCK

	SCREEN = pygame.display.set_mode(
		(setting.scr_width, setting.scr_height))
	
	bg = pygame.Surface( (setting.bg_width, setting.bg_height) )
	bg.fill(setting.bg_color)
	#初始化SCREEN主屏幕,初始化bg地面

	bird = Ball(setting, SCREEN)
	#定义bird

	while True:
		bird.updateIndex()
		gf.check_events(SCREEN, bird)
		gf.update_all(SCREEN, bg, setting, bird)
		FPSCLOCK.tick(60)
		#帧率为60

__rungame__()

setting.py

class Settings():

	def __init__(self):
		self.scr_width = 800
		self.scr_height = 600
		self.scr_color = (255,255,255)

		self.bg_width = self.scr_width
		self.bg_height = 150
		self.bg_color = (130, 130, 130)
		self.bg_position = (0,
			self.scr_height - self.bg_height)

		self.bird_lowest = self.scr_height - self.bg_height - 2
		self.bird_speed_factor = 3

ball.py

import pygame

class Ball():
	
	def __init__(self, birdSetting, screen):
		self.screen = screen
		self.birdSetting = birdSetting
		self.image = [
			pygame.image.load('images/bird-1.png'),
			pygame.image.load('images/bird-2.png'),
			pygame.image.load('images/bird-3.png'),
		]

		self.index = 0
		self.cnt = 0

		self.rect = self.image[0].get_rect()
		self. screen_rect = screen.get_rect()

		self.rect.centerx = self.screen_rect.centerx
		self.rect.bottom = self.birdSetting.bird_lowest

		self.moving_right = False
		self.moving_left  = False
		self.moving_up = False
		self.moving_down  = False
	
		self.level = float(self.rect.centerx)
		self.vertical = float(self.rect.bottom)

	def updatePosition(self):
		if self.moving_right and self.rect.right < self.screen_rect.right:
			self.level += self.birdSetting.bird_speed_factor
		if self.moving_left and self.rect.left > 0:
			self.level -= self.birdSetting.bird_speed_factor
		if self.moving_up and self.rect.top > self.screen_rect.top:
			self.vertical -= self.birdSetting.bird_speed_factor
		if self.moving_down and self.rect.bottom < self.birdSetting.bird_lowest:
			self.vertical += self.birdSetting.bird_speed_factor

		self.rect.centerx = self.level
		self.rect.bottom = self.vertical
		#print(self.vertical)

	def updateIndex(self):
		self.cnt += 1
		if self.cnt % 7 == 0:
			self.cnt = 0
			self.index += 1
			self.index %= 3

game_function.py

import sys, pygame
from ball import Ball 

def check_keydown_events(event, bird):
    if event.key == pygame.K_RIGHT:
        bird.moving_right = True

    elif event.key == pygame.K_LEFT:
        bird.moving_left = True

    elif event.key == pygame.K_UP:
        bird.moving_up = True

    elif event.key == pygame.K_DOWN:
        bird.moving_down = True

def check_keyup_events(event, bird):
    if event.key == pygame.K_RIGHT:
        bird.moving_right = False

    elif event.key == pygame.K_LEFT:
        bird.moving_left = False

    elif event.key == pygame.K_UP:
        bird.moving_up = False

    elif event.key == pygame.K_DOWN:
        bird.moving_down = False

def check_events(screen, bird):

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, bird)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event, bird)

def update_all(SCREEN, bg, setting, bird):
	bird.updatePosition()

	SCREEN.fill(setting.scr_color)
	SCREEN.blit(bg, setting.bg_position)
	SCREEN.blit(bird.image[bird.index], bird.rect)

	pygame.display.flip()

这些代码总共也就150多行,个人认为略有价值的一部分是关于动画的实现

这是三张小鸟图片的源文件                                         

--图片来自网络,侵删

动画实现方法:自己用脑洞想了一个实现方法,把这三张图片存入一个列表中,通过计数器判定显示哪一张图片,计数器三个数一循环,利用pygame.time.clock().tick(60)控制帧数在60左右,修改ball.pyupdateIndex函数即可改变小鸟图片更新的速度


打包发布:

很简单下载个pyinstaller就行,前提是装了pip,装法就不赘述了。

步骤:

1. 打开cmd,输入pip install pyinstaller

2. 进入项目的文件夹路径下,比如我的是D:\__PYTHON__\Fly Bird

3. 输入pyinstaller -F -w main.py -p setting.py -p ball.py -p game_function.py 

     -F -w 程序入口文件, -p 程序文件

4. 此时项目文件夹下会多一个 dist 文件夹和 main.spec,把 image 文件夹复制到 dist 文件夹下即可打包完成

THE END

猜你喜欢

转载自blog.csdn.net/qq_40526226/article/details/86585501