我正在做一个游戏,我有一个问题,我的代码一个数学函数在球速有一些波动,因为Python不能计算100%的准确性。
在正常的代码中,这不是问题,但是因为我有一个条件来改变运动的类型,从上升到下降,它被设置为一些有趣的值。
如果我把这个数字改为300,它只会做一个周期的下降和上升,但如果我把它改为320,它会做一些。
问题是,球的y坐标是如此随机ChatGPT对我说,这是因为这种波动会积累,但最后一个球_y是如此随机,有时它是319下一个317,320,但如果它跳到322或更大的东西,然后320它会停止
我知道最简单的方法是使用一些范围,但它不起作用,因为它总是会跳到一些不好的值。你能告诉我如何用我的代码解决这个问题吗?
import pygame as pg
import random
pg.init()
# Constants
BACKGROUND_COLOR = pg.Color('black')
BALL_X = 240
# Initialize the game window
screen = pg.display.set_mode((500, 800))
clock = pg.time.Clock()
# Load the background image
background_image = pg.image.load("bg.png")
# Draw a rhombus shape
def draw_rhombus(screen, color, x, space):
point1 = (170 + x, 740 - space)
point2 = (350 + x, 740 - space)
point3 = (300 + x, 790 - space)
point4 = (120 + x, 790 - space)
points = [point1, point2, point3, point4]
return pg.draw.polygon(screen, color, points)
# Generate a list of unique colors
def generate_color_list():
colors = ["RED", "ORANGE", "BLUE", "WHITE"]
available_colors = colors.copy()
color_list = []
while len(color_list) < 43:
if not available_colors:
available_colors = colors.copy()
color = random.choice(available_colors)
if color_list and color == color_list[-1]:
continue
color_list.append(color)
available_colors.remove(color)
return color_list
# Draw a stack of rhombus shapes
def draw_rhombus_stack(color_list, space, x):
positions = []
for i in range(43):
x_position = x if i == 42 else 0
position = draw_rhombus(screen, color_list[i], x_position, space + i * 8)
positions.append(position)
return positions
# Move the rhombus stack left or right
def move_rhombus_stack(x, keys):
x_coor = x # Define x_coor variable
if keys[pg.K_LEFT] and x_coor > -500:
x_coor -= 5
if keys[pg.K_RIGHT] and x_coor < 500:
x_coor += 5
return x_coor
# Functions for handling the ball
def draw_ball(color, x, y):
pg.draw.circle(screen, color, (x, y), 15)
def move_ball_down(y, speed, acceleration, direction):
if direction == "down":
speed += acceleration
y += int(speed)
if y >= 415:
direction = "up"
return y, speed, direction
def move_ball_up(y, speed, acceleration, direction):
if direction == "up":
speed -= acceleration
if speed <= 2:
speed = 0
y -= int(speed)
if y <= 320:
direction = "down"
return y, speed, direction
def check_collision(space, color_list, ball_color, index):
if 0 < space < 500 and color_list[ball_color] != color_list[index]:
return True
return False
def choose_ball_color(color_list):
return random.randint(0, 4)
# Main game loop
done = False
color_list = generate_color_list()
ball_color_index = choose_ball_color(color_list)
ball_x = BALL_X
ball_y = 300
ball_speed = 1
ball_acceleration = 0.1
ball_direction = "down"
space = 0
x_coor =0
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
keys = pg.key.get_pressed()
x_coor = move_rhombus_stack(x_coor, keys)
screen.fill(BACKGROUND_COLOR)
# Draw rhombus stack and store their positions
rhombus_positions = draw_rhombus_stack(color_list, space, x_coor)
# Check for collisions
end = False
for i, position in enumerate(rhombus_positions):
end = check_collision(space, color_list, ball_color_index, i)
if end:
break
draw_ball(color_list[ball_color_index], ball_x, ball_y)
# Ball movement
ball_y, ball_speed, ball_direction = move_ball_down(ball_y, ball_speed, ball_acceleration, ball_direction)
ball_y, ball_speed, ball_direction = move_ball_up(ball_y, ball_speed, ball_acceleration, ball_direction)
# Check for game over condition
if end:
# Implement game over logic here
pass
pg.display.flip()
clock.tick(30)
pg.quit()
1条答案
按热度按时间t98cgbkg1#
我建议在球改变方向并开始下落时改变条件。球在某一高度不改变方向,但当速度为0时。
如果每次不将球的位置转换为整数值(
y -= int(speed)
),而是保留浮点值(y -= speed
),您也会获得更平滑的移动。round
代替球在屏幕上绘制时的位置:此外,如果你以0而不是1(
ball_speed = 0
)的速度开始,球将总是从300的高度开始下落。