Pygame跳跃性能不一致

时间:2016-12-27 16:20:19

标签: python pygame

当我跳进我正在制作的游戏时,跳跃高度因计算机和地图而异,甚至认为我正在使用帧速率上限。例如,当我在计算机上运行游戏时,我可以跳过两个高的瓷砖但是当我在另一台计算机上运行时,我只能跳到一个高的瓷砖。

我限制帧率的方法是使用自调用pygame.init()以来当前毫秒数的deltaTime以及自调用pygame.init()以来最后一次循环迭代的毫秒数。

以下是我设置deltaTime的方法(这是在游戏循环的开头):

prevTime = curTime;
    curTime = pygame.time.get_ticks();
    if ((curTime - prevTime) / 1000.0) <= 0.0:
        deltaTime = -((curTime - prevTime) / 1000.0);
    else:
        deltaTime = ((curTime - prevTime) / 1000.0);

这是玩家类:

import pygame;
import Globals;
import Object;
import Tile;
import MAP_HANDLER;

class Obj_Player(Object.Obj_Base):
    #0 - FRAME_RIGHT_IDLE
    #1 - FRAME_RIGHT_WALK_1
    #2 - FRAME_RIGHT_WALK_2
    #3 - FRAME_RIGHT_IDLE
    #4 - FRAME_RIGHT_WALK_1
    #5 - FRAME_RIGHT_WALK_2
    Frame = 0;
    frameTime = 0.0;
    isRightFoot = True;
    #0 - LEFT
    #1 - RIGHT
    lastMoveKeyPress = 0;
    def __init__(self, x, y):
        self.x = x;
        self.y = y;
        self.speed = 300;
        self.Surface = pygame.image.load("Sprites/PLAYER_SPRITES.png");
        self.OBJ_TYPE = 1;
        self.Gravity = 10.0;
        self.collisionTop = (0, 0, 0, 0);
        self.collisionBottom = (0, 0, 0, 0);
        self.collisionRight = (0, 0, 0, 0);
        self.collisionLeft = (0, 0, 0, 0);

    def Update(self, deltaTime, cameraX):
        key = pygame.key.get_pressed();

        self.cameraX = cameraX;

        if key[pygame.K_RIGHT]:
            self.lastMoveKeyPress = 1;
            self.velX = self.speed;
            self.frameTime += deltaTime;

            if self.frameTime >= 0.25:
                if self.isRightFoot == True:
                    if self.Frame == 1:
                        self.Frame = 0;
                        self.isRightFoot = False;
                    else:
                        self.Frame = 1;
                elif self.isRightFoot == False:
                    if self.Frame == 2:
                        self.Frame = 0;
                        self.isRightFoot = True;
                    else:
                        self.Frame = 2;
                self.frameTime = 0.0;
        elif key[pygame.K_LEFT]:
            self.lastMoveKeyPress = 0;
            self.velX = -self.speed;
            self.frameTime += deltaTime;

            if self.frameTime >= 0.25:
                if self.isRightFoot == True:
                    if self.Frame == 4:
                        self.Frame = 3;
                        self.isRightFoot = False;
                    else:
                        self.Frame = 4;
                elif self.isRightFoot == False:
                    if self.Frame == 5:
                        self.Frame = 3;
                        self.isRightFoot = True;
                    else:
                        self.Frame = 5;
                self.frameTime = 0.0;
        else:
            if self.lastMoveKeyPress == 0:
                self.Frame = 3;
            else:
                self.Frame = 0;
            self.velX = 0.0;
            self.frameTime = 0.25;

        if self.x <= 0.0:
            self.x = 0.0;

        if self.x >= Globals.WINDOW_WIDTH - self.w:
            self.x = Globals.WINDOW_WIDTH - self.w;

        if self.y <= 0.0:
            self.y = 0.0;

        for i in range(MAP_HANDLER.getTileArraySize()):
            if self.collisionTop[0] < int(MAP_HANDLER.getTileArray(i).getX() - cameraX) + MAP_HANDLER.getTileArray(i).getW() and self.collisionTop[0] + self.collisionTop[2] > int(MAP_HANDLER.getTileArray(i).getX() - cameraX) and self.collisionTop[1] < int(MAP_HANDLER.getTileArray(i).getY()) + MAP_HANDLER.getTileArray(i).getH() and self.collisionTop[1] + self.collisionTop[3] > int(MAP_HANDLER.getTileArray(i).getY()):
                if MAP_HANDLER.getTileArray(i).getTileType() != '0':
                    self.y = MAP_HANDLER.getTileArray(i).getY() + 64;
                    self.velY = 0.0;

            if self.collisionRight[0] < int(MAP_HANDLER.getTileArray(i).getX() - cameraX) + MAP_HANDLER.getTileArray(i).getW() and self.collisionRight[0] + self.collisionRight[2] > int(MAP_HANDLER.getTileArray(i).getX() - cameraX) and self.collisionRight[1] < int(MAP_HANDLER.getTileArray(i).getY()) + MAP_HANDLER.getTileArray(i).getH() and self.collisionRight[1] + self.collisionRight[3] > int(MAP_HANDLER.getTileArray(i).getY()):
                if MAP_HANDLER.getTileArray(i).getTileType() != '0':
                    self.velX = 0.0;
                    self.cameraX -= 1;

            if self.collisionLeft[0] < int(MAP_HANDLER.getTileArray(i).getX() - cameraX) + MAP_HANDLER.getTileArray(i).getW() and self.collisionLeft[0] + self.collisionLeft[2] > int(MAP_HANDLER.getTileArray(i).getX() - cameraX) and self.collisionLeft[1] < int(MAP_HANDLER.getTileArray(i).getY()) + MAP_HANDLER.getTileArray(i).getH() and self.collisionLeft[1] + self.collisionLeft[3] > int(MAP_HANDLER.getTileArray(i).getY()):
                if MAP_HANDLER.getTileArray(i).getTileType() != '0':
                    self.velX = 0.0;
                    self.cameraX += 1;

            if self.collisionBottom[0] < int(MAP_HANDLER.getTileArray(i).getX() - cameraX) + MAP_HANDLER.getTileArray(i).getW() and self.collisionBottom[0] + self.collisionBottom[2] > int(MAP_HANDLER.getTileArray(i).getX() - cameraX) and self.collisionBottom[1] < int(MAP_HANDLER.getTileArray(i).getY()) + MAP_HANDLER.getTileArray(i).getH() and self.collisionBottom[1] + self.collisionBottom[3] > int(MAP_HANDLER.getTileArray(i).getY()):
                if MAP_HANDLER.getTileArray(i).getTileType() != '0':
                    self.velY = 0.0;
                    self.y = MAP_HANDLER.getTileArray(i).getY() - self.h;
                    self.falling = False;
                    self.jumping = False;
                    if key[pygame.K_UP] and self.jumping != True:
                        self.jumping = True;
                        self.velY = -400.0;
            else:
                self.falling = True;

        if self.falling == True or self.jumping == True:
            if self.velY < 500.0:
                self.velY += self.Gravity;

        #self.x += self.velX * deltaTime;
        self.cameraX += self.velX * deltaTime;
        self.y += self.velY * deltaTime;

        if self.Frame == 0:
            self.imgX = 0;
            self.imgY = 0;
            self.imgW = 16;
            self.imgH = 32;
        elif self.Frame == 1:
            self.imgX = 16;
            self.imgY = 0;
            self.imgW = 16;
            self.imgH = 32;
        elif self.Frame == 2:
            self.imgX = 31;
            self.imgY = 0;
            self.imgW = 16;
            self.imgH = 32;
        elif self.Frame == 3:
            self.imgX = 79;
            self.imgY = 0;
            self.imgW = 16;
            self.imgH = 32;
        elif self.Frame == 4:
            self.imgX = 49;
            self.imgY = 0;
            self.imgW = 16;
            self.imgH = 32;
        elif self.Frame == 5:
            self.imgX = 64;
            self.imgY = 0;
            self.imgW = 16;
            self.imgH = 32;
        else:
            self.Frame = 0;

        self.w = 64;
        self.h = 128;

        self.collisionTop = (self.x  + 15, self.y, self.w - 30, 5);
        self.collisionBottom = (self.x + 15, self.y + self.h - 5, self.w - 30, 5);
        self.collisionLeft = (self.x, self.y + 5, 5, self.h - 10);
        self.collisionRight = (self.x + self.w - 5, self.y + 5, 5, self.h - 10);

    def Draw(self, window):
        if self.x >= 0 - self.w and self.x <= Globals.WINDOW_WIDTH and self.y >= 0 - self.h and self.y <= Globals.WINDOW_HEIGHT:
            crop = pygame.Surface((self.imgW, self.imgH));
            crop.blit(self.Surface, (0, 0), (self.imgX, self.imgY, self.imgW, self.imgH));
            crop = pygame.transform.scale(crop, (self.w, self.h));
            crop.set_colorkey((48, 4, 0));
            window.blit(crop, (self.x, int(self.y)));

0 个答案:

没有答案