与程序相比,OOP 代码的逻辑之间的未知差异

问题描述

我编写了同一个弹跳球游戏的两个版本。一种是基于 OOP 的,一种是程序性的,我希望他们做同样的事情。除了面向对象程序的行为不同。

我不知道解释它的最佳方法,但程序代码“弹跳”使球无限期地弹跳并每次弹跳到相同的高度。但是 OOP 代码“弹跳”会增加每次连续弹跳时的弹跳高度。但我找不到它们之间的逻辑差异。

面向对象代码

import pygame,time
        
class Ball(pygame.sprite.Sprite):
    def __init__(self,colour,radius):
        super().__init__()
        self.image = pygame.Surface([radius*2,radius*2])
        self.colour = colour
        self.radius = radius
        pygame.draw.circle(self.image,self.colour,(radius,radius),self.radius)
        self.rect = self.image.get_rect()
        self.rect.x = 350
        self.rect.y = 350
        self.change_y = 0.5
        self.vel_y = 0
    def update(self):
        self.vel_y += self.change_y
        self.rect.y += self.vel_y
    def bounce(self):
        self.vel_y = self.vel_y * -1
        self.rect.y += self.vel_y 

def play_game():
    all_sprites_list = pygame.sprite.Group()
    ball_list = pygame.sprite.Group()
    ball = Ball(WHITE,10)
    ball_list.add(ball)
    all_sprites_list.add(ball)

    done = False
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        screen.fill(BLACK)
        all_sprites_list.draw(screen)
        for ball in ball_list:
            if ball.rect.y > 690:
                ball.bounce()
            else:
                ball.update()
        pygame.display.update()
        clock.tick(60)

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

pygame.init()
screen_width = 700
screen_height = 700
screen = pygame.display.set_mode([screen_width,screen_height])
clock = pygame.time.Clock()

play_game()
pygame.quit() 

程序代码

import pygame
BLACK = (0,255)
GREEN = (0,0)
RED = (255,0)
broWN = (200,100,0)
 
pygame.init()
size = (700,700)
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()

rect_x = 350
rect_y = 350
rect_changey = 0

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(BLACK)  
    pygame.draw.circle(screen,WHITE,[rect_x,rect_y],10)
    if (rect_y > 690):
        rect_changey = rect_changey* -1
        rect_y += rect_changey
    else:
        rect_changey = rect_changey + 0.5
        rect_y += rect_changey
    pygame.display.flip()
    clock.tick(60)
 
pygame.quit()

更新:ball.update() 函数比过程代码中的代码的等效部分多运行 1 次。还是不知道为什么

解决方法

rect_xrect_y 可以存储浮点值。但是,rect.xrect.y 不能只存储整数值。

由于 pygame.Rect 应该代表屏幕上的一个区域,因此 pygame.Rect 对象只能存储整数数据。

Rect 对象的坐标都是整数。 [...]

当对象的新位置被分配给 Rect 对象时,坐标的小数部分会丢失。如果每一帧都这样做,位置误差会随着时间累积。

如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量和属性中,并同步 pygame.Rect 对象。 round 坐标并将其分配给矩形的位置(例如 .topleft):

x,y = # floating point coordinates
rect.topleft = round(x),round(y)

Ball 类:

class Ball(pygame.sprite.Sprite):
    def __init__(self,colour,radius):
        super().__init__()
        self.image = pygame.Surface([radius*2,radius*2])
        self.colour = colour
        self.radius = radius
        pygame.draw.circle(self.image,self.colour,(radius,radius),self.radius)
        self.rect = self.image.get_rect()
        self.x = 350
        self.y = 350
        self.rect.x = self.x
        self.rect.y = self.y
        self.change_y = 0.5
        self.vel_y = 0
    def update(self):
        self.vel_y += self.change_y
        self.y += self.vel_y
        self.rect.topleft = round(self.x),round(self.y)
    def bounce(self):
        self.vel_y = self.vel_y * -1
        self.y += self.vel_y 
        self.rect.topleft = round(self.x),round(self.y)