簡體   English   中英

如何讓我的播放器在屏幕中間左右移動

[英]How do I make my player move left and right at the middle of the screen

所以我一直在嘗試這樣做,當我的播放器按下 d 時,屏幕會移動但播放器會停留在屏幕中間,我遇到的問題是我用來使它看起來像屏幕的代碼是移動對我不起作用,即使我使屏幕的速度與玩家的速度相同或更快,屏幕的移動速度也會比玩家慢。 https://gyazo.com/1c74b0fc4729106af34d2a41c2b68f40我正在嘗試使屏幕向左、向右和向上移動。

我正在使用的代碼不起作用

    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


我的完整代碼

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()

如果您希望玩家保持居中,請刪除移動玩家的代碼。

試試這個更新的代碼:

    # 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM