我写的这个小游戏运行时,当木板在最右方接球时接不住,有人知道是什么错误吗?
import pygame
import random
import time
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("接弹珠游戏")
score = 0
font = pygame.font.Font(None, 60)
# 加载图片
bg_img = pygame.image.load("venv/bg.png")
ball = pygame.image.load("venv/1.png")
board = pygame.image.load("venv/2.png")
# 设置背景图初始的位置
bg_img_x = 0
bg_img_y = 0
ball_x = random.randint(20, 580) # 球的初始位置随机
ball_y = random.randint(20, 300)
board_x = 300
board_y = 490
move_x = 1
move_y = 1
step_x = 0
step_y = 0
points = 1
count = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.exit()
if event.type == pygame.KEYDOWN:
# 获取键盘按下的键
if event.key == pygame.K_LEFT:
step_x = -6
if event.key == pygame.K_RIGHT:
step_x = 6
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
step_x = 0
if event.key == pygame.K_RIGHT:
step_x = 0
board_x += step_x
board_y += step_y
ball_x += move_x
ball_y += move_y
screen.blit(bg_img, (bg_img_x, bg_img_y))
screen.blit(ball, (ball_x, ball_y))
screen.blit(board, (board_x, board_y))
my_score = font.render(str(score), False, (255, 255, 0)) # 设置分数显示,黄色
screen.blit(my_score, (500, 30))
ball_x += move_x
ball_y += move_y
if ball_x <= 0 or ball_x >= 560: # 左右两侧墙壁
move_x = -move_x # 碰到左右两侧墙壁时,X坐标变为反方向
if ball_y <= 0: # 碰到上方墙壁时,Y坐标改变方向
move_y = -move_y
if board_x <= 0:
board_x = 0
if board_x >= 500:
board_x = 500
elif (board_x - 20) < ball_x < (board_x + 120) and ball_y >= 450:
move_y = -move_y # 下方接到球,改变Y坐标方向,并加分
score += points
count += 1 # 接球次数加1
if count == 3:
count = 0
points += points # 分数翻倍
if move_x > 0:
move_x += 0.5
else:
move_x -= 0.5
move_y -= 0.5
elif ball_y > 460 and (ball_x <= board_x - 20 or ball_x >= board_x + 120):
# 没有接到球,退出程序
# break
pass
pygame.display.update() # 更新窗口,保证窗口始终打开
time.sleep(0.01) # 球运动的速度0.01秒走一个像素
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。