简单游戏中的PYGAME低帧速率

Pygame low frame rate on simple game(简单游戏中的PYGAME低帧速率)

本文介绍了简单游戏中的PYGAME低帧速率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个沙盒游戏/模拟,它看起来像danbal.com的PowderGame,但要简单得多。

当生成一定数量的方块时,我的游戏会滞后

不要注意评论

import pygame
import time
import random

pygame.init()

clock = pygame.time.Clock()

fps = 120

wnx = 800
wny = 600

black = (0,0,0)
grey = (80,80,80)
white = (255,255,255)
black_transparent = (255,255,255,128)
red = (255,0,0)
BACKGROUNDCOLOR = (40,40,40)

#__ Elements __

sand = (255,160,50)
rock = (125,125,125)
bsand = (255,180,150)
brock = (180,180,180)
dirt = (110, 45, 0)
bdirt = (200, 90, 0)
water = (0, 150, 255)
bwater = (25, 200, 255)
wall = (100,100,100)
bwall = (140,140,140)

erase = False

Onbutton = False
color = sand
cubec = sand

wn = pygame.display.set_mode((wnx, wny))
wn.fill(white)

def cursor(cux,cuy,cuw):
    boxc = pygame.draw.rect(wn, black, [cux, cuy, cuw, cuw], 1)                    


def message(Font,Size,colort,xt,yt,text):
    font = pygame.font.SysFont('freepixelregular', Size, True)
    text = font.render(text, True, colort)
    wn.blit(text, (xt, yt))



def cube(cx,cy,cw,ch,cubec):
    pygame.draw.rect(wn, cubec, [cx, cy, cw, ch])


def floor(fx,fy,fw,fh):
    pygame.draw.rect(wn, grey, [fx, fy, fw, fh])
    pygame.draw.line(wn, black, (150,504), (800, 504), 10)

def sidebar(sx,sy,sw,sh):
    pygame.draw.rect(wn, grey, [0, 0, 150, 600])
    pygame.draw.line(wn, black, (154,0), (154, 500), 10)


def button(bx, by, bw, bh, text, abcol, bcol, colorchange):

    global color

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()


    if bx+bw > mouse[0] > bx and by+bh > mouse[1] > by:
        Onbutton = True
        pygame.draw.rect(wn, abcol, [bx, by, bw, bh])
        if click[0] == 1 and colorchange != None:
            color = colorchange

    else:
        pygame.draw.rect(wn, bcol, [bx, by, bw, bh])
        Onbutton = False





    font = pygame.font.SysFont('freepixelregular', 25,True)
    text = font.render(text, True, black)
    wn.blit(text, (bx + (bw/14), by + (bh/4)))


