Python font game font Based on pygame (with source code), pythonpygame

Source: Internet
Author: User

Python font game font Based on pygame (with source code), pythonpygame

This article describes Python font game fonts Based on pygame. We will share this with you for your reference. The details are as follows:

In pygame game development, beautiful fonts are indispensable in a friendly UI.

Today I will give you some introductions on the font of pygame.

First, we need to determine whether there is a font module in our pygame.
Copy codeThe Code is as follows: if not pygame. font: print ('Warning, fonts disabled ')
If yes, you can perform the following operations :-)

We can use the font in pygame as follows:
Copy codeThe Code is as follows: tork_font = pygame. font. Font ('data \ font \ TORK ____. ttf', 20)
Of course, you can also use the built-in fonts in the system:
Copy codeThe Code is as follows: my_font = pygame. font. SysFont ("arial", 10)
Parameter 1: font name

Parameter 2: font size

Compare the two methods above. One is a Custom font and the other is a built-in font.

Custom fonts are better, because custom fonts can be packaged in pygame during packaging.

In this way, we can perform a good transplant. After all, not every system has a corresponding font,

So his portability is not very good, and his dependence is very large.

If the font is defined, we should display the font to suiface. We should do this:
Copy codeCode: position = tork_font.render ('Hello, I \'m Hongten', True, (255,255,255), (23, 43,234 ))
Parameter 1: displayed content

Parameter 2: whether to enable anti-aliasing. If it is True, the font will be smoother, but the speed may be slightly affected.

Parameter 3: font color

Parameter 4: The font background color (optional) can be as follows:
Copy codeThe Code is as follows: position = tork_font.render ('Hello, I \'m Hongten', True, (255,255,255 ))
The following is a demo about the font usage in pygame.

In the demo, players can use the four arrow keys on the keyboard: top, bottom, left, and right to control the movement of frogs,

During the movement process, the position of the frog is dynamically recorded in the lower left corner.

The Code is as follows:

#python fontimport os, pygamefrom pygame.locals import *from sys import exit__author__ = {'name' : 'Hongten',  'mail' : 'hongtenzone@foxmail.com',  'Version' : '1.0'}if not pygame.font: print('Warning, fonts disabled')pygame.init()SCREEN_DEFAULT_SIZE = (500, 500)BG_IMAGE_NAME = 'bg.gif'FROG_IMAGE_NAME = 'frog.gif'TORK_FONT_NAME = 'TORK____.ttf'bg_image_path = os.path.join('data\\image', BG_IMAGE_NAME)frog_image_path = os.path.join('data\\image', FROG_IMAGE_NAME)tork_font_path = os.path.join('data\\font', TORK_FONT_NAME)if not os.path.exists(bg_image_path): print('Can\'t found the background image:', bg_image_path)if not os.path.exists(frog_image_path): print('Can\'t fount the frog image:', frog_image_path)if not os.path.exists(tork_font_path): print('Can\'t fount the font:', tork_font_path)screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)bg = pygame.image.load(bg_image_path).convert()frog = pygame.image.load(frog_image_path).convert_alpha()tork_font = pygame.font.Font(tork_font_path, 20)frog_x, frog_y = 0, 0frog_move_x, frog_move_y = 0, 0while 1: for event in pygame.event.get(): if event.type == QUIT:  exit() elif event.type == KEYDOWN:  if event.key == K_LEFT:  frog_move_x = -1  elif event.key == K_UP:  frog_move_y = -1  elif event.key == K_RIGHT:  frog_move_x = 1  elif event.key == K_DOWN:  frog_move_y = 1 elif event.type == KEYUP:  frog_move_x = 0  frog_move_y = 0 frog_x += frog_move_x frog_y += frog_move_y #print(frog_x, frog_y) screen.blit(bg, (0, 0)) position_str = 'Position:' + str(frog_x) + ',' + str(frog_y) position = tork_font.render(position_str, True, (255, 255,255), (23, 43,234)) screen.blit(position, (0, 480)) screen.blit(frog, (frog_x, frog_y)) pygame.display.update()

Click here to download the complete instance code.

I hope this article will help you with Python programming.

Related Article

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.