首页 文章

精灵产生0,0并且不会更新位置

提问于
浏览
1

我遇到了一个非常烦人的问题,我希望我们中间有人可能对此有所了解,这可能有所帮助 .

问题在于,为表示玩家而创建的精灵卡在其生成位置 . 一个生成位置,它不是我告诉它生成的地方,不会接受在其他地方生成的坐标,它只会在那里产生恰好是屏幕的左上角 . 我让我的程序在运行时连续打印出它的位置以及更改它的坐标的命令,并且它正在接收更新的信息,精灵就不会移动 . 它的行为方式与我碰到一个难以穿透的墙壁的方式相同 .

relavent python / 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

在等号之前加上加号会改变self.rect . (x,y)值,通过添加movey,movex而不是重复地将它们分配给它们的值 .

1 回答

  • 0

    你需要传入你正在使用的变量(x和y):对movex做同样的事情,movey

    def __init__(self,x,y):
            #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
    

相关问题