微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从另一个对象的对象引用变量?

如何解决如何从另一个对象的对象引用变量?

我用pygame编写了一个程序,其中有一个球在屏幕上弹跳,我想绘制下一个球要撞到墙壁的位置。我想引用球的x_step和y_step并使用其移动弹跳位置。我也不想看到跳出位置移动。我只想看看它的结局。如果弹跳位置和球的x和y坐标都相同,我想将弹跳位置移动到新位置。我在做所有这些事情时遇到麻烦。

import pygame
import random

WHITE = (255,255,255)
BLACK = (0,0)
RED = (255,0)
GREEN = (0,0)
BLUE = (0,255)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

MAX_RADIUS = 30

NUMBER_OF_BALLS = 1


class Ball:
    def __init__(self,x,y,x_step,y_step,color,radius,screen):
        self.x = x
        self.y = y
        self.x_step = x_step
        self.y_step = y_step
        self.color = color
        self.radius = radius
        self.screen = screen

    def move_ball(self):
        self.x += self.x_step
        self.y += self.y_step

        if not self.radius <= self.x <= SCREEN_WIDTH - self.radius:
           self.x_step *= -1

        if not self.radius <= self.y <= SCREEN_HEIGHT - self.radius:
            self.y_step *= -1
    
    def move_location(self):
        self.x += self.x_step
        self.y += self.y_step

        if not self.radius <= self.x <= SCREEN_WIDTH - self.radius:
            self.x_step = 0
            self.y_step = 0
            
        if not self.radius <= self.y <= SCREEN_HEIGHT - self.radius:
            self.y_step = 0
            self.x_step = 0

    def draw_ball(self):
        pygame.draw.circle(self.screen,self.color,(self.x,self.y),self.radius)

    def draw_location(self):
        pygame.draw.circle(self.screen,self.radius,2)


def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

    colors = [WHITE,RED,GREEN,BLUE]
    balls = []
    bounce_locations = []
    for _ in range(NUMBER_OF_BALLS):
        x = random.randrange(MAX_RADIUS,SCREEN_WIDTH - MAX_RADIUS)
        y = random.randrange(MAX_RADIUS,SCREEN_HEIGHT - MAX_RADIUS)
        x_step = random.choice([-3,-2,-1,1,2,3])
        y_step = random.choice([-3,3])
        color = random.choice(colors)
        radius = random.randrange(5,MAX_RADIUS)
        ball = Ball(x,screen)
        bounce_location = Ball(x,screen)
        balls.append(moving_ball)
        bounce_locations.append(bounce_location)

    running = True
    clock = pygame.time.Clock()
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        screen.fill(BLACK)
        for ball in balls:
            ball.move_ball()
            ball.draw_ball()

        for location in bounce_locations:
            location.move_location()
            if not radius <= x <= SCREEN_WIDTH - radius:
                location.draw_location()
            if not radius <= y <= SCREEN_HEIGHT - radius:
                location.draw_location()

        pygame.display.update()
        clock.tick(60)


if __name__ == "__main__":
    main()

解决方法

如果要访问对象的属性,则需要引用该对象的变量。

我建议使用实现2个单独的类BallLocation。位置类不需要步骤属性,也不需要move方法。

class Location:
    def __init__(self,x,y,x_step,y_step,color,radius,screen):
        self.x = x
        self.y = y
        self.x_step = x_step
        self.y_step = y_step
        self.color = color
        self.radius = radius
        self.screen = screen
    def move(self):
        self.x += self.x_step
        self.y += self.y_step

        if not self.radius <= self.x <= SCREEN_WIDTH - self.radius:
            self.x_step = 0
            self.y_step = 0
            
        if not self.radius <= self.y <= SCREEN_HEIGHT - self.radius:
            self.y_step = 0
            self.x_step = 0
    def draw(self):
        pygame.draw.circle(self.screen,self.color,(self.x,self.y),self.radius,2)

move的{​​{1}}方法获得一个新的参数``。每次球反弹时,都会附加一个新的Ball对象:

Location

完整示例:

class Ball:
    def __init__(self,screen):
        self.x = x
        self.y = y
        self.x_step = x_step
        self.y_step = y_step
        self.color = color
        self.radius = radius
        self.screen = screen
    def move(self,bounce_locations):
        self.x += self.x_step
        self.y += self.y_step

        if not self.radius <= self.x <= SCREEN_WIDTH - self.radius:
           self.x_step *= -1
           bounce_locations.append(Location(self.x,self.y,self.screen))

        if not self.radius <= self.y <= SCREEN_HEIGHT - self.radius:
            self.y_step *= -1
            bounce_locations.append(Location(self.x,self.screen))

    def draw(self):
        pygame.draw.circle(self.screen,self.radius)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。