import cfg
import sys
import random
import pygame
from modules.sprites.Pipe import *
from modules.sprites.Bird import *
from modules.interfaces.endGame import *
from modules.interfaces.startGame import *
'''游戏初始化'''
def initGame():
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((cfg.SCREENWIDTH, cfg.SCREENHEIGHT))
pygame.display.set_caption('Flappy Bird')
return screen
'''显示当前分数'''
def showScore(screen, score, number_images):
digits = list(str(int(score)))
width = 0
for d in digits:
width += number_images.get(d).get_width()
offset = (cfg.SCREENWIDTH - width) / 2
for d in digits:
screen.blit(number_images.get(d), (offset, cfg.SCREENHEIGHT*0.1))
offset += number_images.get(d).get_width()
'''主函数'''
def main():
screen = initGame()
# 加载必要的游戏资源
# --导入音频
sounds = dict()
for key, value in cfg.AUDIO_PATHS.items():
sounds[key] = pygame.mixer.Sound(value)
# --导入数字图片
number_images = dict()
for key, value in cfg.NUMBER_IMAGE_PATHS.items():
number_images[key] = pygame.image.load(value).convert_alpha()
# --管道
pipe_images = dict()
pipe_images['bottom'] = pygame.image.load(random.choice(list(cfg.PIPE_IMAGE_PATHS.values()))).convert_alpha()
pipe_images['top'] = pygame.transform.rotate(pipe_images['bottom'], 180)
# --小鸟图片
bird_images = dict()
for key, value in cfg.BIRD_IMAGE_PATHS[random.choice(list(cfg.BIRD_IMAGE_PATHS.keys()))].items():
bird_images[key] = pygame.image.load(value).convert_alpha()
# --背景图片
backgroud_image = pygame.image.load(random.choice(list(cfg.BACKGROUND_IMAGE_PATHS.values()))).convert_alpha()
# --其他图片
other_images = dict()
for key, value in cfg.OTHER_IMAGE_PATHS.items():
other_images[key] = pygame.image.load(value).convert_alpha()
# 游戏开始界面
game_start_info = startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg)
# 进入主游戏
score = 0
bird_pos, base_pos, bird_idx = list(game_start_info.values())
base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()
clock = pygame.time.Clock()
# --管道类
pipe_sprites = pygame.sprite.Group()
for i in range(2):
pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('top')[-1])))
pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('bottom')[-1])))
# --bird类
bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)
# --是否增加pipe
is_add_pipe = True
# --游戏是否进行中
is_game_running = True
while is_game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
bird.setFlapped()
sounds['wing'].play()
# --碰撞检测
for pipe in pipe_sprites:
if pygame.sprite.collide_mask(bird, pipe):
sounds['hit'].play()
is_game_running = False
# --更新小鸟
boundary_values = [0, base_pos[-1]]
is_dead = bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.)
if is_dead:
sounds['hit'].play()
is_game_running = False
# --移动base实现小鸟往前飞的效果
base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
# --移动pipe实现小鸟往前飞的效果
flag = False
for pipe in pipe_sprites:
pipe.rect.left -= 4
if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:
pipe.used_for_score = True
score += 0.5
if '.5' in str(score):
sounds['point'].play()
if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:
pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=pipe_pos.get('top')))
pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=pipe_pos.get('bottom')))
is_add_pipe = False
elif pipe.rect.right < 0:
pipe_sprites.remove(pipe)
flag = True
if flag: is_add_pipe = True
# --绑定必要的元素在屏幕上
screen.blit(backgroud_image, (0, 0))
pipe_sprites.draw(screen)
screen.blit(other_images['base'], base_pos)
showScore(screen, score, number_images)
bird.draw(screen)
pygame.display.update()
clock.tick(cfg.FPS)
endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg)
'''run'''
if __name__ == '__main__':
while True:
main()
没有合适的资源?快使用搜索试试~ 我知道了~
手写小游戏python游戏源码-02 飞鸟 FlappyBird
共106个文件
png:52个
pyc:20个
ogg:10个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 76 浏览量
2022-11-04
14:22:38
上传
评论 1
收藏 28.99MB ZIP 举报
温馨提示
手写小游戏python游戏源码-02 飞鸟 FlappyBird.py 可交作业,可学习资料
资源推荐
资源详情
资源评论
收起资源包目录
手写小游戏python游戏源码-02 飞鸟 FlappyBird (106个子文件)
running.gif 14.15MB
running(1).gif 14.15MB
flappy(1).ico 97KB
flappy.ico 97KB
die(1).ogg 17KB
die.ogg 17KB
hit.ogg 15KB
hit(1).ogg 15KB
swoosh.ogg 13KB
swoosh(1).ogg 13KB
point.ogg 13KB
point(1).ogg 13KB
wing.ogg 8KB
wing(1).ogg 8KB
background-day.png 7KB
background-day(1).png 7KB
pipe-green(1).png 2KB
pipe-green.png 2KB
pipe-red(1).png 2KB
pipe-red.png 2KB
message(1).png 2KB
message.png 2KB
background-night.png 1KB
background-night(1).png 1KB
gameover(1).png 737B
gameover.png 737B
base.png 449B
base(1).png 449B
bluebird-downflap.png 410B
bluebird-upflap(1).png 410B
bluebird-downflap(1).png 410B
bluebird-upflap.png 410B
bluebird-midflap.png 409B
bluebird-midflap(1).png 409B
yellowbird-upflap.png 406B
yellowbird-upflap(1).png 406B
redbird-midflap(1).png 405B
yellowbird-downflap.png 405B
redbird-midflap.png 405B
yellowbird-downflap(1).png 405B
redbird-downflap(1).png 404B
redbird-upflap(1).png 404B
redbird-upflap.png 404B
redbird-downflap.png 404B
yellowbird-midflap.png 404B
yellowbird-midflap(1).png 404B
4(1).png 325B
4.png 325B
7.png 324B
5(1).png 324B
5.png 324B
2.png 324B
2(1).png 324B
7(1).png 324B
6.png 323B
6(1).png 323B
9(1).png 322B
9.png 322B
3.png 318B
3(1).png 318B
8.png 317B
8(1).png 317B
1(1).png 315B
1.png 315B
0(1).png 315B
0.png 315B
FlappyBird.py 5KB
cfg.py 3KB
Bird.py 2KB
Bird(1).py 2KB
startGame(1).py 2KB
startGame.py 2KB
endGame.py 944B
endGame(1).py 944B
Pipe.py 723B
Pipe(1).py 723B
cfg.cpython-37(1).pyc 2KB
cfg.cpython-37.pyc 2KB
cfg.cpython-36.pyc 2KB
cfg.cpython-36(1).pyc 2KB
Bird.cpython-37(1).pyc 2KB
Bird.cpython-37.pyc 2KB
Bird.cpython-36.pyc 2KB
Bird.cpython-36(1).pyc 2KB
startGame.cpython-37(1).pyc 2KB
startGame.cpython-37.pyc 2KB
startGame.cpython-36.pyc 2KB
startGame.cpython-36(1).pyc 2KB
Pipe.cpython-37(1).pyc 1KB
Pipe.cpython-37.pyc 1KB
Pipe.cpython-36.pyc 1KB
Pipe.cpython-36(1).pyc 1KB
endGame.cpython-37(1).pyc 1KB
endGame.cpython-37.pyc 1KB
endGame.cpython-36(1).pyc 1KB
endGame.cpython-36.pyc 1KB
swoosh.wav 346KB
swoosh(1).wav 346KB
die(1).wav 190KB
die.wav 190KB
共 106 条
- 1
- 2
资源评论
- m0_710612752023-06-25资源很好用,有较大的参考价值,资源不错,支持一下。
小风飞子
- 粉丝: 321
- 资源: 1496
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功