猫史档案馆


【Python作品分享】撞柱鸟FlappyBird【作品秀】

用户:小黄·豆豆小黄·豆豆查看:0 回复:0 评论:0 创建时间:2021-01-31T20:47:24


【作品展示】

center_image

 

【作品介绍】

超级撞柱鸟

 

【作品源代码】

import pygame
import random

pygame.init()
screen = pygame.display.set_mode([288,512])

background = pygame.image.load("assets/background.png")
pygame.display.set_caption("flappy Bird")

bgm=pygame.mixer.Sound('assets/bgm.wav')
Channel_1=pygame.mixer.Channel(1)
Channel_1.play(bgm)

keep_going = True
clock=pygame.time.Clock()

score=0

class Bird(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.birdsprites = [pygame.image.load('assets/0.png'),pygame.image.load('assets/1.png'),pygame.image.load('assets/2.png')]
		self.a=0
		self.birdx=50
		self.birdy=100
		self.jumpSpeed=7
		self.gravity=0.4

		self.rect=self.birdsprites[self.a].get_rect()
		self.rect.center=(self.birdx,self.birdy)

	def birdUpdate(self):
		self.jumpSpeed-=self.gravity
		self.birdy-=self.jumpSpeed
		self.rect.center=(self.birdx,self.birdy)
		
		if self.jumpSpeed<0:
			self.a=1
		if self.jumpSpeed>0:
			self.a=2
		
		global score
		if self.rect.left==newWall.wallUpRect.right:
			score+=1 
		print(self.rect.left,newWall.wallUpRect.right+1)
		print(score)
			
	def birdcrush(self):
		global keep_going
		resultU=self.rect.colliderect(newWall.wallUpRect)
		resultD=self.rect.colliderect(newWall.wallDownRect)
		
		if resultU or resultD:
			keep_going=False
		
class wall():
	def __init__(self):
		self.wallup=pygame.image.load("assets/bottom.png")
		self.walldown=pygame.image.load("assets/top.png")
		self.wallUpRect=self.wallup.get_rect()
		self.wallDownRect=self.walldown.get_rect()
		
		self.gap=75
		self.wallx=288
		self.offset=random.randint(-50,50)
		self.wallDownRect=self.walldown.get_rect()
	
		self.wallUpY=360+self.gap-self.offset
		self.wallDownY=0-self.gap-self.offset

		self.wallUpRect.center=(self.wallx,self.wallUpY)
		self.wallDownRect.center=(self.wallx,self.wallDownY)
	def wallUpdate(self):
		
		self.wallx -= 3 #柱子移动速度
		self.wallUpRect.center=(self.wallx,self.wallUpY)
		self.wallDownRect.center=(self.wallx,self.wallDownY)

		if self.wallx <-80:
			self.wallx = 288
			self.offset=random.randint(-50,50)

			self.wallUpY=360+self.gap-self.offset
			self.wallDownY=0-self.gap-self.offset

class Text():
	"""docstring for showText"""
	def __init__(self,content):
		red=(100,50,50)
		self.color=red
		self.font=pygame.font.SysFont(None,52)
		contentStr=str(content)
		self.image=self.font.render(contentStr,True,self.color)
	def updateText(self,content):
		contentStr=str(content)
		self.image=self.font.render(contentStr,True,self.color)

newBird=Bird()
newWall=wall()

coolText=Text(score)
keep_going=True
clock=pygame.time.Clock()
endText=Text('END')
while keep_going:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			keep_going = False
		if (event.type == pygame.MOUSEBUTTONDOWN):
			newBird.jumpSpeed=7
	
			channel_2=pygame.mixer.Channel(2)
			fly=pygame.mixer.Sound('assets/fly.wav')
			channel_2.play(fly)   

	screen.blit(background,(0,0))
	screen.blit(newBird.birdsprites[newBird.a],(newBird.rect))
	screen.blit(newWall.wallup,newWall.wallUpRect)
	screen.blit(newWall.walldown,newWall.wallDownRect)
	screen.blit(coolText.image,(10,10))
	
	newWall.wallUpdate()
	newBird.birdUpdate()     
	
	coolText.updateText(score)
	
	newBird.birdcrush()
	pygame.display.update()
	
	clock.tick(60)

pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页