2013-06-25 4 views
0

나는 바닥으로 튀는 공처럼 루프에서 튀어 오르기 위해 텍스트를 가져 와서 애니메이션으로 만들 수있는 프로그램을 작성하려고합니다. 필자는 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() 

답변

0

(견적) 당신이 필요하므로 케이 "모든 수학까지 정말 온다"- y 축를 당신이 위로 갈 수 있도록 원하는 + x 축이는 측면 방법 당신은 만들 수를 갈 수 있도록하는 경우 그것은 위아래로 움직일 것이고 수평으로 움직이면 수평으로 움직이지 않고 멈추고 매번 더 위아래로 움직일 것입니다.

그건 저에게 100 분의 1 달러입니다. 쓰는 데 5 분이 걸렸습니다

+0

감사하지만 난 '바운스'와 함께 행복 해요 : 그래서 __init__function에서 y- 축 (나는 이것에 관해 더 분명해야했다). 단지 텍스트 애니메이션을 내가 문제가있는 화면에 올바르게 blit하는 것입니다 - 이것을 설명하기 위해 사진을 올렸지 만 내 담당자가 충분히 높지는 않습니다. ( – user2521119

0

이 문제를 다시 검토 한 결과, 나는 이것을 해결할 수있었습니다. 나는 바운스를 보상하기 위해 모든 것을 추가해야했습니다. - 내가 x 축을 따라 이동하는 텍스트를 원하지 않는다, 단지 이동

self.array = [round(-x/(500/amount),2)**+self.amount** for x in array] 

작품을 완벽하게 지금 :)