Pygameの勉強 *正確なアニメーションの実現*
一秒間の移動ピクセルを指定する方法
#! /usr/bin/env python # -*- coding: utf-8 -*- import pygame from pygame.locals import * import sys SCR_WIDTH,SCR_HEIGHT = 640,480 pygame.init() screen = pygame.display.set_mode((SCR_WIDTH,SCR_HEIGHT)) pygame.display.set_caption('practice') backImg = pygame.image.load('grass.jpg').convert() #背景 Img = pygame.image.load('pythonImg.png').convert_alpha() #蛇 hit_sound = pygame.mixer.Sound('hit.wav') Img_rect = Img.get_rect() vx = vy = 120 #一秒間の移動ピクセル clock = pygame.time.Clock() while True: time_pressed = clock.tick(60) time_pressed_seconds = time_pressed / 1000.0 #ミリ秒を秒に変換 #画像の移動 Img_rect.x += vx * time_pressed_seconds Img_rect.y += vy * time_pressed_seconds #跳ね返り処理 if Img_rect.left < 0 or Img_rect.right > SCR_WIDTH: vx = -vx if Img_rect.top < 0 or Img_rect.bottom > SCR_HEIGHT: vy = -vy screen.blit(backImg,(0,0)) screen.blit(Img,Img_rect) pygame.display.update() for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == KEYDOWN and evnet.key == K_ESCAPE: sys.exit()
この方法の方が正確なアニメーションを実現できるみたいです♪