猫史档案馆


【pygame:薯架】教你用pygame开发游戏 Season2 Part5

用户:天上的流星_tianqiVirus天上的流星_tianqiVirus查看:0 回复:0 评论:0 创建时间:2019-07-20T21:54:26


教你用pygame开发游戏

今日游戏:薯架(自创)

如果对几行代码没有看懂,那么你可能是没看过上一次的结果

那就去看看我之前的帖子

 

上次我们到哪儿了

哦对,还差些结束的画面

先介绍一下,我们的思路是:

失败了后画面停止运动

播放失败声音

显示失败提示

增添选择重新开始的方式

就这些

我们先看看播放声音

这里要用pygame里的mixer

在之前还要定义一个频道

就像电视一样

不过在这之前,我们得先把音乐弄下来

众所周知,这个只是帖子,放不了声音。我能给你们的是提示,那就是选声音

这是声音导入的方法

music = pygame.mixer.Sound(path)

这是频道定义的方法

channel = pygame.mixer.Channel(int)

中间这个int你就按照顺序填1234……,因为这就是频道

这个是播放频道的方法

channel.play(music)

真熙皞

然后我们先导入我们的音乐

bgm = pygame.mixer.Sound("assets/sound/bgm.wav")

路径按照代码里的来

然后是频道

然后播放

代码我不列出了,你们自己学

还有,大家可能会问,吃到薯条和跳跃之类的怎么没有声音

是因为我选的backGroundMusic(bgm)音乐比较强烈,再加上游戏画面,信息量太大了

还有就是某些游戏上瘾的人会一直玩下去,所以我们要一直放音乐

我们先判断是否在播放,如果是,就再播放音乐

这里判断的方法如下

pygame.mixer.music.get_busy()

其返回值为布尔值,所以我们可以把它放在if

然后如果为否,播放音乐

懂了吗

(懂了什么呀)

然后我们设置一个结束画面

与飞机大战不同,这次我们把它放在主循环里面

定义一个endGoing,不过这次是False,然后用while弄他

还记得猫的类吗

	def catUpdate(self):
		global keepGoing
		self.rect = pygame.Rect(self.x,self.y+2,self.size[0],self.size[1]-2)
		self.jump+=self.downSpeed
		self.y+=self.jump
		if self.y>=650:
			keepGoing=False

改成endGoing=Trueglobal进来的也是endGoing

因为结束画面在循环内

如果它为真,就进入了结束画面

哦对了,结束画面的循环是这样的

	while endGoing:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				exit()

此外,我还要说一句老话,不懂这句话是什么的请看我以前的帖子

(又打广告)

然后,为了播放我们的结束声音,我们先导入

随便弄,这个音乐没要求

还有一点大家要注意,一个频道一次只能播放一个音频,所以说只要我们播放结束音乐,bgm就会停止

真熙皞

导入音频,不妨叫他lose,然后看到mainCat里的catUpdate

		if self.y>=650:
			endGoing=True
			channel.play(lose)

加上第三行代码

至于为什么只在这里添加,原因是这样的:

我们只让它播放一次,这一次就在猫喵掉的瞬间

猫喵掉后会进入结束界面,从而不会运行更新函数

 

好了,这是音乐,然后弄结束图片

center_image

话说开始的游戏介绍玩法都用英文,而这个用中文,风格有点不同

为了演示和掩饰,先这样吧

把它弄成pygameSurface,路径看函数括号里的字符

lose_i = pygame.image.load("assets/image/end/lose.png")

然后给它放到中心点

		screen.blit(lose_i,(244,262))

然后告诉玩家,按下任意按键可以重新开始

		final_text = font.render("Press any key to restart",True,(255,255,255))

为了满足某些英语不大好的人,我把文字列出来

接下来我介绍一个方法

		screen.blit(final_text,(400-final_text.get_rect().width/2,400))

看见里面的get_rect了吗,它能对于图像直接生成矩形

然后我们拿到了它的宽度,就能放到中心点了

话说,说到做到,你不能说让他拉开窗帘出去然后又把窗给关上

所以我们这么弄

			if event.type == pygame.KEYDOWN:

这个也属于侦测的事件,所以放在range循环里

看那个英文也好理解

接着,在里面,我们就弄初始化设定

				myCat.__init__()
				jam1.__init__()
				fried1.__init__()
				endGoing = False
				channel.play(bgm)
				score = 0
				power = 10

注意这个__init__,这个是默认一定义实例就开始运行的,但也是一个函数,也可以调用

现在呢?

真熙皞!

民物欣凫藻!

多棒啊

这样,我们这次的任务就完成了

最终代码如下

import pygame
from random import randint
from sys import exit

#color 				R 		G 		B
backgroundColor = [	100,	100,	155]

windowWidth = 800
windowHeight = 600
leftButton = 1
keepGoing = True
beginGoing = True
endGoing = False
score = 0
power = 10

pygame.init()
screen = pygame.display.set_mode([windowWidth,windowHeight])
pygame.display.set_caption("Potato Cat")
font = pygame.font.SysFont("Lucida Console",20)

