首先,让我们打印可视化的几何图形,而不是文本。惠普酒吧怎么样?如果游戏的最大HP是固定的,并且当前游戏的HP可以从0到最大HP变化,那么打印这两个数据的最简单方法是什么?

../../../../_images/AdvancedOutputProcess1.gif
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 320, 240
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("AdvancedOutputProcess1.gif")
11ballrect = ball.get_rect()
12
13while True:
14    for event in pygame.event.get():
15        if event.type == pygame.QUIT: sys.exit()
16
17    ballrect = ballrect.move(speed)
18    if ballrect.left < 0 or ballrect.right > width:
19        speed[0] = -speed[0]
20    if ballrect.top < 0 or ballrect.bottom > height:
21        speed[1] = -speed[1]
22
23    screen.fill(black)
24    screen.blit(ball, ballrect)
25    pygame.display.flip()
../../../../_images/AdvancedOutputProcess2.gif
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 320, 240
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("AdvancedOutputProcess2.gif")
11ballrect = ball.get_rect()
12
13while True:
14    for event in pygame.event.get():
15        if event.type == pygame.QUIT: sys.exit()
16
17    ballrect = ballrect.move(speed)
18    if ballrect.left < 0 or ballrect.right > width:
19        speed[0] = -speed[0]
20    if ballrect.top < 0 or ballrect.bottom > height:
21        speed[1] = -speed[1]
22
23    screen.fill(black)
24    screen.blit(ball, ballrect)
25    pygame.display.flip()
../../../../_images/AdvancedOutputProcess3.gif
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 320, 240
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("AdvancedOutputProcess3.gif")
11ballrect = ball.get_rect()
12
13while True:
14    for event in pygame.event.get():
15        if event.type == pygame.QUIT: sys.exit()
16
17    ballrect = ballrect.move(speed)
18    if ballrect.left < 0 or ballrect.right > width:
19        speed[0] = -speed[0]
20    if ballrect.top < 0 or ballrect.bottom > height:
21        speed[1] = -speed[1]
22
23    screen.fill(black)
24    screen.blit(ball, ballrect)
25    pygame.display.flip()

是的,只是在某些变量发生变化时重新呈现文本。如何更改变量?该逻辑位于Event语句中。(向上或向下按可调整HP。)方法和以前一样。但它们仍然是文本,这意味着它们没有足够的可视化。如何更详细地显示这两个数据(最大HP、当前HP)?我们可以使用杂志(枪的杂志)的想法。HP是整数值,它是离散的。因此,它可以打印如下:

../../../../_images/AdvancedOutputProcess4.gif
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 320, 240
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("AdvancedOutputProcess4.gif")
11ballrect = ball.get_rect()
12
13while True:
14    for event in pygame.event.get():
15        if event.type == pygame.QUIT: sys.exit()
16
17    ballrect = ballrect.move(speed)
18    if ballrect.left < 0 or ballrect.right > width:
19        speed[0] = -speed[0]
20    if ballrect.top < 0 or ballrect.bottom > height:
21        speed[1] = -speed[1]
22
23    screen.fill(black)
24    screen.blit(ball, ballrect)
25    pygame.display.flip()
../../../../_images/AdvancedOutputProcess5.gif
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 320, 240
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("AdvancedOutputProcess5.gif")
11ballrect = ball.get_rect()
12
13while True:
14    for event in pygame.event.get():
15        if event.type == pygame.QUIT: sys.exit()
16
17    ballrect = ballrect.move(speed)
18    if ballrect.left < 0 or ballrect.right > width:
19        speed[0] = -speed[0]
20    if ballrect.top < 0 or ballrect.bottom > height:
21        speed[1] = -speed[1]
22
23    screen.fill(black)
24    screen.blit(ball, ballrect)
25    pygame.display.flip()
../../../../_images/AdvancedOutputProcess6.gif
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 320, 240
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("AdvancedOutputProcess6.gif")
11ballrect = ball.get_rect()
12
13while True:
14    for event in pygame.event.get():
15        if event.type == pygame.QUIT: sys.exit()
16
17    ballrect = ballrect.move(speed)
18    if ballrect.left < 0 or ballrect.right > width:
19        speed[0] = -speed[0]
20    if ballrect.top < 0 or ballrect.bottom > height:
21        speed[1] = -speed[1]
22
23    screen.fill(black)
24    screen.blit(ball, ballrect)
25    pygame.display.flip()

好多了,不是吗?画逻辑在#6里面。逻辑很简单,首先画一个 大矩形 哪种颜色是黑色。然后,检查是否绘制了 较小的矩形 哪种颜色是红色或不是红色取决于当前的惠普。最后,抽签 保证金 当前的小长方形。颜色边缘为白色。我们可以在中找到位置数据的4个参数 pygame.draw.rect (我的意思是,第三个数据。第一数据是画布数据,第二数据是颜色数据,第四数据是宽度。)要理解4个参数,最简单的方法就是更改它们。将一个值更改为10或30,而其他值为20!那就可以理解了。不用解释了。

此外,现在是具体实现功能的时候了。我将Always语句和Event语句推送到主函数中。(需要#7才能找到主函数并执行它。)并为DrawHP增加了新的功能。游戏的功能化思想与普通的图形用户界面程序并不遥远。例如,更好的做法是 为每个单一值提供单一打印功能 它必须被展示出来。当然,在设计屏幕时必须设置每个值的位置。

<参考代码>::

import pygame, sys
from pygame.locals import*

maxHP = 10
white = (255,255,255)
gray = (127,127,127)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
pygame.init()
pygame.display.set_caption("HP bar Project")
width = 640 #1
height = 480 #2
myScreen = pygame.display.set_mode((width, height))
myTextFont = pygame.font.Font("HoonWhitecatR.ttf", 32)
myText = myTextFont.render((str(maxHP) + "/" + str(maxHP)), True, red, gray)
myTextArea = myText.get_rect()
myTextArea.center = (width/2, height/2) #3
fpsClock = pygame.time.Clock()

def main(): #4
    HP = 5

    while True:
        myText = myTextFont.render((str(HP) + "/" + str(maxHP)), True, red, gray)

        myScreen.fill(gray)

        myScreen.blit(myText, myTextArea)
        drawHP(HP) #5

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_UP:
                    if HP != 10:
                        HP = HP + 1
                elif event.key == K_DOWN:
                    if HP != 0:
                        HP = HP - 1

        pygame.display.update()
        fpsClock.tick(60)

def drawHP(HP): #6
    r = int((height - 40) / maxHP)

    pygame.draw.rect(myScreen, black, (20, 20, 20, 20 + ((maxHP - 0.5) * r)))

    for i in range(maxHP):
        if HP >= (maxHP - i):
            pygame.draw.rect(myScreen, red, (20, 20 + (i * r), 20, r))
        pygame.draw.rect(myScreen, white, (20, 20 + (i * r), 20, r), 1)

    return

if __name__ == '__main__': #7
    main()



Edit on GitHub