如何从 class in Python 中获取变量的值?

How to get value of variable from inside of class in Python?

所以,我在pythonpygame中写了一些简单的代码,只是为了学习。我遇到了问题,当我想检查球的 collisions 是否与托盘的 y 位置相同时。主要问题是我的托盘变量的值由用户更新,它位于 class 托盘内,在函数 move 中。我怎样才能把它拿出来放在 class 外面,放入功能固定球中?

import pygame
from pygame.constants import DROPTEXT
pygame.init()
#window
w_width = 1000
w_height = 600
screen = pygame.display.set_mode((w_width, w_height))
clock = pygame.time.Clock()
open = True
#player
x,y = 100,250
#define player action variables
speed = 5
speed_x,speed_y = 5,4
moving_down = False
moving_up = False
#define ball action variables
x_ball,y_ball = 500,250
radius = 30
class palette(pygame.sprite.Sprite):
    global x,y

    def __init__(self, x, y, speed):
        self.speed = speed
        pygame.sprite.Sprite.__init__(self)
        self.player_rect = pygame.Rect(x,y,30,100)

    def draw(self):
        pygame.draw.rect(screen, 'White', self.player_rect)

    def move(self, moving_up, moving_down):
        #reset movement variables
        global dy
        dy = 0

        #assing movement variables if moving up or down
        if moving_up:
            dy = -self.speed
        if moving_down:
            dy = self.speed

        #update pallete possition
        self.player_rect.y += dy
        
def ball():
    global speed_x,speed_y,x_ball,y_ball
    #update pos of bal
    x_ball += speed_x
    y_ball += speed_y
    #basic colision with screen
    if x_ball>=w_width-(0.5*radius) or x_ball <=0+(0.5*radius):
        speed_x*=-1
    if y_ball>=w_height-(0.5*radius) or y_ball <=0+(0.5*radius):
        speed_y*=-1
    #collision with pallettes

    pygame.draw.circle(screen, (255,255,255), (x_ball,y_ball), radius, 5)

#build players and enemies
player = palette(x,y, speed)
enemy = palette(x*9,y, speed)
#game
while open:
    for event in pygame.event.get():
        #quit game
        if event.type == pygame.QUIT:
            open = False
        #keyboard presses
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                moving_up = True
            if event.key == pygame.K_s:
                moving_down = True
            if event.key == pygame.K_ESCAPE:
                open = False
        #keyboard button released
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                moving_up = False
            if event.key == pygame.K_s:
                moving_down = False
    
    screen.fill((0,0,0))
    clock.tick(60)
    pygame.display.flip


    #keys


    #init players
    player.draw()
    enemy.draw()
    enemy.move(moving_up, moving_down)
    player.move(moving_up, moving_down)
    ball()

    pygame.display.update()

阅读有关 Classes and Instance Objects. The "variables" in a class are called "attributes". You can access the instance attributes through the instance object. In your case player and enemy are instances of the class palette. The palette class has a player_rect attribute which is a pygame.Rect 对象的信息。因此:

y = player.pylyer_rect.y

但是请阅读文档。 pygame.sprite.Group.draw() and pygame.sprite.Group.update()pygame.sprite.Group.

提供的方法

后者委托给包含的pygame.sprite.Sprites — you have to implement the method. See pygame.sprite.Group.update()update方法:

Calls the update() method on all Sprites in the Group. [...]

前者使用包含的 pygame.sprite.Spriteimagerect 属性来绘制对象 - 你必须确保 pygame.sprite.Sprite 具有所需的属性。见 pygame.sprite.Group.draw():

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

所以你应该将 player_rect 重命名为 rectClass Names 通常应使用 CapWords 约定。所以在 Palette 中重命名 palette:

class Palette(pygame.sprite.Sprite):
    def __init__(self, x, y, speed):
        self.speed = speed
        pygame.sprite.Sprite.__init__(self)
        self.rect = pygame.Rect(x,y,30,100)

    # [...]
player = Palette(x, y, speed)
enemy = Palette(x*9, y, speed)