def main():


    number = 0

    toggle_fast = False
    erase = False


    cubex = [0] * number
    cubey = [0] * number
    cubec = [0] * number
    cubew = 10  #cube size
    cubeh = cubew


    floory = 500

    gravity = (cubew*-1)


    clickt = False

    exit = False



    while not exit:



        #________________ QUIT ________________________________________

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                exit = True

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_SPACE:
                    toggle_fast = not toggle_fast
                if event.key == pygame.K_v:
                    erase = not erase


        #_____________________ Click / spawn cube / erase cube _____________________________

            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()

            if toggle_fast == False:

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if mouse[1] < floory and mouse[0] >= 154:



                        cubex.append(round((mouse[0]/cubew),0)*cubew)
                        cubey.append(round((mouse[1]/cubew),0)*cubew)
                        cubec.append(color)





            if click[0] == 1 and toggle_fast == True:
                print(erase)
                if mouse[1] < floory and mouse[0] >= 154:



                    cubex.append(round((mouse[0]/cubew),0)*cubew)
                    cubey.append(round((mouse[1]/cubew),0)*cubew)
                    cubec.append(color)




        #_____________________ GRAVITY _____________________________

        for i in range(len(cubex)):
            cubeR = pygame.Rect(cubex[i], cubey[i] + cubew, cubew, cubeh)
            cisect = [j for j in range(len(cubey)) if j != i and cubeR.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
            watercheck = [j for j in range(len(cubey)) if j != i and cubec[i] != (0, 150, 255) and cubec[j] == (0, 150, 255) and cubeR.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]

            if not any(cisect) and not (cubey[i] + cubew) >= floory:
                if not cubec[i] == (100,100,100):
                    cubey[i] -= gravity



            #for j in range(len(cubex):
            #   if any(watercheck):
            #       if not (cubey[i] + cubew) >= floory or any(cisect):
        #               oldposy = CUBEINFO[i][1] 
        #               oldposx = CUBEINFO[i][0]

        #               CUBEINFO.append(oldposx, oldposy, (0, 150, 255))

                        #cubex.append(oldposx)
                        #cubey.append(oldposy)
                        #cubec.append((0, 150, 255))



        #________water physics___________   

            cubeRxr = pygame.Rect(cubex[i] - cubew, cubey[i], cubew, cubeh)
            cubeRxl = pygame.Rect(cubex[i] + cubew, cubey[i], cubew, cubeh)
            cubeRdiagr = pygame.Rect(cubex[i] + 10, cubey[i] + 10, cubew, cubeh)
            cubeRdiagl = pygame.Rect(cubex[i] - 10, cubey[i] + 10, cubew, cubeh)
            cisectx = [j for j in range(len(cubex)) if j != i and cubeRxr.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
            cisectxl = [j for j in range(len(cubex)) if j != i and cubeRxl.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]
            cisectdr = [j for j in range(len(cubex)) if j != i and cubeRdiagr.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))] 
            cisectdl = [j for j in range(len(cubex)) if j != i and cubeRdiagl.colliderect(pygame.Rect(cubex[j], cubey[j], cubew, cubeh))]



            if cubec[i] == (0, 150, 255):

                if (cubey[i] + cubew) >= floory or any(cisect):  # on ground


                    if not (cubex[i] + cubew) >= 800 and not cubex[i] <= 164:


                        if any(cisectx) and not any(cisectxl):                                              #going right because of right wall
                            cubex[i] += 10
                        elif any(cisectxl) and not any(cisectx):                                                #going left because of left wall
                            cubex[i] -= 10

                        elif any(cisectx) and any(cisectxl):
                            cubex[i] += 0

                        elif any(cisect) or (cubey[i] + cubew) >= floory:

                            negative = [-10, 10]
                            cubex[i] += random.choice(negative)

                        elif any(cisect) and not any(cisectdl) and not any(cisectdr):
                            negative = [-10, 10]
                            cubex[i] += random.choice(negative) 




        #____________________ Element _____________________________

        #_____________________ DRAW _____________________________

        wn.fill(BACKGROUNDCOLOR)

        floor(0,floory,800,100)
        sidebar(0, 0, 150, 600)

        for i in range(len(cubex)):
            cube(cubex[i], cubey[i], cubew, cubeh, cubec[i])

        cursor(round((mouse[0]/cubew),0)*cubew, round((mouse[1]/cubew),0)*cubew, cubew,)

        button(20, 40, 50, 40, 'RCK', brock, rock, (125,125,125))
        button(20, 85, 50, 40, 'SND', bsand, sand, (255,160,50))
        button(20, 130, 50, 40, 'DRT', bdirt, dirt, (110, 45, 2))
        button(20, 175, 50, 40, 'WTR', bwater, water, (0, 150, 255))
        button(20, 220, 50, 40, 'WLL', bwall, wall, (100,100,100))

        #(Font,Size,colort,xt,yt,message):
        message(None, 20, black, 10,400,('ERASE:'+str(erase)))

        pygame.display.update()
        clock.tick(fps)

main()

pygame.quit()
quit()

可能是因为正方形的位置(称为立方体、立方体)位于不同的列表中或其他什么地方?

我刚开始使用python,所以这可能是一个愚蠢的错误

谢谢你的帮助!

推荐答案

提高游戏速度的唯一方法是避免在立方体数组中连续搜索。

要实现此目的,您必须更改数据的表示形式。你必须从另一个方向来思考这个问题。不要在某个位置搜索立方体,而是"询问"某个位置上是否有立方体。不是将多维数据集存储在列表中,而是创建一个二维游乐场网格,并将多维数据集与该网格中的某个字段相关联。

为多维数据集创建和对象(属性‘.dir’用于水,稍后解释):

class Cube:
    def __init__(self, color):
        self.color = color
        self.dir = 1

创建空操场(每个字段由None初始化):

cubew = 10  #cube size
cubeh = cubew

