0

我遇到了一个问题,我每次执行程序时都试图让一个圆圈去同一个地方。但是每次我运行代码时,点并不总是排成一行。我在同一个地方有一个测试圈来比较运行。红色圆圈应该完美地覆盖白色圆圈,但每次我运行程序时它都会改变。我正在重置内核,因为我正在使用 pygame.time.get_ticks() 来计时一切。

import sys, pygame, math
from pygame.locals import *

# set up a bunch of constants
BLUE       = (  0,   0, 255)
WHITE      = (255, 255, 255)
ORANGE     = (255, 165,   0)
PINK       = (255,  20, 147)
RED        = (255,   0,   0)
GREEN      = (  0, 255,   0)
LIMEGREEN  = ( 50, 205,  50)
YELLOW     = (255, 255,   0)
PURPLE     = (160,  32, 240)
BLACK      = (  0,   0,   0)

#Background Colour
BGCOLOR = BLACK

#Setting Window Size and finding window x and y centre 
WINDOWWIDTH = 1918# width of the program's window, in pixels 960x540
WINDOWHEIGHT =  1078# height in pixels
WIN_CENTERX = int(WINDOWWIDTH / 2) # the midpoint for the width of the window
WIN_CENTERY = int(WINDOWHEIGHT / 2) # the midpoint for the height of the window

# frames per second to run at
FPS = 60

#intializing Variables
AMPLITUDE = 450 

colourArray=[BLUE,WHITE,YELLOW,GREEN,RED,PINK,PURPLE,LIMEGREEN,ORANGE]
i=0
xPos = 0
step = 0 
small_step =0
stop_step=step=0
xPos=0  
yPos=0 
c=RED
timestep=0

# standard pygame setup code
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.FULLSCREEN)
pygame.display.set_caption('Task1')
fontObj = pygame.font.Font('freesansbold.ttf', 16)

# main application loop
while True:
    # event handling loop for quit events
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()       

    #setup for label and time   
    tempTime=pygame.time.get_ticks()/1000         
    time_string=str(tempTime)
    instructionsSurf = fontObj.render(time_string, True, WHITE, BGCOLOR)
    instructionsRect = instructionsSurf.get_rect()
    instructionsRect.left = 10
    instructionsRect.bottom = WINDOWHEIGHT - 10

    # fill the screen to draw from a blank state
    DISPLAYSURF.fill(BGCOLOR)
    DISPLAYSURF.blit(instructionsSurf, instructionsRect)

    tempTime=pygame.time.get_ticks()/1000
    #Color change loop
    c=RED  
    if (0<=(tempTime)<3):
        c=RED
    if (3<=(tempTime)<5):
        c=BLUE
    if (5<=(tempTime)<7):
        c=GREEN
    if (7<=(tempTime)<9):
        c=YELLOW
    if (9<=(tempTime)<11):
        c=WHITE 
    if (11<=(tempTime)<17):
        c=RED
    if (17<=(tempTime)<42):
        c=RED
    if (42<=(tempTime)<46):
        c=RED
    if (46<=(tempTime)<120):
        c=colourArray[i]



    #Setting position of x and y coordinates 
    if (0<=(tempTime)<14):
        xPos = 0
        yPos = 0 
    if (14<(tempTime)<17): 
        small_step += 5.111
        xPos = small_step 
        yPos = 0
    if (17<(tempTime)<43):
        step += 0.05001 
        step %= 2 * math.pi
        xPos = math.cos(step) * AMPLITUDE
        yPos = math.sin(step) * AMPLITUDE
    if (43<(tempTime)<46):
        stop_step=step
        xPos = math.cos(stop_step) * AMPLITUDE
        yPos = math.sin(stop_step) * AMPLITUDE
    if (46<(tempTime)<120):
        step += 0.05001
        step %= 2 * math.pi
        xPos = math.cos(step) * AMPLITUDE
        yPos = math.sin(step) * AMPLITUDE  



    #test dot
    pygame.draw.circle(DISPLAYSURF, WHITE, (WIN_CENTERX+AMPLITUDE, 0+WIN_CENTERY),12,0)
    # draw dot1 
    dot1=pygame.draw.circle(DISPLAYSURF, c, (int(xPos)+ WIN_CENTERX, int(yPos) + WIN_CENTERY), 12,0)
    # draw dot2
    dot2=pygame.draw.circle(DISPLAYSURF, BLACK, (int(xPos) + WIN_CENTERX, int(yPos) + WIN_CENTERY), 6,0)



    #refresh
    pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)
    pygame.display.update()
    FPSCLOCK.tick(FPS)
4

1 回答 1

0

我只扫描了你的代码,但我猜你的不一致是由于高帧速率(60)。FPSCLOCK.tick(FPS) 将确保您达到 60,但并不意味着您将达到 60 fps。因此,如果您的计算机无法处理每秒 60 帧,它将低于 60 帧。

Cristph Terasa 对使用busy_loop 的建议应该可以帮到你,但我个人没有这方面的经验,我想分享一种跨不同FPS 规范游戏速度的方法。

而不是重新发明一个轮子,这里是一个解释它的问题的链接。我推荐第二个答案,由 pmoleri 撰写。 在 Pygame 中,标准化不同 fps 值的游戏速度

无论帧速率如何,此解决方案都应该可以帮助您的游戏以相同的速度运行。

于 2016-04-18T17:14:41.707 回答