我正在开发的游戏有问题。游戏的重点是用木板击球,以使球不会掉落。代码中有很多错误,但是我认为我应该先解决这个问题。基本上,一旦球从木板反弹,我就会增加速度增量。但是,一旦增量变得过高,您将看到球“跳跃”,而不是在屏幕上平稳移动。我尝试增加FPS并减小速度增量,但是速度太慢。下面是我的代码:
import pygame
pygame.init()
win = pygame.display.set_mode((750,750))
pygame.display.set_caption('Ball Plank')
clock = pygame.time.Clock()
x = 305
y = 650
width = 150
height = 10
vel = 30
score = 0
lives = 3
radius = 35
ball_x = 377
ball_y = 297
ball_vely = 10
ball_velx = 10
font = pygame.font.SysFont('comicsans',40,True)
def drawing():
win.fill((0,0))
text_score = font.render('Score: ' + str(score),1,(255,255,255))
text_lives = font.render('Lives: ' + str(lives),255))
win.blit(text_score,(570,20))
win.blit(text_lives,(30,20))
circle = pygame.draw.circle(win,(0,0),(int(ball_x),int(ball_y)),radius)
rect = pygame.draw.rect(win,(x,y,width,height))
pygame.display.update()
run = True
while run:
clock.tick(40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 750 - width - vel:
x += vel
ball_y += ball_vely
ball_x += ball_velx
if ball_y - radius - 10 <= 0:
ball_vely *= -1
elif ball_y + radius + 8 > y and ball_y <= y and ball_x > x - radius and ball_x < x + width + radius:
ball_velx = 10
if ball_vely > 0:
ball_vely += 0.5
else:
ball_vely -= 0.5
ball_vely *= -1
score += 1
if ball_x > x + 75:
ball_velx *= 1 + (ball_x - x + 75)/150
elif ball_x < x + 75:
ball_velx *= -(1 + (ball_x - x + 75)/150)
if ball_x - radius - 10 <= 0 or ball_x + radius + 10 >= 750:
ball_velx *= -1
if ball_y > 760 + radius:
lives -= 1
if lives == 0:
run = False
drawing()
pygame.display.quit()
pygame.quit()
不确定到底出了什么问题,对不起,代码太多了。