首页 > 解决方案 > 简单的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()

标签: pythonpygame

解决方案


此行检查碰撞:

# 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:

推荐阅读