当+=时,numpy数组中的值不会改变

vaqhlq81  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(134)

我很困惑,为什么我的numpy数组中的值在我使用+=时没有改变,但在我使用-=时却改变了。我已经使用-= -(speed * dt)trying dims[1] = (speed * dt) + dims[1]进行了检查,但它似乎仍然不起作用。
它也不会引发错误,只是不会更改值。
下面是我的代码(我在Ubuntu上):

import pygame
import threading
import numpy as np

screen = pygame.display.set_mode([1420, 720])

playerdims = np.array([500, 200, 50, 80])
wasd = np.zeros((4, 1), dtype=bool)
clock = pygame.time.Clock()

movspeed = 5
dt = 0

def updatedims(dims = playerdims, speed = movspeed, event = None):
        
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_w:
            wasd[0] = True
        if event.key == pygame.K_a:
            wasd[1] = True
        if event.key == pygame.K_s:
            wasd[2] = True
        if event.key == pygame.K_d:
            wasd[3] = True
            
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_w:
            wasd[0] = False
        if event.key == pygame.K_a:
            wasd[1] = False
        if event.key == pygame.K_s:
            wasd[2] = False
        if event.key == pygame.K_d:
            wasd[3] = False
            
    if wasd[0]:
        dims[1] -= (speed * dt)
    if wasd[1]:
        dims[0] -= (speed * dt)
    if wasd[2]:                    
        dims[1] += (speed * dt)   # doesn't change here
    if wasd[3]:
        dims[0] += (speed * dt)   # and here
        
while True:
    
    screen.fill('black')
    
    for ev in pygame.event.get():
        thread1 = threading.Thread(target=updatedims, args=(playerdims, movspeed, ev), daemon=False)
        thread1.start()
        
        if ev.type == pygame.QUIT:
            quit()
        
    pygame.draw.rect(screen, 'red', (tuple(playerdims[:2]), tuple(playerdims[2:])))
    
    thread1.join()
    
    pygame.display.flip()
    
    dt = clock.tick(30) / 1000
3bygqnnd

3bygqnnd1#

这里有一个简化的版本,它不会无缘无故地与Numpy或线程混在一起。
当你有很多粒子或者其他你想快速更新的东西时,你可能会考虑使用Numpy,但这只会在将来考虑。
此外,线程是相当棘手的工作;你的原始代码会为Pygame事件循环中的每个事件生成一个新的线程(不一定是轻量级的),并且只会等待其中一个事件完成(使用.join()),即使是这样,也只会在你绘制了一些东西之后发生。

import pygame

screen = pygame.display.set_mode([1420, 720])
playerdims = [500, 200, 50, 80]
wasd = {
    pygame.K_w: False,
    pygame.K_a: False,
    pygame.K_s: False,
    pygame.K_d: False,
}
clock = pygame.time.Clock()
dt = 0
speed = 25

while True:

    # Read events and key state
    for event in pygame.event.get():
        # If this is a key event...
        if event.type in (pygame.KEYDOWN, pygame.KEYUP):
            # If this key is in `wasd`, update it
            if event.key in wasd:
                wasd[event.key] = (
                    event.type == pygame.KEYDOWN
                )
        if event.type == pygame.QUIT:
            quit()

    # Update position
    if wasd[pygame.K_w]:
        playerdims[1] -= speed * dt
    if wasd[pygame.K_a]:
        playerdims[0] -= speed * dt
    if wasd[pygame.K_s]:
        playerdims[1] += speed * dt
    if wasd[pygame.K_d]:
        playerdims[0] += speed * dt

    # Update screen
    screen.fill("black")
    pygame.draw.rect(
        screen,
        "red",
        (tuple(playerdims[:2]), tuple(playerdims[2:])),
    )
    pygame.display.flip()
    dt = clock.tick(30) / 1000

相关问题