score_i = pygame.transform.喵oothscale(pygame.image.load("assets/image/FrenchFried/1.png"),(40,40))
power_i = pygame.transform.喵oothscale(pygame.image.load("assets/image/tomatoJam/2.png"),(40,40))
title = pygame.image.load("assets/image/begin/title.png")
play = pygame.image.load("assets/image/begin/howToPly.png")
beginButton = pygame.image.load("assets/image/button/start.png")
guide = pygame.image.load("assets/image/begin/guide1.png")
lose_i = pygame.image.load("assets/image/end/lose.png")

bgm = pygame.mixer.Sound("assets/sound/bgm.wav")
lose = pygame.mixer.Sound("assets/sound/lose.wav")
channel = pygame.mixer.Channel(1)
channel.play(bgm)

class mainCat:
	def __init__(self):
		self.size=[32,32]
		self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/cat/1.png"),self.size)
		self.x = 100
		self.y = 50
		self.jump = 0
		self.downSpeed = 0.01
		self.jumpSpeed = 1.5
		self.rect = pygame.Rect(self.x,self.y+2,self.size[0],self.size[1]-2)

	def catUpdate(self):
		global endGoing
		self.rect = pygame.Rect(self.x,self.y+2,self.size[0],self.size[1]-2)
		self.jump+=self.downSpeed
		self.y+=self.jump
		if self.y>=650:
			endGoing=True
			channel.play(lose)

class FrenchFried:
	def __init__(self):
		self.size=[61,23]
		self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/FrenchFried/2.png"),self.size)
		self.x = -61
		self.y = 0
		self.speed = 0.8
		self.rect = pygame.Rect(self.x+3,self.y+10,self.size[0]-3,self.size[1]-10)

	def friedUpdate(self):
		global windowWidth,windowHeight,score,power
		self.rect = pygame.Rect(self.x+3,self.y+10,self.size[0]-3,self.size[1]-10)
		self.x-=self.speed
		if self.x<=-self.size[0]:
			self.x = windowWidth
			self.y = randint(0+100,windowHeight-100)
		if self.rect.colliderect(myCat.rect):
			score+=1
			power+=1
			self.x = windowWidth
			self.y = randint(0+100,windowHeight-100)

class tomatoJam:
	def __init__(self):
		self.size = [80,80]
		self.image = pygame.transform.喵oothscale(pygame.image.load("assets/image/tomatoJam/1.png"),self.size)
		self.x = -80
		self.y = 0
		self.speed = 0.5
		self.rect = pygame.Rect(self.x+24,self.y+38,self.size[0]-25,self.size[1]-20)

	def jamUpdate(self):
		global windowWidth,windowHeight,power
		self.rect = pygame.Rect(self.x+24,self.y+38,self.size[0]-25,self.size[1]-20)
		self.x-=self.speed
		if self.x<=-self.size[0]:
			self.x = windowWidth
			self.y = randint(0+100,windowHeight-100)
		if self.rect.colliderect(myCat.rect):
			power+=5
			self.x = windowWidth
			self.y = randint(0+100,windowHeight-100)

myCat = mainCat()
fried1 = FrenchFried()
jam1 = tomatoJam()

buttonRect = pygame.Rect(125+12,300+12,36,36)

while beginGoing:

	screen.fill(backgroundColor)
	x,y = pygame.mouse.get_pos()
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			exit()
		if event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == 1:
				if buttonRect.collidepoint(x,y):
					beginGoing = False

	screen.blit(title,(300,0))
	screen.blit(play,(300,200))
	screen.blit(beginButton,(125,300))
	screen.blit(guide,(80,350))
	screen.blit(myCat.image,(myCat.x,myCat.y))
	pygame.display.update()

while keepGoing:

	screen.fill(backgroundColor)

	score_text = font.render(str(score),True,(255,255,255))
	power_text = font.render(str(power),True,(255,255,255))

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			exit()
		if event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == leftButton:
				if power>0:
					myCat.jump=-myCat.jumpSpeed
					power-=1

	if pygame.mixer.music.get_busy():
		channel.play(bgm)
	
	myCat.catUpdate()
	fried1.friedUpdate()
	jam1.jamUpdate()

	pygame.draw.rect(screen,(75,75,125),(0,0,800,50))
	screen.blit(score_i,(0,5))
	screen.blit(power_i,(300,5))
	screen.blit(power_text,(350,20))
	screen.blit(score_text,(50,20))

	screen.blit(myCat.image,(myCat.x,myCat.y))
	screen.blit(fried1.image,(fried1.x,fried1.y))
	screen.blit(jam1.image,(jam1.x,jam1.y))

	pygame.display.update()

	while endGoing:
		final_text = font.render("Press any key to restart",True,(255,255,255))
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				exit()
			if event.type == pygame.KEYDOWN:
				myCat.__init__()
				jam1.__init__()
				fried1.__init__()
				endGoing = False
				channel.play(bgm)
				score = 0
				power = 10

		screen.blit(final_text,(400-final_text.get_rect().width/2,400))
		screen.blit(lose_i,(244,262))
		pygame.display.update()

pygame.quit()

这样,我们这一次的任务就完成了

想要继续学习pygame和游戏制作的请关注

written by : 天上的流星

本帖内的所有代码,思想仅供参考,请勿用于商业盈利用途

我是说,自己玩玩自己弄的也行

 

 


回复

上一页1 页 / 共 0下一页