Python: pygame game programming Tour 4 (game interface text processing)

Source: Internet
Author: User

This section describes how to process fonts on the game interface. The experiment uses real-time display of the current time and ball position on the interface as an example. For details, see the code.

I. Code


[Python]
#-*-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: Image Loading Function
Input: pic_name image name
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Obtain the absolute path of the directory where the current script file is located
Current_dir = OS. path. split (OS. path. abspath (_ file _) [0]

# Specify the image directory
Path = OS. path. join (current_dir, 'image', pic_name)

# Loading Images
Return pygame. image. load (path). convert ()
 
Def show_text (surface_handle, pos, text, color, font_bold = False, font_size = 13, font_italic = False ):
'''''
Function: Text Processing Function
Input: surface_handle: surface handle
Pos: text display position
Color: text color
Font_bold: whether to bold
Font_size: font size
Font_italic: whether it is Italic
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Obtain the system font and set the text size
Cur_font = pygame. font. SysFont ("", font_size)

# Set whether to bold attributes
Cur_font.set_bold (font_bold)

# Set whether to use italic attributes
Cur_font.set_italic (font_italic)

# Set text content
Text_fmt = cur_font.render (text, 1, color)

# Draw text
Surface_handle.b133 (text_fmt, pos)


Def control_ball (event ):
'''''
Function: Controls ball movements.
Input: event
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Relative offset coordinates
Speed = [x, y] = [0, 0]

# Speed
Speed_offset = 4

# When the direction key is pressed, the position is calculated.
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

# When the direction keys are released, the relative offset is 0, that is, they are not moved.
If event. type in (pygame. KEYUP, pygame. K_LEFT, pygame. K_RIGHT, pygame. K_DOWN ):
Speed = [0, 0]

Return speed
 
 
Def play_ball ():
'''''
Function: Main Function
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
Pygame. init ()

# Window size
Window_size = Rect (0, 0,700,500)

# Set window mode
Screen = pygame. display. set_mode (window_size.size)

# Set the window title
Pygame. display. set_caption ('moving ball (2)-control the ball moving through the arrow key ')

# Loading small ball Images
Ball_image = load_image('ball.gif ')

# Loading window background images
Back_image = load_image('back_image.jpg ')

# Obtain the opening area of a small ball Image
Ball_rect = ball_image.get_rect ()

While True:

# Exit event processing
For event in pygame. event. get ():
If event. type = pygame. QUIT:
Pygame. quit ()
Sys. exit ()

# Make the ball move, and the speed is controlled by the speed variable
Cur_speed = control_ball (event)

# The clamp method of the Rect uses a moving range within the window.
Ball_rect = ball_rect.move (cur_speed). clamp (window_size)

# Set the window background www.2cto.com
Screen. Bits (back_image, (0, 0 ))

# Draw a ball on the background Surface
Screen. Bits (ball_image, ball_rect)


Text_time = u "time: % s" % time. strftime ("% H: % M: % S", time. gmtime ())
Show_text (screen, (20,400), text_time, (0,255, 0), True)

Text_pos = u "Ball Position :( % d, % d)" % (ball_rect.left, ball_rect.top)
Show_text (screen, (20,420), text_pos, (0,255,255), True)

Author_info = u "Author: dyx1024"
Show_text (screen, (20,440), author_info, (0, 0,255), True, 13, True)

Title_info = u "text test"
Show_text (screen, (450, 20), title_info, (255, 0, 0), True, 40)

# Update window content
Pygame. display. flip ()

If _ name _ = '_ main __':
Play_ball ()


Ii. Test
1. Run the start interface. The position of the ball is (0, 0 ).

2. Press the arrow key to see the real-time change of the ball position. Of course, the time is also changed.

 


From Socrates Column
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.