The code below moves two balls on the screen. The first one is moved with an angle of 10 degree at a low speed with a good drawing quality. The second ball is moved with an angle of 1 degree and in order for the angle to be respected, the speed must be much higher and the drawing is unsatisfactory with a lot of blinking. Is there a way to slow down the drawing of the second ball and avoid the excessive blinking?
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)
The issue is caused, because pygame.Rect
stores integral coordinates:
The coordinates for Rect objects are all integers. [...]
The fraction component of delta_x
and delta_y
is lost at self.rect.move(delta_x, delta_y)
.
You have to use floating point numbers for the computation. Add an attribute self.pos
, which is a tupe with 2 components an stores the center point of a ball:
self.pos = self.rect.center
Compute the position with the maximum floating point accuracy:
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)
Update the self.rect.center
by the round()
ed position.
self.rect.center = round(self.pos[0]), round(self.pos[1])
self.pos
is the "internal" position and responsible for the exact computation of the position. self.rect.center
is the integral position and responsible to draw the ball. self.pos
slightly changes in each frame. self.rect.center
changes only if the a component of the coordinate has changed by 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))
With this solution you can scale up the flops per second ( clock.tick()
) and scale down the speed by the same scale. This leads to a smooth movement without blinking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.