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

尼古拉斯·寒

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

import pygame
import random

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 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.color, (self.x, self.y), 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, -2, -1, 1, 2, 3])
        color = random.choice(colors)
        radius = random.randrange(5, MAX_RADIUS)
        ball = Ball(x, y, x_step, y_step, color, radius, screen)
        bounce_location = Ball(x, y, 0, 0, color, radius, 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()
拉比德76

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

我建议使用工具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方法Ball``。每次球反弹时,Location都会附加一个新对象:

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(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.color, self.radius, self.screen))

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

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

完整的例子:

import pygame
import random

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

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

MAX_RADIUS = 30
NUMBER_OF_BALLS = 1

class Location:
    def __init__(self, x, y, color, radius, screen):
        self.x = x
        self.y = y
        self.color = color
        self.radius = radius
        self.screen = screen
    def draw(self):
        pygame.draw.circle(self.screen, self.color, (self.x, self.y), self.radius, 2)

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(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.color, self.radius, self.screen))

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

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

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, -2, -1, 1, 2, 3])
        color = random.choice(colors)
        radius = random.randrange(5, MAX_RADIUS)
        moving_ball = Ball(x, y, x_step, y_step, color, radius, screen)
        balls.append(moving_ball)

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

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


if __name__ == "__main__":
    main()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

如何通过引用从另一个变量中删除字典中的对象

来自分类Dev

如何通过PHP中的另一个变量引用对象?

来自分类Dev

ReactJS如何使用另一个变量引用对象

来自分类Dev

如何从另一个类访问对象变量?

来自分类Dev

创建一个引用另一个对象的参数的变量?

来自分类Dev

另一个对象内部的引用对象实例

来自分类Dev

如何使用cfc对象的引用从另一个函数调用一个函数?

来自分类Dev

如何在SPARQL中查找引用另一个对象的对象

来自分类Dev

如何更改引用另一个对象属性的对象属性的值?

来自分类Dev

如何防止删除Realm中另一个对象引用的对象?

来自分类Dev

如何订阅rxjs中另一个对象中引用的对象的属性更改

来自分类Dev

来自另一个类中对象的引用变量

来自分类Dev

你如何创建一个使用另一个类的变量的java对象?

来自分类Dev

如何更改保存另一个模型的对象的Rails模型对象变量名称

来自分类Dev

如何将引用从一个对象内部传递到另一个对象的构造函数

来自分类Dev

创建对另一个对象的引用

来自分类Dev

在JavaScript中删除对另一个对象的引用

来自分类Dev

自引用对象(字段是另一个的结果)

来自分类Dev

在JavaScript中删除对另一个对象的引用

来自分类Dev

基类是对另一个对象的引用

来自分类Dev

Realm Swift - 保存对另一个对象的引用

来自分类Dev

如何通过GetComponent从另一个游戏对象中的另一个脚本访问变量?

来自分类Dev

如何从jQuery中的另一个对象访问在一个对象中声明的变量

来自分类Dev

如何使用另一个变量访问对象的变量/方法

来自分类Dev

递增Integer变量不会影响另一个引用同一对象的变量

来自分类Dev

如果我有一个对象是另一个对象的属性,如何从该属性对象引用主对象?

来自分类Dev

从另一个对象观察@Published变量

来自分类Dev

从另一个函数访问对象中的变量

Related 相关文章

  1. 1

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

  2. 2

    如何通过引用从另一个变量中删除字典中的对象

  3. 3

    如何通过PHP中的另一个变量引用对象?

  4. 4

    ReactJS如何使用另一个变量引用对象

  5. 5

    如何从另一个类访问对象变量?

  6. 6

    创建一个引用另一个对象的参数的变量?

  7. 7

    另一个对象内部的引用对象实例

  8. 8

    如何使用cfc对象的引用从另一个函数调用一个函数?

  9. 9

    如何在SPARQL中查找引用另一个对象的对象

  10. 10

    如何更改引用另一个对象属性的对象属性的值?

  11. 11

    如何防止删除Realm中另一个对象引用的对象?

  12. 12

    如何订阅rxjs中另一个对象中引用的对象的属性更改

  13. 13

    来自另一个类中对象的引用变量

  14. 14

    你如何创建一个使用另一个类的变量的java对象?

  15. 15

    如何更改保存另一个模型的对象的Rails模型对象变量名称

  16. 16

    如何将引用从一个对象内部传递到另一个对象的构造函数

  17. 17

    创建对另一个对象的引用

  18. 18

    在JavaScript中删除对另一个对象的引用

  19. 19

    自引用对象(字段是另一个的结果)

  20. 20

    在JavaScript中删除对另一个对象的引用

  21. 21

    基类是对另一个对象的引用

  22. 22

    Realm Swift - 保存对另一个对象的引用

  23. 23

    如何通过GetComponent从另一个游戏对象中的另一个脚本访问变量?

  24. 24

    如何从jQuery中的另一个对象访问在一个对象中声明的变量

  25. 25

    如何使用另一个变量访问对象的变量/方法

  26. 26

    递增Integer变量不会影响另一个引用同一对象的变量

  27. 27

    如果我有一个对象是另一个对象的属性,如何从该属性对象引用主对象?

  28. 28

    从另一个对象观察@Published变量

  29. 29

    从另一个函数访问对象中的变量

热门标签

归档