内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

Python:pygame游戏编程之旅四(游戏界面文字处理)

时间:2015-02-22 23:40:53 来源: 作者: 人气: 评论:

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

一、代码

 
  1. # -*- coding:utf-8 -*-  
  2.   
  3. import os  
  4. import sys  
  5. import time  
  6.   
  7. import pygame  
  8. from pygame.locals import *  
  9. from pygame.font import *  
  10.   
  11.   
  12. def load_image(pic_name):  
  13.     ''''' 
  14.     Function:图片加载函数 
  15.     Input:pic_name 图片名称 
  16.     Output: NONE 
  17.     author: socrates 
  18.     blog:http://blog.csdn.net/dyx1024 
  19.     date:2012-04-15 
  20.     '''  
  21.     #获取当前脚本文件所在目录的绝对路径  
  22.     current_dir = os.path.split(os.path.abspath(__file__))[0]  
  23.       
  24.     #指定图片目录  
  25.     path = os.path.join(current_dir, 'image', pic_name)  
  26.       
  27.     #加载图片  
  28.     return pygame.image.load(path).convert()  
  29.   
  30. def show_text(surface_handle, pos, text, color, font_bold = False, font_size = 13, font_italic = False):   
  31.     ''''' 
  32.     Function:文字处理函数 
  33.     Input:surface_handle:surface句柄 
  34.            pos:文字显示位置 
  35.            color:文字颜色 
  36.            font_bold:是否加粗 
  37.            font_size:字体大小 
  38.            font_italic:是否斜体 
  39.     Output: NONE 
  40.     author: socrates 
  41.     blog:http://blog.csdn.net/dyx1024 
  42.     date:2012-04-15 
  43.     '''         
  44.     #获取系统字体,并设置文字大小  
  45.     cur_font = pygame.font.SysFont("宋体", font_size)  
  46.       
  47.     #设置是否加粗属性  
  48.     cur_font.set_bold(font_bold)  
  49.       
  50.     #设置是否斜体属性  
  51.     cur_font.set_italic(font_italic)  
  52.       
  53.     #设置文字内容  
  54.     text_fmt = cur_font.render(text, 1, color)  
  55.       
  56.     #绘制文字  
  57.     surface_handle.blit(text_fmt, pos)    
  58.                  
  59.      
  60. def control_ball(event):  
  61.     ''''' 
  62.     Function:控制小球运动 
  63.     Input:event 
  64.     Output: NONE 
  65.     author: socrates 
  66.     blog:http://blog.csdn.net/dyx1024 
  67.     date:2012-04-15 
  68.     '''      
  69.     #相对偏移坐标  
  70.     speed = [x, y] = [00]  
  71.       
  72.     #速度  
  73.     speed_offset = 4  
  74.       
  75.     #当方向键按下时,进行位置计算  
  76.     if event.type == pygame.KEYDOWN:  
  77.         if event.key == pygame.K_LEFT:  
  78.             speed[0] -= speed_offset  
  79.         if event.key == pygame.K_RIGHT:  
  80.             speed[0] = speed_offset  
  81.         if event.key == pygame.K_UP:  
  82.             speed[1] -= speed_offset  
  83.         if event.key == pygame.K_DOWN:  
  84.             speed[1] = speed_offset  
  85.       
  86.     #当方向键释放时,相对偏移为0,即不移动  
  87.     if event.type in (pygame.KEYUP, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_DOWN) :  
  88.         speed = [00]  
  89.               
  90.     return speed  
  91.   
  92.   
  93. def play_ball():  
  94.     ''''' 
  95.     Function:主函数 
  96.     Input:NONE 
  97.     Output: NONE 
  98.     author: socrates 
  99.     blog:http://blog.csdn.net/dyx1024 
  100.     date:2012-04-15 
  101.     '''      
  102.     pygame.init()  
  103.       
  104.     #窗口大小  
  105.     window_size = Rect(00700500)  
  106.       
  107.     #设置窗口模式  
  108.     screen = pygame.display.set_mode(window_size.size)  
  109.       
  110.     #设置窗口标题  
  111.     pygame.display.set_caption('运动的小球(2)-通过方向键控制小球移动')  
  112.       
  113.     #加载小球图片  
  114.     ball_image = load_image('ball.gif')  
  115.       
  116.     #加载窗口背景图片  
  117.     back_image = load_image('back_image.jpg')  
  118.       
  119.     #获取小球图片的区域开状  
  120.     ball_rect = ball_image.get_rect()  
  121.       
  122.     while True:  
  123.           
  124.         #退出事件处理  
  125.         for event in pygame.event.get():  
  126.             if event.type == pygame.QUIT:  
  127.                 pygame.quit()  
  128.                 sys.exit()  
  129.           
  130.         #使小球移动,速度由speed变量控制  
  131.         cur_speed = control_ball(event)  
  132.           
  133.         #Rect的clamp方法使用移动范围限制在窗口内  
  134.         ball_rect = ball_rect.move(cur_speed).clamp(window_size)  
  135.           
  136.         #设置窗口背景  
  137.         screen.blit(back_image, (00))  
  138.                
  139.         #在背景Surface上绘制 小球  
  140.         screen.blit(ball_image, ball_rect)  
  141.           
  142.           
  143.         text_time = u"时间:%s" % time.strftime("%H:%M:%S", time.gmtime())  
  144.         show_text(screen, (20400), text_time, (02550), True)  
  145.           
  146.         text_pos = u"小球位置:(%d,%d)" % (ball_rect.left, ball_rect.top)  
  147.         show_text(screen, (20420), text_pos, (0255255), True)  
  148.           
  149.         author_info = u"作者:dyx1024"  
  150.         show_text(screen, (20440), author_info, (00255), True13True)  
  151.           
  152.         title_info = u"文字测试"  
  153.         show_text(screen, (45020), title_info, (25500), True40)  
  154.           
  155.         #更新窗口内容  
  156.         pygame.display.flip()  
  157.           
  158. if __name__ == '__main__':  
  159.     play_ball()  


二、测试

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

[URL]c1a8fb04252ef0d7aa8bea8fab1b9844.png

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

[URL]bbf67e0607435cba6ce7c807c0bf1fa5.png

分享给小伙伴们:
本文标签:Python游戏  

相关文章

    无相关信息

发表评论共有条评论

友情链接1 百悠游站点 |

友情链接2: 帝国CMS |

百悠游站点,电脑学习,游戏网赚分享,游戏工作室门户网站。

Copyright (C) buyzd.cn, All Rights Reserved.

百悠游站点 版权所有