gpt4 book ai didi

python - 如何在Pygame中以小角度低速并闪烁的方式绘制移动圆圈?

转载 作者:行者123 更新时间:2023-12-04 10:07:08 24 4
gpt4 key购买 nike

下面的代码在屏幕上移动两个球。第一个以 10 度的角度低速移动,具有良好的绘图质量。第二个球以 1 度的角度移动,为了尊重该角度,速度必须高得多,并且眨眼很多,绘图不令人满意。有没有办法减慢第二个球的绘制速度并避免过度闪烁?

import pygame, sys, math
from pygame.locals import *

pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('Bouncing Ball with position and angle')

# Set our color constants
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)

# Create the ball class
class Ball():
def __init__(self,
screen,
color,
radius,
startX,
startY,
speed,
angle=45):
super().__init__()
self.screen = screen
self.color = color
rectSize = radius * 2
self.rect = pygame.Rect(startX, startY, rectSize, rectSize)
self.speed = speed
self.angle = math.radians(angle)

def update(self):
delta_x = self.speed * math.cos(self.angle)
delta_y = self.speed * math.sin(self.angle)
self.rect = self.rect.move(delta_x, delta_y)

if self.rect.right >= self.screen.get_width() or self.rect.left <= 0:
self.angle = math.pi - self.angle

if self.rect.top <= 0 or self.rect.bottom >= self.screen.get_height():
self.angle = -self.angle

def draw(self):
'''
Draw our ball to the screen with position information.
'''
pygame.draw.circle(self.screen, self.color, self.rect.center, int(self.rect.width / 2))

# Create a new Ball instance named 'myball'
myball = Ball(screen=DISPLAYSURF, color=YELLOW, startX=100, startY=100, radius=150, speed=8, angle=10)
mySmaLlAngleball = Ball(screen=DISPLAYSURF, color=YELLOW, startX=100, startY=500, radius=150, speed=58, angle=-1)

run = True
clock = pygame.time.Clock()

# Display loop
while run:
# Handle events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

# Update ball position
myball.update()
mySmaLlAngleball.update()

# Draw screen
DISPLAYSURF.fill(BLACK)
myball.draw()
mySmaLlAngleball.draw()
pygame.display.update()
clock.tick(30)

最佳答案

问题是造成的,因为 pygame.Rect 存储积分坐标:

The coordinates for Rect objects are all integers. [...]

delta_x的分数分量和 delta_y丢失于 self.rect.move(delta_x, delta_y) .
您必须使用浮点数进行计算。添加属性 self.pos ,这是一个包含 2 个组件的 tupe,用于存储球的中心点:
self.pos = self.rect.center
计算具有最大浮点精度的位置:
delta_x = self.speed * math.cos(self.angle)
delta_y = self.speed * math.sin(self.angle)
self.pos = (self.pos[0] + delta_x, self.pos[1] + delta_y)
更新 self.rect.center round() ed位置。
self.rect.center = round(self.pos[0]), round(self.pos[1])
self.pos是“内部”位置,负责位置的精确计算。 self.rect.center是积分位置,负责拉球。 self.pos每帧略有变化。 self.rect.center只有当坐标的 a 分量改变了 1 时才会改变。
类(class) Ball :
class Ball():
def __init__(self,
screen,
color,
radius,
startX,
startY,
speed,
angle=45):
super().__init__()
self.screen = screen
self.color = color
rectSize = radius * 2
self.rect = pygame.Rect(startX, startY, rectSize, rectSize)
self.speed = speed
self.angle = math.radians(angle)
self.pos = self.rect.center

def update(self):
delta_x = self.speed * math.cos(self.angle)
delta_y = self.speed * math.sin(self.angle)
self.pos = (self.pos[0] + delta_x, self.pos[1] + delta_y)

self.rect.center = round(self.pos[0]), round(self.pos[1])

if self.rect.right >= self.screen.get_width() or self.rect.left <= 0:
self.angle = math.pi - self.angle

if self.rect.top <= 0 or self.rect.bottom >= self.screen.get_height():
self.angle = -self.angle

def draw(self):
'''
Draw our ball to the screen with position information.
'''
pygame.draw.circle(self.screen, self.color, self.rect.center, int(self.rect.width / 2))

使用此解决方案,您可以按比例增加每秒的触发器 ( clock.tick() ) 并按相同比例降低速度。这会导致平滑移动而不会眨眼。

关于python - 如何在Pygame中以小角度低速并闪烁的方式绘制移动圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61528967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com