【发布时间】:2014-07-21 09:22:06
【问题描述】:
# imports
import pygame
import sys
import random
def randomcolor():
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
selection = random.randrange(1, 5)
if selection == 1:
return blue
elif selection == 2:
return red
elif selection == 3:
return green
elif selection == 4:
return white
def start(dx, dy):
#initialize
pygame.init()
size = width, height = 640, 480
x = width / 2
y = height / 2
randc = (0, 0, 255)
#call window as ROOT
root = pygame.display.set_mode(size)
pygame.display.set_caption("Moving Object")
#main loop
while 1:
root.fill(0)
if x > 640:
randc = randomcolor()
dx = -dx
x += dx
elif x < 0:
randc = randomcolor()
dx = -dx
x += dx
else:
x += dx
if y > 480:
randc = randomcolor()
dy = -dy
y += dy
elif y < 0:
randc = randomcolor()
dy = -dy
y += dy
else:
y += dy
pygame.draw.circle(root, randc, (x, y), 20, 0)
pygame.time.wait(10)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
velx = input("Enter velocity in x: ")
vely = input("Enter velocity in y: ")
start(velx, vely)
由于缺少更好的词,我基本上希望我的程序在两个“关键帧”之间渲染帧,以便我的程序中的动画在更高的速度下看起来更清晰。我目前使用 pygame.time 的原因是,即使在速度 1 下,动画也非常快,所以这是我能想到的最好的办法,将其减慢到合理的速度。
【问题讨论】:
标签: python python-2.7 pygame