Python 如何从一个对象为另一个对象引用变量?
我用pygame制作了一个程序,其中一个球在屏幕上反弹,我想画出下一个球撞到墙上的位置。我想参考球的x_步和y_步,并用它移动弹跳位置。我也不想看到反弹位置移动。我只是想看看它的结局。如果反弹位置和球的x和y坐标相同,我想将反弹位置移动到新位置。我做这一切都有困难Python 如何从一个对象为另一个对象引用变量?,python,object,variables,pygame,Python,Object,Variables,Pygame,我用pygame制作了一个程序,其中一个球在屏幕上反弹,我想画出下一个球撞到墙上的位置。我想参考球的x_步和y_步,并用它移动弹跳位置。我也不想看到反弹位置移动。我只是想看看它的结局。如果反弹位置和球的x和y坐标相同,我想将反弹位置移动到新位置。我做这一切都有困难 import pygame import random WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (
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()
导入pygame
随机输入
白色=(255,255,255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
屏幕宽度=800
屏幕高度=600
最大半径=30
球的数量=1
班级舞会:
定义初始(自、x、y、x、y步长、y步长、颜色、半径、屏幕):
self.x=x
self.y=y
self.x_步长=x_步长
self.y_步长=y_步长
self.color=颜色
自半径=半径
self.screen=屏幕
def移动球(自身):
self.x+=self.x_步
self.y+=self.y\u步
如果不是self.radius如果要访问对象的属性,则需要引用此对象的变量
我建议使用两个单独的类Ball
和Location
。location类不需要步骤属性和移动方法
类位置:
定义初始(自、x、y、x、y步长、y步长、颜色、半径、屏幕):
self.x=x
self.y=y
self.x_步长=x_步长
self.y_步长=y_步长
self.color=颜色
自半径=半径
self.screen=屏幕
def移动(自我):
self.x+=self.x_步
self.y+=self.y\u步
如果不是self.radius,为什么球会离开屏幕几个像素?@NicholasChill,因为球的速度大于1