pygame render怎么显示中文_[Pygame] 写一个带Button的开始页面

最近在玩Pygame小游戏,因而想把一些部分整理下来供往后学习参考。

效果大概是这样的:

v2-5bc28f2aa8c4c059f5229d0fa3a21379_b.jpg
游戏的名称:Starting Screen,两个游戏按钮Play 和 Exit,当鼠标碰触时按钮会发生高亮。点击按钮退出Startting_Screen函数,正常游戏中会进入到游戏的主页面

导入pygame

为了效果好一些,我选了张背景图bg.jpg,剩下的部分就是设定好屏幕的宽、高
pygame的初始化还是需要的
import pygame

display_width = 1200
display_height = 600

WHITE = (255, 255, 255)
RED = (255, 0, 0)
bg_location = 'bg.jpg'

pygame.init()

按钮类 Class Button

说是Button,其实就是设定特定的text要在屏幕的哪个地方显示,而当鼠标移到这个text所占的区域的时候(or 点击),程序再做出对应的操作
class Button(object):
	def __init__(self, text, color, x=None, y=None, **kwargs):
		self.surface = font.render(text, True, color)

		self.WIDTH = self.surface.get_width()
		self.HEIGHT = self.surface.get_height()

		if 'centered_x' in kwargs and kwargs['centered_x']:
			self.x = display_width // 2 - self.WIDTH // 2
		else:
			self.x = x

		if 'centered_y' in kwargs and kwargs['cenntered_y']:
			self.y = display_height // 2 - self.HEIGHT // 2
		else:
			self.y = y

	def display(self):
		screen.blit(self.surface, (self.x, self.y))

	def check_click(self, position):
		x_match = position[0] > self.x and position[0] < self.x + self.WIDTH
		y_match = position[1] > self.y and position[1] < self.y + self.HEIGHT

		if x_match and y_match:
			return True
		else:
			return False

开始页面 Starting Screen

开始页面做的,,就是加载背景、Button,如果加了音乐的话,还需要这里进行音乐的播放
在Button的显示上,这边设置了当鼠标移到Button上就显示红色高亮,这个也是通过加载text中实现的,一个while中的条件判读
def starting_screen():
	screen.blit(bg, (0,0))

	game_title = font.render('Starting Screen', True, WHITE)

	screen.blit(game_title, (display_width//2 - game_title.get_width()//2, 150))

	play_button = Button('Play', RED, None, 350, centered_x=True)
	exit_button = Button('Exit', WHITE, None, 400, centered_x=True)
	
	play_button.display()
	exit_button.display()

	pygame.display.update()

	while True:

		if play_button.check_click(pygame.mouse.get_pos()):
			play_button = Button('Play', RED, None, 350, centered_x=True)
		else:
			play_button = Button('Play', WHITE, None, 350, centered_x=True)

		if exit_button.check_click(pygame.mouse.get_pos()):
			exit_button = Button('Exit', RED, None, 400, centered_x=True)
		else:
			exit_button = Button('Exit', WHITE, None, 400, centered_x=True)

		play_button.display()
		exit_button.display()
		pygame.display.update()

		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				raise SystemExit
		if pygame.mouse.get_pressed()[0]:
			if play_button.check_click(pygame.mouse.get_pos()):
				break
			if exit_button.check_click(pygame.mouse.get_pos()):
				break

调用函数

这边到了实现的部分,你需要一个screen作为基础,然后加载背景图,加载字体,之后再调用Starting_scrreen函数
screen = pygame.display.set_mode((display_width, display_height))
bg = pygame.image.load(bg_location)
font_addr = pygame.font.get_default_font()
font = pygame.font.Font(font_addr, 36)

starting_screen()

总结:

Pygame所涉及的模块并不多,实现的2D小游戏也没有多少市场了,但编程的初学者可以用来了解项目的流程及熟悉Python及面向对象编程。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值