简体   繁体   English

播放器比相机python更快

[英]Player is faster than the camera python

So I been making this game and I been trying figure out how to make my camera move, The code that I put for my camera movement works but know the sides of my platform are not colliding with my player https://gyazo.com/d330800333081323cdbc8f410a769a90 , if I put the player colliding with the sides with the "camera" movement, The player will become faster than the camera but I don't want that, I want my camera and player colliding with the sides to work.所以我一直在做这个游戏,我一直在尝试弄清楚如何让我的相机移动,我为我的相机移动放置的代码有效,但我知道我的平台的侧面没有与我的玩家发生碰撞https://gyazo.com/ d330800333081323cdbc8f410a769a90 ,如果我让玩家通过“相机”运动与侧面碰撞,玩家将变得比相机更快,但我不希望那样,我希望我的相机和玩家与侧面碰撞。

my code for camera movment我的相机运动代码

    if keys[pygame.K_d]: # and playerman.x > 350:
        for Platform in platforms:
            Platform.x -= Platform.speed

    if keys[pygame.K_a]: # and playerman.x < 350:
        for Platform in platforms:
            Platform.x += playerman.speed

This is what I wrote to make player collide with sides这是我写的让玩家与侧面碰撞的内容

        # lets player move
    keys = pygame.key.get_pressed()
    px, py = playerman.x, playerman.y
#    if keys[pygame.K_a] and px > playerman.speed:
#        px -= playerman.speed
#    if keys[pygame.K_d] and px < 700 - playerman.height - playerman.speed:
#        px += playerman.speed
#    if keys[pygame.K_w] and py > playerman.speed:
#        py -= playerman.speed
#    if keys[pygame.K_s] and py <500 - playerman.width - playerman.speed:
#        py += playerman.speed

My Full code我的完整代码

import pygame
pygame.init()

window = pygame.display.set_mode((700,500))
pygame.display.set_caption("Noobs First Game")

# Playerman
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        pygame.draw.rect(window,self.color,self.get_rect())

class bullet(object):
    def __init__(self,x,y,color):
        self.x = x
        self.y = y
        self.color = color
        self.speed = 10
        self.color = color
        self.rect.topleft = (self.x,self.y)
    def draw(self):
        self.rect.topleft = round(self.x), round(self.y)
        pygame.draw.rect(window,self.color,self.rect)

class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y =y
        self. width = width
        self.color = color
        self.height = height
        self.color = color
        self.speed = 4
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


# Colors for hitbox
white = (255,255,255)
green = (0,255,0)

# Drawing Player
playerman = Player(255,255,40,40,white)

#Drawing Platforms
platform1 = Platform(0,0,40,500,green)
platform2 = Platform(40,460,700,40,green)

# List
platforms = [platform1,platform2]

# Windows color
def redrawwindow():
    window.fill((0,0,0))

    # Drawing the player and other stuff to the screen
    playerman.draw()

    for Platform in platforms:
        Platform.draw()

x = 10
y = 10
x_change = 0
y_change = 0
old_x = x
old_y = y
fps = (30)
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False



    
    
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
            x_change = -7
        if event.key == pygame.K_a:
            x_change = 7

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_d or event.key == pygame.K_a:
            x_change = 0

        x += x_change
        if x > 500 - playerman.width or x < 0:
            x = old_x
           
        # lets player move
    keys = pygame.key.get_pressed()
    px, py = playerman.x, playerman.y
#    if keys[pygame.K_a] and px > playerman.speed:
#        px -= playerman.speed
#    if keys[pygame.K_d] and px < 700 - playerman.height - playerman.speed:
#        px += playerman.speed
#    if keys[pygame.K_w] and py > playerman.speed:
#        py -= playerman.speed
#    if keys[pygame.K_s] and py <500 - playerman.width - playerman.speed:
#        py += playerman.speed

    if keys[pygame.K_d]: # and playerman.x > 350:
        for Platform in platforms:
            Platform.x -= Platform.speed

    if keys[pygame.K_a]: # and playerman.x < 350:
        for Platform in platforms:
            Platform.x += playerman.speed

    


    platform_rect_list = [p.rect for p in platforms]
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)


    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px
  
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

        # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
                       
            # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

        # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
                py -= playerman.speed
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

    redrawwindow()
    pygame.display.update()
pygame.quit()

You have to ensure that the platforms rectangles are at the correct position when you do the collision test.在进行碰撞测试时,您必须确保平台矩形位于正确的位置。 Add a method get_rect() to the class Platform :向类Platform添加一个方法get_rect()

class Platform:
    def __init__(self,x,y,width,height,color):
        # [...]

    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        pygame.draw.rect(window,self.color,self.get_rect())

In your current setup, the player doesn't move, but the platforms are moving.在您当前的设置中,玩家不会移动,但平台会移动。 You have to undo the movement of the platforms rather than the movement of the player when a collision is detected:当检测到碰撞时,您必须撤消平台的移动而不是玩家的移动:

run = True
while run:
    # [...]

    move_right = keys[pygame.K_d]
    move_left = keys[pygame.K_a]
    if move_right: 
        for Platform in platforms:
            Platform.x -= Platform.speed
    if move_left:
        for Platform in platforms:
            Platform.x += Platform.speed

    platform_rect_list = [p.get_rect() for p in platforms] # get_rect()
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)

    playerman.y = py
    cI = player_rect.collidelist(platform_rect_list)
    if cI >= 0:
        # undo movement of platforms dependent on the direction and intersection distance
        playerman.x = px
        if move_right: 
            dx = platform_rect_list[cI].left - player_rect.right
            for Platform in platforms:
                Platform.x -= dx
                Platform.get_rect() # update rectangle
        if move_left:
            dx = platform_rect_list[cI].right - player_rect.left
            for Platform in platforms:
                Platform.x -= dx
                Platform.get_rect() # update rectangle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM