【问题标题】:Why does the velocity of the platform change when the player lands on it?为什么玩家落地时平台的速度会发生变化?
【发布时间】:2020-09-13 17:10:05
【问题描述】:

我正在创建一个基本的平台系统。当玩家撞击平台时,我希望将玩家速度设置为平台速度。然而,平台速度在登陆时会发生变化,正如我的打印语句所测试的那样,尽管平台速度没有通过代码明确改变(据我所知)。我已经包含了整个源代码,因为我不确定平台速度在哪里或如何被改变。

当玩家与平台相交并且玩家速度发生变化时,平台速度似乎会发生变化,例如在打印语句“1”和“2”之间。

import pygame
import numpy as np
import random

class player:
    def __init__(self,position,velocity):
        self.position=position
        self.velocity=velocity

    def updatepos(self):
        self.position+=self.velocity

    def draw(self):
        self.updatepos()
        pygame.draw.rect(screen,(255,0,0),(self.position[0],self.position[1]-20,20,20))

class platform:
    def __init__(self,position,length,velocity):
        self.position=position
        self.length=length
        self.velocity=velocity

    def updatepos(self):
        self.position += self.velocity

    def draw(self):
        pygame.draw.rect(screen, (0, 0, 255), (self.position[0], self.position[1], self.length, 5))


width, height= 800,600
screen=pygame.display.set_mode((width,height))

player=player(np.array([100.0,100.0]),np.array([0.0,0.0]))

platforms=[ platform( np.array([100.0,200.0]), 100, np.array([1.0,1.0]) ) ]
#for _ in range(1):
    #platforms+=[platform( np.array( [float(random.randrange(0,800)),float(random.randrange(0,800))]), 50.0, np.array( [float(random.randrange(0,3)),float(random.randrange(0,3))] ))]


wpress=False
apress=False
dpress=False

#main loop
running=True
while running:

    #gravity
    print(str(platforms[0].velocity)+" 1")
    player.velocity+=np.array([0.0,0.5])
    print(str(platforms[0].velocity)+" 2")

    #checking intersections
    startline = player.position
    endline = player.position+player.velocity

    direction=endline-startline

    grounded=False
    for _ in range(len(platforms)):
        if startline[1] <= platforms[_].position[1] and endline[1] >= platforms[_].position[1]:

            scalar= (platforms[_].position[1] - startline[1]) / direction[1]

            x=startline[0]+direction[0]*scalar
            y=platforms[_].position[1]

            if platforms[_].position[0]-20 < x < platforms[_].position[0] + platforms[_].length:

                player.position = np.array( [x,y] )

                player.velocity=platforms[_].velocity

                grounded=True
                break

    #drawing on screen
    for _ in range(len(platforms)):
        platforms[_].position+=platforms[_].velocity
        platforms[_].draw()

    #controls
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_w:
                if grounded:
                    print(str(platforms[0].velocity) + " 21")
                    player.velocity[1]= -8.0
                    print(str(platforms[0].velocity) + " 22")

            elif event.key == pygame.K_a:
                apress=True

            elif event.key == pygame.K_d:
                dpress=True

            elif event.key == pygame.K_SPACE:

                player.position=np.array([100.0,100.0])
                player.velocity=np.array([0.0,0.0])

                platforms=[]
                for _ in range(60):
                    platforms += [
                        platform(np.array([float(random.randrange(0, 800)), float(random.randrange(0, 800))]), 50.0,
                                 np.array([float(random.randrange(0, 3)), float(random.randrange(0, 3))]))]

        elif event.type == pygame.KEYUP:

            if event.key == pygame.K_a:
                apress = False
            elif event.key == pygame.K_d:
                dpress = False

        if apress and dpress or not (apress or dpress):
            print(str(platforms[0].velocity) + " 31")
            player.velocity[0] = 0
            print(str(platforms[0].velocity) + " 32")
        elif apress:
            print(str(platforms[0].velocity) + " 41")
            player.velocity[0]=-5.0
            print(str(platforms[0].velocity) + " 42")
        elif dpress:
            print(str(platforms[0].velocity) + " 51")
            player.velocity[0] = 5.0
            print(str(platforms[0].velocity) + " 52")


    player.draw()
    pygame.display.flip()

    clock = pygame.time.Clock()
    clock.tick(5)
    screen.fill((0,0,0))

【问题讨论】:

    标签: python numpy pygame


    【解决方案1】:

    您将播放器和平台设置为共享相同的速度数组:

    player.velocity=platforms[_].velocity
    

    由于它是一个有多个对象指向它的单个数组,因此当您通过player 对象更改它时,它也会为platform 对象更改。

    【讨论】:

    • 谢谢,我无法自己解决这个问题。所以,如果我是正确的,当我将玩家速度设置为平台速度时,我会将平台速度的索引发送给玩家速度。有没有我可以使用的功能让玩家速度只接收平台速度的数据?
    • @Progamer 试试这个player.velocity = np.copy(platforms[_].velocity)。这将为您提供平台速度数组的副本,而不是对同一数组的引用。
    • 你也可以这样做player.velocity[:] = platforms[_].velocity,这样你只需复制值而不是创建一个新数组(或者只是像你一样复制引用)。更多解释见stackoverflow.com/q/19676538/1358308
    猜你喜欢
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多