pg_rect = pygame.Rect(160, 0, 650, 500)
pg_size = (pg_rect.width // cubew, pg_rect.height // cubeh)
pg_grid = [[None for i in range(pg_size[1])] for j in range(pg_size[0])]

点击鼠标,就可以轻松地将立方体添加到操场上:

if event.type == pygame.MOUSEBUTTONDOWN:
    if mouse[1] < floory and mouse[0] >= 154: 

        i, j = ((mouse[0]-pg_rect.left) // cubew, (mouse[1]-pg_rect.top) // cubeh)
        if not pg_grid[i][j]:
            pg_grid[i][j] = Cube(color)

若要绘制多维数据集,必须遍历整个字段:

for i in range(pg_size[0]):
    for j in range(pg_size[1]):
        if pg_grid[i][j]:
            pos = (pg_rect.left + i * cubew, pg_rect.top + j * cubeh)
            cube(*pos, cubew, cubeh, pg_grid[i][j].color)

为了更新立方体(重力、水)的位置,必须列出所有立方体,并且必须更改立方体在字段中的位置。

cubes = [(i, j) for i in range(pg_size[0]) for j in range(pg_size[1]-1, 0, -1) if pg_grid[i][j]]
for i, j in cubes:

    # [...]

对于重力,必须检查立方体下方的场是否为"自由"(None):

fall_down = pg_grid[i][j].color != wall
if fall_down and j < pg_size[1]-1 and pg_grid[i][j+1] == None:

    pg_grid[i][j+1] = pg_grid[i][j]
    pg_grid[i][j] = None
对于水效果,必须检查self.dir所标识的立方体旁边的场是否为"自由",如果是"自由",则立方体前进。否则,它的方向必须改变:

is_water = pg_grid[i][j].color == water
if is_water:

    if pg_grid[i][j].dir < 0:
        if i <= 0 or pg_grid[i-1][j]:
            pg_grid[i][j].dir = 1
        else:
            pg_grid[i-1][j] = pg_grid[i][j]
            pg_grid[i][j] = None

    else:
        if i >= pg_size[0]-1 or pg_grid[i+1][j]:
            pg_grid[i][j].dir = -1
        else:
            pg_grid[i+1][j] = pg_grid[i][j]
            pg_grid[i][j] = None

请参见我对原始代码应用更改的示例:

import pygame
import time
import random

pygame.init()

clock = pygame.time.Clock()

fps = 120

wnx = 800
wny = 600

black = (0,0,0)
grey = (80,80,80)
white = (255,255,255)
black_transparent = (255,255,255,128)
red = (255,0,0)
BACKGROUNDCOLOR = (40,40,40)

#__ Elements __

sand = (255,160,50)
rock = (125,125,125)
bsand = (255,180,150)
brock = (180,180,180)
dirt = (110, 45, 0)
bdirt = (200, 90, 0)
water = (0, 150, 255)
bwater = (25, 200, 255)
wall = (100,100,100)
bwall = (140,140,140)

erase = False

Onbutton = False
color = sand
cubec = sand

wn = pygame.display.set_mode((wnx, wny))
wn.fill(white)

class Cube:
    def __init__(self, color):
        self.color = color
        self.dir = 1

def cursor(cux,cuy,cuw):
    boxc = pygame.draw.rect(wn, black, [cux, cuy, cuw, cuw], 1)                    

def message(Font,Size,colort,xt,yt,text):
    font = pygame.font.SysFont('freepixelregular', Size, True)
    text = font.render(text, True, colort)
    wn.blit(text, (xt, yt))

def cube(cx,cy,cw,ch,cubec):
    pygame.draw.rect(wn, cubec, [cx, cy, cw, ch])

def floor(fx,fy,fw,fh):
    pygame.draw.rect(wn, grey, [fx, fy, fw, fh])
    pygame.draw.line(wn, black, (150,504), (800, 504), 10)

def sidebar(sx,sy,sw,sh):
    pygame.draw.rect(wn, grey, [0, 0, 150, 600])
    pygame.draw.line(wn, black, (154,0), (154, 500), 10)

def button(bx, by, bw, bh, text, abcol, bcol, colorchange):

    global color

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if bx+bw > mouse[0] > bx and by+bh > mouse[1] > by:
        Onbutton = True
        pygame.draw.rect(wn, abcol, [bx, by, bw, bh])
        if click[0] == 1 and colorchange != None:
            color = colorchange
    else:
        pygame.draw.rect(wn, bcol, [bx, by, bw, bh])
        Onbutton = False

    font = pygame.font.SysFont('freepixelregular', 25,True)
    text = font.render(text, True, black)
    wn.blit(text, (bx + (bw/14), by + (bh/4)))


def main():

    number = 0

    toggle_fast = False
    erase = False

    cubew = 10  #cube size
    cubeh = cubew

    pg_rect = pygame.Rect(160, 0, 650, 500)
    pg_size = (pg_rect.width // cubew, pg_rect.height // cubeh)
    pg_grid = [[None for i in range(pg_size[1])] for j in range(pg_size[0])]

    floory = 500
    gravity = (cubew*-1)
    clickt = False
    exit = False

    while not exit:
        #________________ QUIT ________________________________________
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                exit = True

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_SPACE:
                    toggle_fast = not toggle_fast
                if event.key == pygame.K_v:
                    erase = not erase
        #_____________________ Click / spawn cube / erase cube _____________________________

            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()

            if toggle_fast == False:

                if event.type == pygame.MOUSEBUTTONDOWN:
                    if mouse[1] < floory and mouse[0] >= 154: 

                        i, j = ((mouse[0]-pg_rect.left) // cubew, (mouse[1]-pg_rect.top) // cubeh)
                        if not pg_grid[i][j]:
                            pg_grid[i][j] = Cube(color)

            if click[0] == 1 and toggle_fast == True:
                print(erase)
                if mouse[1] < floory and mouse[0] >= 154:

                        i, j = ((mouse[0]-pg_rect.left) // cubew, (mouse[1]-pg_rect.top) // cubeh)
                        if not pg_grid[i][j]:
                            pg_grid[i][j] = Cube(color)

        # update cubes
        cubes = [(i, j) for i in range(pg_size[0]) for j in range(pg_size[1]-1, 0, -1) if pg_grid[i][j]]
        for i, j in cubes:

                    fall_down = pg_grid[i][j].color != wall
                    is_water = pg_grid[i][j].color == water

                    if fall_down and j < pg_size[1]-1 and pg_grid[i][j+1] == None:
                        #_____________________ GRAVITY _____________________________
                        pg_grid[i][j+1] = pg_grid[i][j]
                        pg_grid[i][j] = None

                    elif is_water:
                        #________water physics___________ 
                        if pg_grid[i][j].dir < 0:
                            if i <= 0 or pg_grid[i-1][j]:
                                pg_grid[i][j].dir = 1
                            else:
                                pg_grid[i-1][j] = pg_grid[i][j]
                                pg_grid[i][j] = None

                        else:
                            if i >= pg_size[0]-1 or pg_grid[i+1][j]:
                                pg_grid[i][j].dir = -1   
                            else:
                                pg_grid[i+1][j] = pg_grid[i][j]
                                pg_grid[i][j] = None

        wn.fill(BACKGROUNDCOLOR)

        floor(0,floory,800,100)
        sidebar(0, 0, 150, 600)

        # draw cubes
        for i in range(pg_size[0]):
            for j in range(pg_size[1]):
                if pg_grid[i][j]:
                    pos = (pg_rect.left + i * cubew, pg_rect.top + j * cubeh)
                    cube(*pos, cubew, cubeh, pg_grid[i][j].color)

        cursor(round((mouse[0]/cubew),0)*cubew, round((mouse[1]/cubew),0)*cubew, cubew,)

        button(20, 40, 50, 40, 'RCK', brock, rock, (125,125,125))
        button(20, 85, 50, 40, 'SND', bsand, sand, (255,160,50))
        button(20, 130, 50, 40, 'DRT', bdirt, dirt, (110, 45, 2))
        button(20, 175, 50, 40, 'WTR', bwater, water, (0, 150, 255))
        button(20, 220, 50, 40, 'WLL', bwall, wall, (100,100,100))

        #(Font,Size,colort,xt,yt,message):
        message(None, 20, black, 10,400,('ERASE:'+str(erase)))

        pygame.display.update()
        clock.tick(fps)

main()

pygame.quit()
quit()

这篇关于简单游戏中的PYGAME低帧速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:简单游戏中的PYGAME低帧速率