cool hit counter pygame series_font game font_source code download_Intefrankly

pygame series_font game font_source code download


In pygame game development, a nice font is a must in a friendly UI

Today we will bring you some introductory notes about fonts in pygame

First we have to determine if our pygame hasfont This module

1 if not pygame.font: print('Warning, fonts disabled')

If so only then you can proceed to the next action :-)

We can use the font in pygame like this.

1 tork_font = pygame.font.Font('data\font\TORK____.ttf', 20)

Of course, you can also use the fonts that come with the system: the

1 my_font = pygame.font.SysFont("arial", 10)

Parameter 1: Font name

Parameter 2: Font size

Compare the two methods above, one with a custom font and one with the system's own font, as opposed to

Custom fonts are a little better because you can pack custom fonts in during the pygame packaging process

This allows for a good portability; whereas the fonts that come with the system, after all, are not available on every system.

So he's not very portable and dependent.

If the font is defined, then we should display the font on top of suiface, and we should do this.

1 position = tork_font.render('Hello,I'm Hongten', True, (255, 255,255), (23, 43,234))

Parameter 1: Contents of the display

Parameter 2: whether to turn on anti-aliasing, that is, True, the font will be smoother, but the corresponding speed has a little impact

Parameter 3: Font color

Parameter IV: Font background colour (optional) i.e. it could look like this.

1 position = tork_font.render('Hello,I'm Hongten', True, (255, 255,255))

A demo is given below to talk about the use of fonts in pygame

In the demo, the player can control the frog's movement using the four arrow keys on the keyboard: up, down, left and right.

As you move, the position of the frog is dynamically recorded in the lower left corner.

=============================================

Code section.

=============================================

 1 #python font
 2 
 3 import os, pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 
 7 __author__ = {'name' : 'Hongten',
 8               'mail' : 'hongtenzone@foxmail.com',
 9               'blog' : 'http://www.cnblogs.com/hongten',
10               'Version' : '1.0'}
11 
12 if not pygame.font: print('Warning, fonts disabled')
13 
14 pygame.init()
15 SCREEN_DEFAULT_SIZE = (500, 500)
16 BG_IMAGE_NAME = 'bg.gif'
17 FROG_IMAGE_NAME = 'frog.gif'
18 TORK_FONT_NAME = 'TORK____.ttf'
19 
20 bg_image_path = os.path.join('data\image', BG_IMAGE_NAME)
21 frog_image_path = os.path.join('data\image', FROG_IMAGE_NAME)
22 tork_font_path = os.path.join('data\font', TORK_FONT_NAME)
23 
24 if not os.path.exists(bg_image_path):
25     print('Can't found the background image:', bg_image_path)
26 if not os.path.exists(frog_image_path):
27     print('Can't fount the frog image:', frog_image_path)
28 if not os.path.exists(tork_font_path):
29     print('Can't fount the font:', tork_font_path)
30     
31 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
32 bg = pygame.image.load(bg_image_path).convert()
33 frog = pygame.image.load(frog_image_path).convert_alpha()
34 tork_font = pygame.font.Font(tork_font_path, 20)
35 
36 frog_x, frog_y = 0, 0
37 frog_move_x, frog_move_y = 0, 0
38 
39 while 1:
40     for event in pygame.event.get():
41         if event.type == QUIT:
42             exit()
43         elif event.type == KEYDOWN:
44             if event.key == K_LEFT:
45                 frog_move_x = -1
46             elif event.key == K_UP:
47                 frog_move_y = -1
48             elif event.key == K_RIGHT:
49                 frog_move_x = 1
50             elif event.key == K_DOWN:
51                 frog_move_y = 1
52         elif event.type == KEYUP:
53             frog_move_x = 0
54             frog_move_y = 0
55     frog_x += frog_move_x
56     frog_y += frog_move_y
57     #print(frog_x, frog_y)
58     screen.blit(bg, (0, 0))
59     position_str = 'Position:' + str(frog_x) + ',' + str(frog_y)
60     position = tork_font.render(position_str, True, (255, 255,255), (23, 43,234))
61     screen.blit(position, (0, 480))
62     screen.blit(frog, (frog_x, frog_y))
63     pygame.display.update()

Source Code Download.http://files.cnblogs.com/hongten/pygame_font_frog.rar

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten


Recommended>>
1、DIY an exclusive HID injection device
2、Is the online traffic bonus fading away Bounty Cat uses seconddegree connections to help merchants acquire offline customers
3、Get out of these six misconceptions before you really understand bitcoin
4、Baidu Security hosts BCTF Matchmaking Campus Live protects the good life with AI
5、Chinas first driverless crossseat monorail line officially opened to traffic

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号