화면 가장자리에서 튀어 나와서 상자가 튀어 나오는 애니메이션을 만들려고합니다. 그리고 시간 기반 애니메이션과 더러운 직사각형을 사용하여이를 수행하려고합니다.파이 게임의 고르지 애니메이션
상자를 움직일 수있었습니다. 그러나 애니메이션이 고르지 않습니다.
30 FPS : https://www.youtube.com/watch?v=0de8ENxn7GQ
60 FPS : 여기 https://www.youtube.com/watch?v=b5sXgeOlgHU
그리고 내 코드입니다 : 아무것도 내가
import sys
import random
import pygame
class Box:
def __init__(self, x, y):
self.width = 38
self.height = 38
self.color = (255, 0, 0)
self.x = x
self.y = y
self.old_x = x
self.old_y = y
self.d_x = 1
self.d_y = -1
self.px_per_second = 200
def move(self):
self.old_x = self.x
self.old_y = self.y
if self.x <= 0:
self.d_x *= -1
if self.x + self.width >= task.width:
self.d_x *= -1
if self.y <= 0:
self.d_y *= -1
if self.y + self.height >= task.height:
self.d_y *= -1
self.x += ((self.px_per_second*self.d_x)*
(task.ms_from_last_frame/1000.0))
self.y += ((self.px_per_second*self.d_y)*
(task.ms_from_last_frame/1000.0))
def draw(self):
self.x_i = int(self.x)
self.y_i = int(self.y)
self.old_x_i = int(self.old_x)
self.old_y_i = int(self.old_y)
_old_rect = (pygame.Rect(self.old_x_i, self.old_y_i,
self.width, self.height))
_new_rect = (pygame.Rect(self.x_i, self.y_i, self.width, self.height))
if _old_rect.colliderect(_new_rect):
task.dirty_rects.append(_old_rect.union(_new_rect))
else:
task.dirty_rects.append(_old_rect)
task.dirty_rects.append(_new_rect)
pygame.draw.rect(task.screen, task.bg_color, _old_rect)
pygame.draw.rect(task.screen, (self.color), _new_rect)
class ObjectTask:
def __init__(self, width, height):
pygame.init()
self.max_fps = 60
self.clock = pygame.time.Clock()
self.width = width
self.height = height
self.bg_color = (255, 255, 255)
self.dirty_rects = []
self.screen = pygame.display.set_mode((self.width, self.height),
pygame.FULLSCREEN)
self.screen.fill(self.bg_color)
pygame.display.update()
def animation_loop(self):
self.box1 = Box(self.width/2, self.height/2)
while 1:
self.ms_from_last_frame = self.clock.tick(self.max_fps)
self.box1.move()
self.box1.draw()
pygame.display.update(self.dirty_rects)
self.dirty_rects = []
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if __name__ == "__main__":
task = ObjectTask(726, 546)
task.animation_loop()
거기에 여기에 대해 무엇을 이야기하고있는 것을 도시 두 개의 동영상이 있습니다 choppiness을 줄이기 위해 할 수 있습니까? 또한, 나는 파이 게임 (Pygame)에 익숙하지 않기 때문에 내가 잘못하고/비효율적으로하는 것을 보았다면 알려주십시오.
12 비트 RAM이 장착 된 64 비트 Windows 7, i5-6300u 컴퓨터에서 애니메이션을 실행하고 있습니다. 파이썬 2.7.12와 파이 게임 1.9.2를 사용하고 있습니다.
미리 감사드립니다.
많은 불필요한 속성. 'self.x_i','self.old_x_i' (그리고 y's)는 지역 변수가 될 수 있습니다. 'self.d_x'는'200'과'self.d_y = -200'이므로'self.px_per_second'는 필요 없습니다. 'self.box1'은 또한 지역 변수가 될 수 있습니다. 그리고 단순화를 위해'_old_rect = pygame.Rect (self.x, self.y, self.height, self.width)'를 사용하여 새로운 사각형을 만들 수 있습니다. x와 y를 암시 적으로 정수로 변환하므로 필요가 없습니다. 너 스스로해라. 귀하의 비디오를 기반으로하면 귀하의 더러운 rects에 문제가있는 것 같습니다. 잠깐 살펴 보겠습니다. 몇 가지 간단한 팁을 공유 할 수있을 것 같아요. –