【发布时间】: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