我是靠谱客的博主 老迟到西牛,最近开发中收集的这篇文章主要介绍Pygame开发"Flappy bird"小游戏"Emojin Bird"Pygame开发"Flappy bird"小戏"Emojin Bird",觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Pygame开发"Flappy bird"小戏"Emojin Bird"
微信关注公众号:夜寒信息
致力于为每一位用户免费提供更优质技术帮助与资源供给,感谢支持!
前几天学习了Pygame,想试着用以用,于是写了这个小游戏。优化比较差,游戏精灵的碰撞检测没学好,不会利用,导致代码冗余,感兴趣的同学可以拿来代码自己优化一下,很抱歉发了半成品,若有学习交流意向可以与我一起研究。
以下是一些图片:
以下是源码:
#上长的上边缘为211像素,下边缘为332像素。
#下长的上边缘为68像素,下边缘为189像素。
#中长的上边缘为156像素,下边缘为279像素。
#管间距300
#管上下间距为121
import pygame, sys, random, time
from pygame.locals import *
class Pipe(pygame.sprite.Sprite):
def __init__(self, bg, bar, bar2, pipe_sg, pipe_xg):
super().__init__()
self.bg = pygame.image.load(bg).convert()
self.bar = pygame.image.load(bar).convert()
self.bar2 = pygame.image.load(bar2).convert()
self.sg = pygame.image.load(pipe_sg).convert()
self.xg = pygame.image.load(pipe_xg).convert()
self.rect_bg = self.bg.get_rect()
self.rect_sg1 = self.sg.get_rect()
self.rect_sg2 = self.sg.get_rect()
self.rect_sg3 = self.sg.get_rect()
self.rect_sg4 = self.sg.get_rect()
self.rect_xg1 = self.xg.get_rect()
self.rect_xg2 = self.xg.get_rect()
self.rect_xg3 = self.xg.get_rect()
self.rect_xg4 = self.xg.get_rect()
self.rect_bar = self.bar.get_rect()
self.rect_bar2 = self.bar2.get_rect()
class Bird(pygame.sprite.Sprite):
def __init__(self, bird_fly, bird_jmp, bird_die):
super().__init__()
self.fly = pygame.image.load(bird_fly).convert_alpha()
self.jmp = pygame.image.load(bird_jmp).convert_alpha()
self.die = pygame.image.load(bird_die).convert_alpha()
self.rect_fly = self.fly.get_rect()
self.rect_jmp = self.jmp.get_rect()
self.rect_die = self.die.get_rect()
def check(b, p, pipe_key):
if pipe_key == 2:
if p.rect_sg1.right > b.rect_fly.left and b.rect_fly.top <= p.rect_sg1.bottom and b.rect_fly.right >= p.rect_sg1.left:
return True
elif b.rect_fly.right >= p.rect_xg1.left and b.rect_fly.bottom >= p.rect_xg1.top and b.rect_fly.left < p.rect_xg1.right:
return True
elif p.rect_sg1.right > b.rect_jmp.left and b.rect_jmp.top <= p.rect_sg1.bottom and b.rect_jmp.right >= p.rect_sg1.left:
return True
elif b.rect_jmp.right >= p.rect_xg1.left and b.rect_jmp.bottom >= p.rect_xg1.top and b.rect_jmp.left < p.rect_xg1.right:
return True
elif pipe_key == 3:
if p.rect_sg2.right > b.rect_fly.left and b.rect_fly.top <= p.rect_sg2.bottom and b.rect_fly.right >= p.rect_sg2.left:
return True
elif b.rect_fly.right >= p.rect_xg2.left and b.rect_fly.bottom >= p.rect_xg2.top and b.rect_fly.left < p.rect_xg2.right:
return True
elif p.rect_sg2.right > b.rect_jmp.left and b.rect_jmp.top <= p.rect_sg2.bottom and b.rect_jmp.right >= p.rect_sg2.left:
return True
elif b.rect_jmp.right >= p.rect_xg2.left and b.rect_jmp.bottom >= p.rect_xg2.top and b.rect_jmp.left < p.rect_xg2.right:
return True
elif pipe_key == 4:
if p.rect_sg3.right > b.rect_fly.left and b.rect_fly.top <= p.rect_sg3.bottom and b.rect_fly.right >= p.rect_sg3.left:
return True
elif b.rect_fly.right >= p.rect_xg3.left and b.rect_fly.bottom >= p.rect_xg3.top and b.rect_fly.left < p.rect_xg3.right:
return True
elif p.rect_sg3.right > b.rect_jmp.left and b.rect_jmp.top <= p.rect_sg3.bottom and b.rect_jmp.right >= p.rect_sg3.left:
return True
elif b.rect_jmp.right >= p.rect_xg3.left and b.rect_jmp.bottom >= p.rect_xg3.top and b.rect_jmp.left < p.rect_xg3.right:
return True
elif pipe_key == 1:
if p.rect_sg4.right > b.rect_fly.left and b.rect_fly.top <= p.rect_sg4.bottom and b.rect_fly.right >= p.rect_sg4.left:
return True
elif b.rect_fly.right >= p.rect_xg4.left and b.rect_fly.bottom >= p.rect_xg4.top and b.rect_fly.left < p.rect_xg4.right:
return True
elif p.rect_sg4.right > b.rect_jmp.left and b.rect_jmp.top <= p.rect_sg1.bottom and b.rect_jmp.right >= p.rect_sg4.left:
return True
elif b.rect_jmp.right >= p.rect_xg4.left and b.rect_jmp.bottom >= p.rect_xg4.top and b.rect_jmp.left < p.rect_xg4.right:
return True
else:
return False
def main():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 380))
pygame.display.set_caption("Emojin Bird")
bg = "flappy bird/背景.jpg"
bar = "flappy bird/底部.jpg"
bar2 = "flappy bird/底部2.jpg"
pipe_sg = "flappy bird/上管.jpg"
pipe_xg = "flappy bird/下管.jpg"
bird_fly = "flappy bird/飞.png"
bird_jmp = "flappy bird/跳.png"
bird_die = "flappy bird/死.png"
bg_music = "flappy bird/bg_music.ogg"
die_wav = "flappy bird/die_music.wav"
pygame.mixer.music.load(bg_music)
die_sound = pygame.mixer.Sound(die_wav)
pygame.mixer.music.play()
p = Pipe(bg, bar, bar2, pipe_sg, pipe_xg)
b = Bird(bird_fly, bird_jmp, bird_die)
b.rect_fly.bottom = 200
p.rect_sg1.left = 500
p.rect_xg1.left = 500
b.rect_fly.left = 150
p.rect_bar.bottom = 380
p.rect_bar2.bottom = 380
p.rect_sg2.left = -100
p.rect_xg2.left = -100
p.rect_sg3.left = -100
p.rect_xg3.left = -100
p.rect_sg4.left = -100
p.rect_xg4.left = -100
jmp_key = 0 #跳跃标志
die_key = 0 #死亡标志
pipe_key = 0 #主子标记
vol_allow = 1 #撞击标志
bg_speed = [-4, 0] #背景速度
jmp_speed = [0, -1] #跳跃速度
fly_speed = [0, 1] #下坠速度
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
sys.exit()
if event.key == K_SPACE:
if not die_key:
jmp_key = 1
fly_speed[1] = 1
#else:
# jmp_key = 0
# fly_speed[1] = 1
screen.fill((255, 255, 255))
screen.blit(p.bg,(0,0))
if p.rect_sg1.left == 500:
p.rect_sg1.bottom = random.randrange(60, 220, 5)
p.rect_xg1.top = p.rect_sg1.bottom + 100
pipe_key = 1
elif p.rect_sg1.left == 300:
p.rect_sg2.left = 500
p.rect_xg2.left = 500
p.rect_sg2.bottom = random.randrange(60, 220, 5)
p.rect_xg2.top = p.rect_sg2.bottom + 100
pipe_key = 2
elif p.rect_sg1.left == 100:
p.rect_sg3.left = 500
p.rect_xg3.left = 500
p.rect_sg3.bottom = random.randrange(60, 220, 5)
p.rect_xg3.top = p.rect_sg3.bottom + 100
pipe_key = 3
elif p.rect_sg1.left == -100:
p.rect_sg4.left = 500
p.rect_xg4.left = 500
p.rect_sg4.bottom = random.randrange(60, 220, 5)
p.rect_xg4.top = p.rect_sg4.bottom + 100
p.rect_sg1.left = 700
p.rect_xg1.left = 700
pipe_key = 4
screen.blit(p.sg, p.rect_sg1)
screen.blit(p.xg, p.rect_xg1)
screen.blit(p.sg, p.rect_sg2)
screen.blit(p.xg, p.rect_xg2)
screen.blit(p.sg, p.rect_sg3)
screen.blit(p.xg, p.rect_xg3)
screen.blit(p.sg, p.rect_sg4)
screen.blit(p.xg, p.rect_xg4)
if check(b, p, pipe_key):
bg_speed = [0, 0]
jmp_key = 0
screen.blit(b.die, b.rect_fly)
die_key = 1
if b.rect_fly.bottom >= 352:
bg_speed = [0, 0]
jmp_key = 0
# fly_speed = [0, 0]
screen.blit(b.die, b.rect_fly)
# b.rect_fly.bottom = 364
die_key = 1
if jmp_key and jmp_speed[1] >= -12:
jmp_speed[1] -= 2
screen.blit(b.jmp, b.rect_fly)
b.rect_fly = b.rect_fly.move(jmp_speed)
else:
jmp_key = 0
jmp_speed = [0, -1]
if die_key:
if b.rect_fly.bottom <= 351:
fly_speed[1] += 1
b.rect_fly = b.rect_fly.move(fly_speed)
else:
fly_speed = [0, 0]
b.rect_fly.bottom = 365
else:
# jmp_key = 0
fly_speed[1] += 1
screen.blit(b.fly, b.rect_fly)
b.rect_fly = b.rect_fly.move(fly_speed)
'''if die_key:
screen.blit(b.fly, b.rect_fly)
jmp_key = 0
jmp_speed[1] = -1
fly_speed[1] += 1
if b.rect_fly.bottom >= 352:
fly_speed = [0, 0]
b.rect_fly.bottom = 364
die_key = 0
else:
bg_speed = [0, 0]
screen.blit(b.die, b.rect_fly)'''
# b.rect_fly = b.rect_fly.move(fly_speed)
if p.rect_bar.left <= 0 and p.rect_bar.right > 0:
p.rect_bar2.left = p.rect_bar.right
elif p.rect_bar.right <= 0 and p.rect_bar.left < 0:
p.rect_bar.left = p.rect_bar2.right
if die_key and vol_allow:
pygame.mixer.music.stop()
die_sound.play()
vol_allow = 0
#pygame.time.delay(100)
#die_sound.set_volume(0)
screen.blit(p.bar, p.rect_bar)
screen.blit(p.bar2, p.rect_bar2)
p.rect_bar = p.rect_bar.move(bg_speed)
p.rect_bar2 = p.rect_bar2.move(bg_speed)
p.rect_sg1 = p.rect_sg1.move(bg_speed)
p.rect_xg1 = p.rect_xg1.move(bg_speed)
p.rect_sg2 = p.rect_sg2.move(bg_speed)
p.rect_xg2 = p.rect_xg2.move(bg_speed)
p.rect_sg3 = p.rect_sg3.move(bg_speed)
p.rect_xg3 = p.rect_xg3.move(bg_speed)
p.rect_sg4 = p.rect_sg4.move(bg_speed)
p.rect_xg4 = p.rect_xg4.move(bg_speed)
clock.tick(30)
pygame.display.update()
if __name__ == "__main__":
main()
这是素材文件:
https://www.lanzous.com/iabevof
微信关注公众号:夜寒信息
致力于为每一位用户免费提供更优质技术帮助与资源供给,感谢支持!
最后
以上就是老迟到西牛为你收集整理的Pygame开发"Flappy bird"小游戏"Emojin Bird"Pygame开发"Flappy bird"小戏"Emojin Bird"的全部内容,希望文章能够帮你解决Pygame开发"Flappy bird"小游戏"Emojin Bird"Pygame开发"Flappy bird"小戏"Emojin Bird"所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复