python - 简单的python游戏,玩家可以踢球,但是如果玩家改变了他们的大小,球就不能再被触摸了
问题描述
import pygame
import random
pygame.init()
# Window setup
size = [400, 300]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
# player position
x = size[0] / 2
y = size[1] / 2
# ball position
ballX = random.randrange(0, size[0])
ballY = random.randrange(0, size[1])
# colors
red = pygame.color.Color('#FF8080')
blue = pygame.color.Color('#8080FF')
white = pygame.color.Color('#FFFFFF')
black = pygame.color.Color('#000000')
width = 10
height = 10
radius = 6
# Game loop
done = False
def checkOffScreenX(x):
if x > size[0] - width>-x:
x -= 10
sound=pygame.mixer.Sound('walltouch2.mp3')
sound.play(loops=0)
elif x < 0:
x += 10
sound=pygame.mixer.Sound('walltouch2.mp3')
sound.play(loops=0)
return x
def checkOffScreenY(y):
if y > size[1] - height> -y:
y -= 10
sound=pygame.mixer.Sound('walltouch2.mp3')
sound.play(loops=0)
elif y < 0:
y += 10
sound=pygame.mixer.Sound('walltouch2.mp3')
sound.play(loops=0)
return y
下面是问题发生的地方,每当玩家尝试改变他们的大小时,球就无法正确击球,在测试这个问题时我发现玩家的hitbox是玩家最初的大小,随着玩家宽度和高度的增加,hitbox 的大小不会增加
def checkTouching():
# Causes mini explosion if the players are touching
global x
global ballX
global y
global ballY
global width
global height
# Check if player and ball are touching
if -10 < y - ballY <10 and -10 < x - ballX<10:
# Music
sound=pygame.mixer.Sound('collision2.mp3')
sound.play(loops=0)
#draw an explosion
pygame.draw.circle(screen, white, [x, y], 15)
xDiff = x - ballX
yDiff = y - ballY
# check if ball is on edge of screen
if ballX == 0:
xDiff -= 5
elif ballX == size[0]:
xDiff += 5
if ballY == 0:
yDiff -= 5
elif ballY == size[1]:
yDiff += 5
# move the ball and the player
x += xDiff * 3
ballX -= xDiff * 3
y += yDiff * 3
ballY -= yDiff * 3
while not done:
screen.fill(black)
keys = pygame.key.get_pressed()
#player movement
if keys[pygame.K_w]:
y -= 1
elif keys[pygame.K_s]:
y += 1
elif keys[pygame.K_d]:
x += 1
elif keys [pygame.K_a]:
x -= 1
elif keys[pygame.K_UP]:
height += 0.5
if height > 130:
height -= 5
elif keys[pygame.K_DOWN]:
height -= 0.5
if height < 6:
height += 5
elif keys[pygame.K_RIGHT]:
width += 0.5
if width > 130:
width -= 5
elif keys[pygame.K_LEFT]:
width -= 0.5
if width < 6:
width += 5
elif keys[pygame.K_r]:
width = 10
height = 10
clock.tick(200)
# draw player
pygame.draw.ellipse(screen, blue, [x, y, width, height])
# draw ball
pygame.draw.circle(screen, red, [ballX, ballY], 6)
# Check if player is touching the ball
checkTouching()
# Check off-screen
x = checkOffScreenX(x)
y = checkOffScreenY(y)
ballX = checkOffScreenX(ballX)
ballY = checkOffScreenY(ballY)
pygame.display.flip()
解决方案
此行检查碰撞:
# Check if player and ball are touching:
if -10 < y - ballY <10 and -10 < x - ballX<10:
将硬编码的初始值10
更改为变量值width
并将height
进行碰撞检查:
if -height < y - ballY < height and -width < x - ballX < width:
推荐阅读
- c# - 如何在没有小偏移的情况下将 UV 映射到立方体
- c# - 使用多个重叠可能性的两个列表的一对一匹配算法
- jquery - 无法在 Typescript 中实现 CryptoJS
- javascript - $ 未定义 - jquery
- boost - 有没有办法摆脱“新的 Boost 版本可能有不正确或缺少的依赖项和导入的目标”警告?
- javascript - 将字符串输出为 innerHTML,但不会呈现为 HTML
- perl - Perl,如何根据数组中的特定位置对哈希(数组)键进行排序
- python - selenium ChromeWebDriver:无法设置 cookie
- servlets - 如何“自定义” Servlet 的相对 URL
- c# - 在我的 Rpi 上加载资源时出现 SSL 问题