我遇到了一个相当恼人的问题,我希望我们中间有人可能有关于它的知识,可能可以帮助。雪碧衍生物0,0,并不会更新位置
问题是,创建的代表玩家的精灵会卡在它的重生位置。 btw的产卵位置并不是我告诉它产卵的地方,并且不会接受坐标在其他地方产卵,它只会在那里产卵,恰好在屏幕的左上角。我让我的程序在运行时持续打印它的位置以及改变坐标的命令,并且它接收应该更新的信息,精灵不会移动。它的行为方式就好像我碰到了一堵不可逾越的墙壁。
relavent巨蟒/ pygame的代码:
class Player(pygame.sprite.Sprite):
#This class represents the Player.
def __init__(self):
#Set up the player on creation.
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
#draws on the sprite
self.image = pygame.image.load("spaceship.png").convert_alpha()
# scales sprite based on resolution
self.image = pygame.transform.scale(self.image,(width // 8,height // 7))
self.rect = self.image.get_rect()
self.rect.y = y #sets initial spawn point to x and y which are variables
self.rect.x = x # set to the middle of the screen earlier in the program
def update(self):
# Update the player's position. #
# Set the player's x,y coordinates to that of movex, movey
self.rect.x = movex #movex and movey are a means for changing x and y via player input
self.rect.y = movey
....调用播放器类.....
player = Player() #creates the player sprite that gets manipulated via the player class
player_list.add(player) #adds the new player to both lists to aid in tracking and updating
all_sprites_list.add(player)
......的意思,而游戏功能里面。 ...
all_sprites_list.update() #forces all sprites within the list to refer to their class's update function
all_sprites_list.draw(screen) #auto magically draws all the new sprites
pygame.display.flip() #actually renders the updated imagery for entire program.
这应该是任何有关的问题精灵。我当然可以提供进一步的信息,如果它被要求的话,我只是不想在这里发布大量的代码块,因为它可能会吓跑人们。 :P
编辑:x和y是初始设置为全局变量是高度// 2和宽度// 2的高度和宽度被用于分辨率
movey和的Movex被全局设置为0,但当游戏在游戏功能循环中运行时更新。 示例代码movey,的Movex:
if event.type == KEYDOWN: #ship movement
if event.key == K_LEFT:
movex=-6
if event.key == K_RIGHT:
movex=+6
if event.key ==K_UP:
movey=-6
if event.key ==K_DOWN:
movey=+6
if event.key == K_a:
movex=-6
if event.key == K_d:
movex=+6
if event.key ==K_w:
movey=-6
if event.key ==K_s:
movey=+6
x += movex
y +=movey
UPDATE !!!!!!
self.rect.x = movex #movex and movey are a means for changing x and y via player input
self.rect.y = movey
这一行简单地需要的是:
self.rect.x += movex #movex and movey are a means for changing x and y via player input
self.rect.y += movey
在添加加的等号之前通过加入movey,的Movex代替改变self.rect(X,Y)的值。重复地分配给他们的价值。
哪里'x','y','movex'和'movey'来自?这些肯定是有关的。 –
精灵的左上角在0,0处产生,看起来应该是默认位置。那么问题就变成了,为什么它会忽略我的self.rect.y/x语句并在那里产生,然后拒绝更新的位置? – oxrock