以前的项目看起来像一个单一的图像,而不是游戏。因为没有输入,也没有控制输出的过程。当然,点击窗口上的退出按钮不算,因为它只是关闭了整个程序。首先,我们将让短信“Hello World!”要自动移动(现在项目将看起来像动画而不是单个图像),这意味着在这个项目上添加第一个处理逻辑。如何移动文本?我们知道文本的位置是在初始语句中初始化的。因此,应在Always语句中更新文本位置,并添加一些变量来处理某些内容。

../../../../_images/Bagic-PROCESS-sourcecode.png
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 220, 140
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("Bagic-PROCESS-sourcecode.png")
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/Bagic-PROCESS-resultscreen.png
 1import sys, pygame
 2pygame.init()
 3
 4size = width, height = 220, 140
 5speed = [2, 2]
 6black = 0, 0, 0
 7
 8screen = pygame.display.set_mode(size)
 9
10ball = pygame.image.load("Bagic-PROCESS-resultscreen.png")
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()

(移动世界项目的源代码及其结果屏幕)

(不是Moving World Project的全部源代码,而是部分)

(移动的世界!自动移动,就像Arkanid球或DVD屏幕保护程序一样。)

在初始语句的末尾追加了新的第1-5行。此外,在Always语句的开头插入了多个if-Else阶段(#6-#9),在Always语句的末尾插入了第11行。我们可以理解#2-#10的命令的作用。当语句总是被启动时,它们只是改变“移动世界”的位置变量。但这其中存在一个问题。“移动的世界”有多快?可以肯定的是,“运动世界”的位移是Sqrt(2)(简单的勾股方程)。但《移动的世界》的位移多久会改变一次?如果不计算Always语句的时间复杂度,就无法确定!(因为这取决于语句总是被启动的频率)和时间复杂性将因计算机而异,因此无法固定。

我们需要增加一个概念, 固定速度 进入这个项目。多么?看看#1和#11。有 pygame.time.Clock() 关于初始陈述和 tick(60) On Always语句。60表示FPS(每秒帧速率)。我们知道FPS意味着在1秒内更改显示的频率。在PYGAME中,什么功能意味着改变(=更新)显示?没错。更新()函数。因此,FPS意味着语句总是在1秒内执行的频率。因为在1个Always语句中有1个Pygame.display.update()函数。(所以,FPS意味着 选择性延迟 根据当前程序的处理速度, 非选择性加速 ,因此,如果FPS太高,则FPS无法工作。)如果我们让fps(=) time )在这个项目中是固定的,我们可以改变 速度 通过为特定游戏对象找到适当的值 位移 。在游戏开始前,需要使用pygame.time.Clock()来确定项目的速度。请注意,在调用Pygame.display.update()时必须调用tick函数。因为Tick计算的是更新函数的数量。它是在Pygame.display.update()之后可以执行的函数的一个例外。

好的,我们了解到在屏幕更新时需要“修复时间”。除非是静态游戏,否则动态游戏的每个屏幕都会频繁更换。所以,我们必须知道这一点。然而,这个项目看起来不像一个游戏,因为它的结果可以很容易地预测(没有输入来改变结果),现在,输入逻辑将被插入。

<参考代码>::

import pygame, sys
from pygame.locals import*

white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
pygame.init()
pygame.display.set_caption("Moving World Project")
myScreen = pygame.display.set_mode((640, 480))
myTextFont = pygame.font.Font("HoonWhitecatR.ttf", 32)
myText = myTextFont.render("Moving World!", True, red, green)
myTextArea = myText.get_rect()
myTextArea.center = (320, 240)
fpsClock = pygame.time.Clock() #1
x = 0 #2
y = 0 #3
moveRight = 1 #4
moveUp= 1 #5

while True:
    if (moveRight == 1): #6
        x = x + 1
        if (x >= 320 - 75):
            moveRight = 0
    elif (moveRight == 0): #7
        x = x - 1
        if (x <= -320 + 75):
            moveRight = 1

    if (moveUp == 1): #8
        y = y + 1
        if (y >= 240 - 15):
            moveUp = 0
    elif (moveUp == 0): #9
        y = y - 1
        if (y <= -240 + 15):
            moveUp = 1



myTextArea.center = (320 + x, 240 + y) #10

myScreen.fill(white)
myScreen.blit(myText, myTextArea)

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()

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



Edit on GitHub