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 になるという意味ではありません。したがって、コンピュータが 1 秒あたり 60 フレームを処理できない場合、60 フレーム未満になります。

busy_loop を使用するという Cristph Terasa の推奨事項は役に立ちますが、私は個人的には経験がなく、異なる FPS 間でゲーム速度を正規化する方法を共有したいと考えています。

車輪を再発明するのではなく、それを説明する質問へのリンクを次に示します。pmoleri によって書かれた 2 番目の回答をお勧めします。 Pygame では、さまざまな fps 値でのゲーム速度の正規化

このソリューションは、フレーム レートに関係なく、ゲームを同じ速度で実行するのに役立ちます。

于 2016-04-18T17:14:41.707 に答える