나는 바닥으로 튀는 공처럼 루프에서 튀어 오르기 위해 텍스트를 가져 와서 애니메이션으로 만들 수있는 프로그램을 작성하려고합니다. 필자는 Pygame (Peter Pete Shinners, 당신이 누구이던간에 누구에게나 감사드립니다)에게 아직 처음이라 생각했던 비슷한 코드 조각을 사용했습니다. 그러나 코드를 업데이트하고 오랜 시간 플레이 해본 후에도 여전히 화면에 제대로 표시되도록하십시오. 텍스트는 렌더링 된 영역 위로 시작한 다음 점차적으로보기로 떨어지지만 텍스트의 맨 위 부분은 잘립니다.파이 게임에서 튀는 텍스트 애니메이션 문제
블리치 영역을 창 주위로 이동하고 사각형의 크기를 조정하고 프로그램이 사용하고있는 표면을 시도했지만 아무것도 수정하지 않은 것 같습니다.
import os, sys, math, pygame, pygame.font, pygame.image
from pygame.locals import *
def bounce():
# define constants
G = 0.98
FLOOR = 0
COEFFICIENT = 0.8
#define variables
ball = 500
direction = 'DOWN'
v = 0
count = 0
#create array to store data
array = [ball]
while True:
if count == 4:
return array
elif ball > FLOOR and direction == 'DOWN':
v += G
if (ball - v) >= FLOOR:
ball = ball - v
array.append(round(ball,2))
else:
ball = FLOOR
array.append(round(ball,2))
direction = 'UP'
v *= COEFFICIENT
count += 1
elif ball >= FLOOR and direction == 'UP':
v -= G
if (ball + v) >= FLOOR:
ball = ball + v
array.append(round(ball,2))
if v <= 0:
direction = 'DOWN'
else:
ball = FLOOR
array.append(ball)
direction = 'UP'
v *= COEFFICIENT
class textBouncy:
array = bounce()
def __init__(self, font, message, fontcolor, amount=10):
# Render the font message
self.base = font.render(message, 0, fontcolor)
# bounce amount (height)
self.amount = amount
#size = rect of maximum height/width of text
self.size = self.base.get_rect().inflate(0, amount).size
#normalise array to meet height restriction
self.array = [round(-x/(500/amount),2) for x in array]
def animate(self):
# create window surface s
s = pygame.Surface(self.size)
# height = max inflated height
height = self.size[1]
# define a step-sized rectangle in the location of the step
src = Rect(0, 0, self.base.get_width(), height)
# moves the message according to the array list.
dst = src.move(0, self.array[i])
if (i + 1) == len(self.array):
global i
i = 0
# blits the information onto the screen
s.blit(self.base, dst, src)
return s
entry_info = 'Bouncing ball text'
if __name__ == '__main__':
pygame.init()
#create text renderer
i = 0
array = bounce()
bigfont = pygame.font.Font(None, 60)
white = 255, 255, 255
renderer = textBouncy(bigfont, entry_info, white, 16)
text = renderer.animate()
#create a window the correct size
win = pygame.display.set_mode(text.get_size())
win.blit(text, (0, 10))
pygame.display.flip()
#run animation loop
finished = 0
while True:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
text = renderer.animate()
i += 1
win.blit(text, (0, 10)) # blits the finished product from animate
pygame.display.flip()
감사하지만 난 '바운스'와 함께 행복 해요 : 그래서 __init__function에서 y- 축 (나는 이것에 관해 더 분명해야했다). 단지 텍스트 애니메이션을 내가 문제가있는 화면에 올바르게 blit하는 것입니다 - 이것을 설명하기 위해 사진을 올렸지 만 내 담당자가 충분히 높지는 않습니다. ( – user2521119