如何从类外部不断更新变量?

2024-04-19 17:37:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python/pygame类的新手,我正在制作一个游戏。 所以我的问题是,我有一个类可以将太空船绘制到屏幕上,但是我需要在它们的x和y中添加spacemove_x,spacemove_y

这些spacemove_x,spacemove_y是在用户移动自己的飞船时定义的

(我使用spacemove_ux,spacemove_uy来更新其他东西的位置,比如星星,因为这个游戏是从上往下看的,所以当我移动我的航天器时,其他东西应该会变得更近/更远,这取决于我的角色要往哪个方向走)

所以spacemove_x,spacemove_y总是自己更新它,但是我怎样才能把这个信息给类呢?你知道吗

代码:

import pygame
import random

class BaseClass(pygame.sprite.Sprite):

 allsprites = pygame.sprite.Group()
 def __init__(self, x, y, width, height,spacemove_x,spacemove_y):
    pygame.sprite.Sprite.__init__(self)
    BaseClass.allsprites.add(self)

    self.shipDefaultUp = pygame.image.load("textures/ships/shipDefault/shipUP.png")
    self.shipDefaultRight = pygame.image.load("textures/ships/shipDefault/shipRIGHT.png")
    self.shipDefaultDown = pygame.image.load("textures/ships/shipDefault/shipDOWN.png")
    self.shipDefaultLeft = pygame.image.load("textures/ships/shipDefault/shipLEFT.png")

    self.shipCargo1Up = pygame.image.load("textures/ships/shipCargo1/shipCargo1UP.png")
    self.shipCargo1Right = pygame.image.load("textures/ships/shipCargo1/shipCargo1RIGHT.png")
    self.shipCargo1Down = pygame.image.load("textures/ships/shipCargo1/shipCargo1DOWN.png")
    self.shipCargo1Left = pygame.image.load("textures/ships/shipCargo1/shipCargo1LEFT.png")

    self.shipCargo2Up = pygame.image.load("textures/ships/shipCargo2/shipCargo2UP.png")
    self.shipCargo2Right = pygame.image.load("textures/ships/shipCargo2/shipCargo2RIGHT.png")
    self.shipCargo2Down = pygame.image.load("textures/ships/shipCargo2/shipCargo2DOWN.png")
    self.shipCargo2Left = pygame.image.load("textures/ships/shipCargo2/shipCargo2LEFT.png") 
    self.image = pygame.image.load("textures/ships/shipDefault/shipUP.png")
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
    self.width = width
    self.height = height

    self.spacemove_x = spacemove_x
    self.spacemove_y = spacemove_y

    #self.dirrection = random.randrange(1,5)
    self.dirrection = 1
    self.timer = random.randrange(10,50)
    self.speed = random.randrange(2,10)

    self.shiptype = 3#random.randrange(1,3)

    #shiptypes#
    #1 = shipDefault
    #2 = shipCargo1
    #3 = shipCargo2

    self.move1 = ((1),(2),(4))
    self.move2 = ((1),(2),(3))
    self.move3 = ((2),(3),(4))
    self.move4 = ((1),(3),(4))

class spacecraftBOT(BaseClass):
    ships = pygame.sprite.Group()

    def __init__(self, x, y, width, height,spacemove_x,spacemove_y):
        BaseClass.__init__(self, x, y, width, height,spacemove_x,spacemove_y)
        spacecraftBOT.ships.add(self)

    def motion(self):

        #1 = UP
        #2 = RIGHT
        #3 = DOWN
        #4 = LEFT

        self.timer -=1

        if self.dirrection == 1: ##############SHIP UP
            self.rect.y -=self.speed
            self.rect.y -=self.spacemove_x

            if self.shiptype == 1:
                self.image = self.shipDefaultUp

            if self.shiptype == 2:
                self.image = self.shipCargo1Up

        if self.dirrection == 2:################SHIP RIGHT
            self.rect.x +=self.speed
            self.rect.x +=self.spacemove_x
            if self.shiptype == 1:
                self.image = self.shipDefaultRight

            if self.shiptype == 2:
                self.image = self.shipCargo1Right

            if self.shiptype == 3:
                self.image = self.shipCargo2Right

        if self.dirrection == 3: ##############SHIP DOWN
            self.rect.y +=self.speed
            self.rect.y +=self.spacemove_y

            if self.shiptype == 1:
                self.image = self.shipDefaultDown

            if self.shiptype == 2:
                self.image = self.shipCargo1Down

            if self.shiptype == 3:
                self.image = self.shipCargo2Down

        if self.dirrection == 4: ################SHIP LEFT
            self.rect.x -=self.speed
            self.rect.x -=self.spacemove_x

            if self.shiptype == 1:
                self.image = self.shipDefaultLeft

            if self.shiptype == 2:
                self.image = self.shipCargo1Left

            if self.shiptype == 3:
                self.image = self.shipCargo2Left


        if self.dirrection == 5:
            print("loitter")

        if self.timer < 0:
            self.dirrection = random.randrange(1,6)
            self.timer = random.randrange(10,50)

这就是我在游戏中创建航天器的方式:

spacecraft1 = spacecraftBOT(500,500,100,34,spacemove_x,spacemove_y)

那么我如何给这个类更新spacemove_x,spacemove_y?你知道吗


Tags: rectimageselfifpngloadrandompygame
2条回答

So how can i give this class updated spacemove_x,spacemove_y?

只要分配它们就行了:

spacecraft1.spacemove_x = 100
spacecraft1.spacemove_y = 200

顺便说一句,spacecraft1是类spacecraftBOT的实例,而不是类。你知道吗

您可以使用点运算符访问实例成员。事实上,您已经用self多次这样做了。名称self并不特殊;它只是一个引用类实例的变量。类似地,spacecraft1是一个引用类实例的变量。这意味着您可以使用完全相同的表示法:

spacecraft1.spacemove_x = 100

或者

spacecraft1.spacemove_x += 50

或者你想要的任何东西。你知道吗

或者,可以定义move()方法:

class spacecraftBOT(BaseClass):
    # ... all of the code you already have ...

    def move(self, x, y):
        self.spacemove_x = x
        self.spacemove_y = y

现在您可以使用以下命令调用该方法

spaceship1.move(100, 50)

请注意,这将直接跳到给定的位置。如果要增加x和y坐标,只需实现正确的逻辑即可。(提示:使用+=而不是=。)

相关问题 更多 >