猫史档案馆


【Python作品分享】小鸟【作业帖】

用户:EFSFEFSF查看:7 回复:2 评论:7 创建时间:2021-09-30T20:32:43


【作品展示】

center_image

 

【作品介绍】

飞行的小鸟,鼠标点击飞行,空格重生

 

【作品源代码】

import pygame
import random
import sys

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

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

bgm = pygame.mixer.Sound("bgm2.wav")
channel_1 = pygame.mixer.Channel(1)
channel_1.play(bgm)

score = 0

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


class Bird(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.birdSprites = [pygame.image.load(
			"0.png"), pygame.image.load("1.png"), pygame.image.load("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 birdUpdata(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
		if newBird.rect.top > ground.rect.top:
			self.rect.centery = ground.rect.top
		else:
			global score
			if self.rect.left == newWall.wallUpRect.right:
				score = score+1

	def birdCrush(self):
		global keep_going
		resultU = self.rect.colliderect(newWall.wallUpRect)
		resultD = self.rect.colliderect(newWall.wallDownRect)

		if resultU or resultD or newBird.rect.bottom >= ground.rect.top:
			hit = pygame.mixer.Sound("hit.wav")
			channel_3 = pygame.mixer.Channel(3)
			channel_3.play(hit)
			keep_going = False


class Wall():
	def __init__(self):
		self.wallUp = pygame.image.load("bottom.png")
		self.wallDown = pygame.image.load("top.png")
		self.wallUpRect = self.wallUp.get_rect()
		self.wallDownRect = self.wallDown.get_rect()
		self.gap = 65
		self.wallx = 288
		self.offset = random.randint(-50, 50)
		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 wallUpdata(self):
		self.wallx -= 6
		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, 55)
			self.wallUpY = 360+self.gap-self.offset
			self.wallDownY = 0-self.gap - self.offset


class Text():
	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 updataText(self, content):
		contentStr = str(content)
		self.image = self.font.render(contentStr, True, self.color)


class Ground():
	def __init__(self):
		self.image = pygame.image.load("ground.png")
		self.rect = self.image.get_rect()
		self.rect.bottom = 560
		self.rect.left = -30


newBird = Bird()
newWall = Wall()
coolText = Text(score)
keep_going = True
clock = pygame.time.Clock()

endText = Text("END")
ground = Ground()
highest_score = 0
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
		if keep_going:
			if (event.type == pygame.MOUSEBUTTONDOWN):
				newBird.jumpSpeed = 7

				channel_2 = pygame.mixer.Channel(2)
				fly = pygame.mixer.Sound('fly.WAV')
				channel_2.play(fly)
		else:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_SPACE:
					keep_going = True
					score = 0
					newBird.birdX = 50
					newBird.birdY = 100
					newWall.wallx = 288
					newBird.jumpSpeed = 7

	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))
	screen.blit(ground.image, ground.rect)

	newWall.wallUpdata()
	newBird.birdUpdata()
	if keep_going:
		newBird.birdCrush()
		coolText.updataText(score)
	else:
		screen.blit(endText.image, (110, 230))
		if score > highest_score:
			highest_score = score
		highest_score_text = Text('best play:'+str(highest_score))
		screen.blit(highest_score_text.image, (50, 270))
	pygame.display.update()
	clock.tick(60)
pygame.quit

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
大黄鸡hhh大黄鸡hhh

imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E5%8A%A0%E6%B2%B9.gif"alt="emotion_编程猫_加油"

点赞0


评论


Python-happyyyyPython-happyyyy

a z

点赞0


评论