无法为我的Ping Pong pygame制作命中箱

时间:2019-03-21 08:09:54

标签: python python-3.x pygame

这是我的脚本,它需要制作一个打击盒,以便球弹起。这是一个非常基本的乒乓球游戏。我已经尝试过使用座标并尝试观看其他视频和论坛,我非常感谢有人可以帮助我。我根本听不懂,这是学校的作业。有点懒,但是有人可以帮我做一个碰撞箱。或者至少帮助我。

这是脚本:

import pygame
pygame.init()

width = 500
height = 400
#pygame.title("Ping Pong Level 1")
screen = pygame.display.set_mode((width, height))
y = 30
g = 30
x = 40
f = 100
velxX = 2
velfF = 2
velX = 50
velF = 20

clock = pygame.time.Clock()


def draw():
    screen.fill((0, 0, 0))
    player = pygame.draw.rect(screen, (255, 255, 255), (30, y, 30, 100))
    player1 = pygame.draw.rect(screen, (255, 255, 255), (440, g, 30, 100))
    line = pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()


    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_w] and y > 0:
        y -= 5
    if pressed[pygame.K_s] and y < height - 100:
        y += 5
    if pressed[pygame.K_UP] and g > 0:
        g -= 5
    if pressed[pygame.K_DOWN] and g < height - 100:
        g += 5


    velF += velfF
    velX += velxX

    if velF < 0 or velF > height - 5:
        velfF *= -1
    if velX < 0 or velX > width - 5:
        velxX *= -1

    pygame.draw.circle(screen, (0, 255, 0), (velX, velF), 5)
    clock.tick(60)
    pygame.display.flip()

    draw()

screen.mainloop()

1 个答案:

答案 0 :(得分:3)

好的,让我们逐步完成此步骤。

首先,让我们重新排序您的代码,以便我们有一个不错的标准主循环event handling -> state update -> draw。我们看到您有一个draw函数,但是您并未在该函数中绘制所有内容。另外,您在pygame.display.flip()之前致电drawscreen.mainloop()没有任何意义,并且sys.exit()也将因为您未导入sys而无法正常工作。

步骤1:

import pygame

def main():
    pygame.init()

    width = 500
    height = 400
    #pygame.title("Ping Pong Level 1")
    screen = pygame.display.set_mode((width, height))
    y = 30
    g = 30
    x = 40
    f = 100
    velxX = 2
    velfF = 2
    velX = 50
    velF = 20

    clock = pygame.time.Clock()

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

        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_w] and y > 0:
            y -= 5
        if pressed[pygame.K_s] and y < height - 100:
            y += 5
        if pressed[pygame.K_UP] and g > 0:
            g -= 5
        if pressed[pygame.K_DOWN] and g < height - 100:
            g += 5

        velF += velfF
        velX += velxX

        if velF < 0 or velF > height - 5:
            velfF *= -1
        if velX < 0 or velX > width - 5:
            velxX *= -1

        screen.fill((0, 0, 0))
        pygame.draw.circle(screen, (0, 255, 0), (velX, velF), 5)
        player = pygame.draw.rect(screen, (255, 255, 255), (30, y, 30, 100))
        player1 = pygame.draw.rect(screen, (255, 255, 255), (440, g, 30, 100))
        line = pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
        pygame.display.flip()

        clock.tick(60)

if __name__ == '__main__':
    main()

接下来,让我们看看您的游戏状态和绘图。您有一堆名称很奇怪的变量,还有一些您没有使用或没有意义的变量(例如velxXfplayer1),您还会绘制一些矩形,因此,让我们使用适当的数据结构来解决问题:有用的RectVector2类。

第2步:

import pygame

def main():
    pygame.init()

    screen = pygame.display.set_mode((500, 400))
    screen_rect = screen.get_rect()

    left_paddle = pygame.Rect((30, 30, 30, 100))
    right_paddle = pygame.Rect((440, 30, 30, 100))

    ball_position = pygame.Vector2((50, 20))
    ball_direction = pygame.Vector2((1, 1)).normalize()
    ball_speed = 5

    clock = pygame.time.Clock()

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

        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_w]: left_paddle.move_ip(0, -5)
        if pressed[pygame.K_s]: left_paddle.move_ip(0, 5)
        if pressed[pygame.K_UP]: right_paddle.move_ip(0, -5)
        if pressed[pygame.K_DOWN]: right_paddle.move_ip(0, 5)

        # keep paddles on the screen
        left_paddle.clamp_ip(screen_rect)
        right_paddle.clamp_ip(screen_rect)

        ball_position += ball_direction * ball_speed

        # flip direction on edge
        if ball_position[1] < 0 or ball_position[1] > screen_rect.height - 5:
            ball_direction = pygame.Vector2(ball_direction[0], -ball_direction[1])

        if ball_position[0] < 0 or ball_position[0] > screen_rect.width - 5:
            ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])

        screen.fill((0, 0, 0))
        pygame.draw.rect(screen, (255, 255, 255), left_paddle)
        pygame.draw.rect(screen, (255, 255, 255), right_paddle)
        pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
        pygame.draw.circle(screen, (0, 255, 0), [int(v) for v in ball_position], 5)
        pygame.display.flip()

        clock.tick(60)

if __name__ == '__main__':
    main()

到目前为止,太好了。现在,实现碰撞检测很容易。我们可以计算出下一步的球位置,然后检查它们是否会与桨碰撞。由于我们使用的是Rect类,因此很容易检查碰撞(collidepoint)并使其变大(inflate,因为球的大小大于一个像素)

第3步:

import pygame

def main():
    pygame.init()

    screen = pygame.display.set_mode((500, 400))
    screen_rect = screen.get_rect()

    left_paddle = pygame.Rect((30, 30, 30, 100))
    right_paddle = pygame.Rect((440, 30, 30, 100))

    ball_position = pygame.Vector2((150, 120))
    ball_direction = pygame.Vector2((1, 1)).normalize()
    ball_speed = 5

    clock = pygame.time.Clock()

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

        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_w]: left_paddle.move_ip(0, -5)
        if pressed[pygame.K_s]: left_paddle.move_ip(0, 5)
        if pressed[pygame.K_UP]: right_paddle.move_ip(0, -5)
        if pressed[pygame.K_DOWN]: right_paddle.move_ip(0, 5)

        # keep paddles on the screen
        for paddle in (left_paddle, right_paddle):
            paddle.clamp_ip(screen_rect)

        ball_position += ball_direction * ball_speed

        # flip direction on edge
        if ball_position[1] < 0 or ball_position[1] > screen_rect.height - 5:
            ball_direction = pygame.Vector2(ball_direction[0], -ball_direction[1])

        if ball_position[0] < 0 or ball_position[0] > screen_rect.width - 5:
            ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])

        next_ball_position = ball_position + ball_direction * ball_speed
        for paddle in (left_paddle, right_paddle):
            if paddle.inflate(5, 5).collidepoint(next_ball_position):
                ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])

        screen.fill((0, 0, 0))
        for paddle in (left_paddle, right_paddle):
            pygame.draw.rect(screen, (255, 255, 255), paddle)
        pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
        pygame.draw.circle(screen, (0, 255, 0), [int(v) for v in ball_position], 5)
        pygame.display.flip()

        clock.tick(60)

if __name__ == '__main__':
    main()

enter image description here

请注意,这仅在球撞击前部的桨叶时有效,但是您可以轻松地检查球相对于桨叶的位置,以确定是否要更改球的方向y或{{1} }轴。

由于我们使用矢量作为球的方向,因此也很容易让球以45°以外的角度移动。