猫史档案馆


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

用户:天上的流星_tianqiVirus天上的流星_tianqiVirus查看:0 回复:0 评论:0 创建时间:2019-07-13T07:37:25


教你用pygame开发游戏

今日游戏:薯架(自创)

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

那就去看看我之前的帖子

事先说明,我的代码编程水平没这个烂

 

这一次我们来玩点高级的

首先先查漏补缺,看看前三期游戏设计有没有到位

center_image

(伏笔?)

这个猫疯了,没能量了还飞

所以我们先改进

center_image

请加上if power>0:语句,同时将语句内的两行代码添加缩进

同时,我们知道,这个猫一旦掉出了屏幕就挂了,所以我们还要加上这个的检测

由于只有几行代码,我就不列出来了,记住,要放在mainCat类里面

下一个是关于吃的的

为了在一开始就随机选择y坐标显示,我们就一开始把它放在屏幕左侧

#薯条__init__()的内容
		self.x = -61
		self.y = 0
#番茄酱__init__()的内容
		self.x = -80
		self.y = 0

这是其一,其二是增加开头,结尾,音乐

开头结尾老样子,各一个while,请参考飞机大战

beginGoing = True
endGoing = True

然后是显示屏幕再加上点叉叉关闭的功能

都喵路,我就直接列出代码了

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

	pygame.display.update()

同时,由于开始界面,游戏界面的后面都有代码,所以为了直接退出,请改成sys.exit()

同时我们只用sys里的exit,所以我们干脆用from import

from sys import exit

那么就要把sys.去掉

现在,我们为开始界面加背景,用fill方法,rgb值和游戏界面的一样

然后把我们的小猫咪给显示出来,为了接应游戏中猫的初始位置,我们就把猫显示在那里

100x50位置

现在我们弄标题

center_image

然后显示

显示前的一步是不可缺的,你们还记得吗

这没什么难的,图片也能自己弄下来

这里坐标我给大家,300x0

center_image

接下来弄点玩法

center_image

center_image

显示,坐标为300x200

center_image

然后再弄个开始按钮,按钮长这鬼样子

center_image

弄到image文件夹下新建的button文件夹,命名为start.png

显示于125x300,设置矩形类,前两个参数为x,y坐标值各加12,后两个都是36

获取鼠标坐标,这个请参考飞机大战

然后判断按钮是否被点击,即鼠标有没有碰到按钮和按钮是否被点击,也请参考飞机大战

同时我们要告诉玩家:“看见那个按钮了吗,你怼它,对,戳它,就能开始游戏了。”

center_image

命名为guide1.png,存放于title.png所属文件夹

然后显示center_image

这样,开始界面的初始设定就完成了

最终代码如下

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 = True
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")

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 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

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
	
	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()

pygame.quit()

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

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

written by : 天上的流星

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

 

p.s. 我加工作室了,名字前要加工作室的名称,所以我不希望有人盗用我天上的流星这个名字

总而言之,看我的头像吧,是一个东德国旗加上一个装吐(兔)的人


回复

上一页1 页 / 共 0下一页