乒乓球游戏故障

2024-06-28 19:59:29 发布

您现在位置:Python中文网/ 问答频道 /正文

很多时候,球会在球拍的拐角处出现故障,只是故障通过,给球员一分,而不是打到拐角处,以不同的方式反弹。有人能帮我解释一下这个游戏的逻辑吗。非常感谢,谢谢!你知道吗

x1 = 20
y1 = 175
xsize = 35
ysize = 150
speed1 = 0

x2 = 740
y2 = 175
speed2 = 0

ballx = 550
bally = 250
speedx = 5
speedy = 5

score1 = 0
score2 = 0

bg = pygame.image.load("pongbg2.png")


while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                speed1 = -10
            if event.key == pygame.K_s:
                speed1 = 10
            if event.key == pygame.K_UP:
                speed2 = -10
            if event.key == pygame.K_DOWN:
                speed2 = 10

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                speed1 = 0
            if event.key == pygame.K_s:
                speed1 = 0
            if event.key == pygame.K_UP:
                speed2 = 0
            if event.key ==  pygame.K_DOWN:
                speed2 = 0


    screen.blit(bg, (0, 0))
    player1(x1, y1, xsize, ysize)
    player2(x2, y2, xsize, ysize)
    ball(ballx,bally)
    Score1(score1)
    Score2(score2)

    y1 += speed1
    y2 += speed2
    ballx += speedx
    bally += speedy

    if y1 < 0:
        y1 = 0

    if y1 > 350:
        y1 = 350

    if y2 < 0:
        y2 = 0

    if y2 > 350:
        y2 = 350

    if ballx+20 > x2 and bally-20 > y2 and bally+20 < y2+ysize and ballx < x2+3:
        speedx = -speedx

    if ballx-20 < x1+35 and bally-20 > y1 and bally+20 < y1+ysize and ballx > x1+38:
        speedx = -speedx

    if bally > 477 or bally < 23:
        speedy = -speedy

    if ballx < 13:
        score2 += 1
        ballx = 350
        bally = 250

    if ballx > 750:
        score1 += 1
        ballx = 350
        bally = 250

Tags: andkeyeventifpygamex1x2y1