【问题标题】:Pygame - collision between two objects not workingPygame - 两个对象之间的碰撞不起作用
【发布时间】:2020-08-11 09:21:18
【问题描述】:

我正在尝试重新创建“Pong”,但我正在努力找出碰撞出了什么问题。代码如下:

ball_x += ball_x_change
    collide = player_collision(player_x, ball_x, player_y, ball_y)
    if collide:
        ball_x -= ball_x_change
    ball(ball_x, ball_y)

这意味着将球从其起始位置移向球员,并检查球员图标和球是否发生碰撞。如果有,它将改变球的方向。然后它在屏幕上渲染球。 以下是全部内容:

import pygame
import math

pygame.init()

screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Pong")

icon = pygame.image.load("ping-pong.png")
pygame.display.set_icon(icon)

player_icon = pygame.image.load("vertical-line.png")
player_x = 800 - (64 + 32)
player_y = 236
player_y_change = 0

cpu_icon = pygame.image.load("vertical-line.png")
cpu_x = 0 - 32
cpu_y = 236
cpu_y_change = 0

ball_icon = pygame.image.load("oval.png")
ball_x = 384
ball_y = 284
ball_x_change = 0.5
ball_y_change = 0

def player(player_x, player_y):
    screen.blit(player_icon, (player_x, player_y))

def cpu(cpu_x, cpu_y):
    screen.blit(cpu_icon, (cpu_x, cpu_y))

def ball(ball_x, ball_y):
    screen.blit(ball_icon,(ball_x, ball_y))

def player_collision(ball_x, ball_y, player_x, player_y):
    bump = math.hypot(player_x - ball_x, player_y - ball_y)
    if bump < 20:
        return True
    else:
        return False

running = True
while running:

    screen.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                player_y_change += 1
            if event.key == pygame.K_UP:
                player_y_change -= 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN or pygame.K_UP:
                player_y_change = 0

    player_y += player_y_change
    if player_y <= 0:
        player_y = 0
    elif player_y >= 472:
        player_y = 472
    player(player_x, player_y)

    cpu_y += cpu_y_change
    if cpu_y <= 0:
        player_y = 0
    elif player_y >= 472:
        player_y = 472
    cpu(cpu_x, cpu_y)

    ball_x += ball_x_change
    collide = player_collision(player_x, ball_x, player_y, ball_y)
    if collide:
        ball_x -= ball_x_change
    ball(ball_x, ball_y)

    pygame.display.update()

感谢您的宝贵时间。

更新了代码,还是不行:

import pygame
import math

pygame.init()

screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Pong")

icon = pygame.image.load("ping-pong.png")
pygame.display.set_icon(icon)

player_icon = pygame.image.load("vertical-line.png")
player_x = 672
player_y = 236
player_y_change = 0

cpu_icon = pygame.image.load("vertical-line.png")
cpu_x = 0
cpu_y = 236
cpu_y_change = 0

ball_icon = pygame.image.load("oval.png")
ball_x = 384
ball_y = 284
ball_x_change = 0.8
ball_y_change = 0

def player(player_x, player_y):
    screen.blit(player_icon, (player_x, player_y))

def cpu(cpu_x, cpu_y):
    screen.blit(cpu_icon, (cpu_x, cpu_y))

def ball(ball_x, ball_y):
    screen.blit(ball_icon,(ball_x, ball_y))

def player_collision(ball_x, player_x, ball_y, player_y):
    touch = math.hypot(player_x - ball_x, ball_y - player_y)
    if touch < 10:
        return True
    else:
        return False

running = True
while running:

    screen.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                player_y_change += 1
            if event.key == pygame.K_UP:
                player_y_change -= 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN or pygame.K_UP:
                player_y_change = 0

    player_y += player_y_change
    if player_y <= 0:
        player_y = 0
    elif player_y >= 472:
        player_y = 472
    player(player_x, player_y)

    cpu_y += cpu_y_change
    if cpu_y <= 0:
        player_y = 0
    elif player_y >= 472:
        player_y = 472
    cpu(cpu_x, cpu_y)

    ball_x += ball_x_change
    collide = player_collision(ball_x, player_x, ball_y, player_y)
    if collide:
        ball_x_change = -ball_x_change
        ball_x += ball_x_change
    ball(ball_x, ball_y)

    pygame.display.update()

【问题讨论】:

    标签: python pygame collision-detection


    【解决方案1】:

    函数player_collision 采用(ball_x, ball_y, player_x, player_y) 中的参数,但是当您调用该函数时,您给出的参数顺序错误

    collide = player_collision(player_x, ball_x, player_y, ball_y)
    #should be
    collide = player_collision(ball_x, ball_y, player_x, player_y)
    

    您还应该按照@RFairey 的建议进行操作并添加ball_x_change *= -1

    【讨论】:

      【解决方案2】:

      看起来当发生碰撞时,您只需将球移回 x 一步:

      if collide:
          ball_x -= ball_x_change   
      

      要翻转它的方向,你还需要否定 ball_x_change,以便它在反弹后向另一个方向移动:

      if collide:
          ball_x -= ball_x_change
          ball_x_change = -ball_x_change
      

      【讨论】:

      • 我这样做了,但还是不行。球仍然穿过球员。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多