博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python:pygame游戏编程之旅四(游戏界面文字处理)
阅读量:7224 次
发布时间:2019-06-29

本文共 3053 字,大约阅读时间需要 10 分钟。

  本节讲解游戏界面中字体的处理,以在界面中实时显示当前时间、小球位置为例进行实验,具体见代码。

一、代码

# -*- coding:utf-8 -*- import os import sys import time import pygame from pygame.locals import * from pygame.font import * def load_image(pic_name): ''' Function:图片加载函数 Input:pic_name 图片名称 Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' #获取当前脚本文件所在目录的绝对路径 current_dir = os.path.split(os.path.abspath(__file__))[0] #指定图片目录 path = os.path.join(current_dir, 'image', pic_name) #加载图片 return pygame.image.load(path).convert() def show_text(surface_handle, pos, text, color, font_bold = False, font_size = 13, font_italic = False): ''' Function:文字处理函数 Input:surface_handle:surface句柄 pos:文字显示位置 color:文字颜色 font_bold:是否加粗 font_size:字体大小 font_italic:是否斜体 Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' #获取系统字体,并设置文字大小 cur_font = pygame.font.SysFont("宋体", font_size) #设置是否加粗属性 cur_font.set_bold(font_bold) #设置是否斜体属性 cur_font.set_italic(font_italic) #设置文字内容 text_fmt = cur_font.render(text, 1, color) #绘制文字 surface_handle.blit(text_fmt, pos) def control_ball(event): ''' Function:控制小球运动 Input:event Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' #相对偏移坐标 speed = [x, y] = [0, 0] #速度 speed_offset = 4 #当方向键按下时,进行位置计算 if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed[0] -= speed_offset if event.key == pygame.K_RIGHT: speed[0] = speed_offset if event.key == pygame.K_UP: speed[1] -= speed_offset if event.key == pygame.K_DOWN: speed[1] = speed_offset #当方向键释放时,相对偏移为0,即不移动 if event.type in (pygame.KEYUP, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_DOWN) : speed = [0, 0] return speed def play_ball(): ''' Function:主函数 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' pygame.init() #窗口大小 window_size = Rect(0, 0, 700, 500) #设置窗口模式 screen = pygame.display.set_mode(window_size.size) #设置窗口标题 pygame.display.set_caption('运动的小球(2)-通过方向键控制小球移动') #加载小球图片 ball_image = load_image('ball.gif') #加载窗口背景图片 back_image = load_image('back_image.jpg') #获取小球图片的区域开状 ball_rect = ball_image.get_rect() while True: #退出事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() #使小球移动,速度由speed变量控制 cur_speed = control_ball(event) #Rect的clamp方法使用移动范围限制在窗口内 ball_rect = ball_rect.move(cur_speed).clamp(window_size) #设置窗口背景 screen.blit(back_image, (0, 0)) #在背景Surface上绘制 小球 screen.blit(ball_image, ball_rect) text_time = u"时间:%s" % time.strftime("%H:%M:%S", time.gmtime()) show_text(screen, (20, 400), text_time, (0, 255, 0), True) text_pos = u"小球位置:(%d,%d)" % (ball_rect.left, ball_rect.top) show_text(screen, (20, 420), text_pos, (0, 255, 255), True) author_info = u"作者:dyx1024" show_text(screen, (20, 440), author_info, (0, 0, 255), True, 13, True) title_info = u"文字测试" show_text(screen, (450, 20), title_info, (255, 0, 0), True, 40) #更新窗口内容 pygame.display.flip() if __name__ == '__main__': play_ball()
二、测试

1、运行开始界面,小球位置为(0, 0)。

2、按下方向键,可见小球位置实时变动,当然,时间也是变动的。

转载于:https://www.cnblogs.com/dyx1024/archive/2012/04/15/2556678.html

你可能感兴趣的文章
lvs 负载均衡fullnat 模式clientip 怎样传递给 realserver
查看>>
3.关于python函数,以及作用域,递归等知识点
查看>>
正则表达式
查看>>
***博客系统文章的数据库存储方式
查看>>
新一代 CI 持续集成工具 flow.ci 正式开源
查看>>
centos7.3 docker升级
查看>>
**后台怎么处理JSON数据中含有双引号?
查看>>
【C#】as 关键字的用法
查看>>
文本文件命令(wc,cut,sort,uniq)及常用参数
查看>>
vSphere网络原理及vSwitch
查看>>
了解Oracle数据库的版本号
查看>>
Emeditor检测到列的数量不一致。
查看>>
myeclipse自定义代码风格
查看>>
使用阿里云ECS自建RDS MySQL从库
查看>>
nethogs监控进程网络流量
查看>>
LinkedHashSet 元素唯一,存储取出有序
查看>>
!!!四种常见的 POST 提交数据方式(含application/json)
查看>>
vim的用法简介
查看>>
Docker快速入门
查看>>
Linux运维常见面试题之精华收录
查看>>