Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 精灵在pygame中不会向左或向右移动_Python_Pygame - Fatal编程技术网

Python 精灵在pygame中不会向左或向右移动

Python 精灵在pygame中不会向左或向右移动,python,pygame,Python,Pygame,我试图通过按箭头键向左或向右移动一个名为Player的精灵。可以通过改变精灵矩形的中心来向右或向左移动。正如您可能看到的,我试图从精灵的x坐标中加/减8,以便将其向右或向左移动。但是,当我按箭头键时,精灵不会移动。我怎样才能解决这个问题 import pygame import random import time import pygame as pg # set the width and height of the window width = 800 height = 370 gro

我试图通过按箭头键向左或向右移动一个名为Player的精灵。可以通过改变精灵矩形的中心来向右或向左移动。正如您可能看到的,我试图从精灵的x坐标中加/减8,以便将其向右或向左移动。但是,当我按箭头键时,精灵不会移动。我怎样才能解决这个问题

import pygame
import random
import time
import pygame as pg


# set the width and height of the window
width = 800
height = 370
groundThickness = 30
pheight = 50
pwidth = 50
playerx = 0+pwidth
playery = height-groundThickness-pheight/2
fps = 30
# define colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (5, 35, 231)
# initialize pygame 
pygame.init()

# initialize pygame sounds
pygame.mixer.init()
# create window
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("my game")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.Surface((pwidth, pheight))
            self.image.fill(red)
            self.rect = self.image.get_rect()
            self.rect.center = (playerx, playery)

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

# Game loop
running = True
x_change = 0
while running:
    # keep loop running at the right speed
    clock.tick(fps)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                print("left")
                x_change = -8
            elif event.key == pygame.K_RIGHT:
                print("right")
                x_change = 8
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0
        print(x_change)
        playerx += x_change
        all_sprites.update()



    #ground
    pygame.draw.rect(screen, (0,255,0), ((0, height-groundThickness), (width, groundThickness)))



    # Update
    all_sprites.update()



    # Draw / render
    all_sprites.draw(screen)
    pygame.display.update()
    # AFTER drawing everything, flip the display
    pygame.display.flip()

pygame.quit()

你从未改变过精灵玩家的位置。 你认为它会怎样移动? 您所做的只是更改局部变量playerx的值,但从未将更改推回到sprite对象中

首先添加下面的中间线:

    playerx += x_change
    player.rect.center = (playerx, playery)
    all_sprites.update()

看到了吗?你能从那里开始吗?

还需要用背景色或图像填充屏幕,以使屏幕保持移动精灵的当前位置的更新。以下命令将添加到游戏循环的绘制/渲染部分的顶部:

screen.fill(black)

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。这样做的目的是使一面变大,但不会整体移动精灵。是否有更好的方法移动精灵;而不是移动中心?是的。看任何一本在线教程。它的作用是使一面变大,但不会移动整个精灵@CJPeine你必须每帧用背景色或图像填充屏幕以清除它,然后重新绘制所有其他内容。-我还建议为你的玩家精灵添加一个更新方法,并将运动代码playerx+=x_change等放到这个方法中。更新方法将被主循环中的所有_sprite.update调用。好的,所以我让sprite移动。我怎样才能按住箭头键并持续移动精灵?