如何使用我使用的代码使球从绘制的矩形弹回?

时间:2019-07-13 21:06:03

标签: python pygame

我有一个if语句,不断检查球和乒乓球的x和y值是否相同。但是,它并没有改变方向。为什么会这样呢? if语句是“ bally + = directiony”之后的第一个语句

import pygame
import random
pygame.init()



screenL = 1000
screenW = 600
screen = pygame.display.set_mode([screenL, screenW])
playerpoints = 0
aipoints = 0
run = True
playery = 50
playerx = 10
aiy = screenW/2
ballx = screenL//2 
bally = screenW//2
directiony = 2
directionx = 2
while run:
    pygame.time.delay(5)
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

    screen.fill((0,0,0))
    pygame.draw.rect(screen, (255, 255, 255), (playerx, playery, 10, 100))
    keys = pygame.key.get_pressed()

    if playery > 0 and keys[pygame.K_UP]:
    playery -= 2
    if playery < screenW - 100 and keys[pygame.K_DOWN]:
    playery += 2
    pygame.draw.line(screen, [255, 255, 255], [screenL/2, 0], [screenL/2, screenW])
    pygame.draw.rect(screen, (255, 255, 255), (screenL - 60, aiy, 10, 100))
    keys = pygame.key.get_pressed()
    pygame.draw.circle(screen, [255, 255, 255], [ballx, bally], 8)

    ballx += directionx
    bally += directiony
    if ballx == playerx + 10 and bally == playery + 100:
    directionx = 2
    if bally == 0:
    directiony = 2
    if bally == screenW:
    directiony = -2
    if ballx == 0:
    directionx = 2
    if ballx == screenL:
    directionx = -2
    pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

您应使用pygame.Rect保留x,y,width,height。它具有检查两个矩形(或矩形和点-在单击菜单中的按钮时)之间的碰撞的功能,因此它将检查pong的不同位置的碰撞。

ball_rect.colliderect(player_rect)

ball_rect.colliderect(AI_rect)

此代码检查与两个pongs的冲突

import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 600

SCREEN_WIDTH_HALF = SCREEN_WIDTH//2
SCREEN_HEIGHT_HALF = SCREEN_HEIGHT//2

#--- game ---

# - init -

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# - objects -

player_rect = pygame.Rect(10, 50, 10, 100)
ai_rect = pygame.Rect(SCREEN_WIDTH-60, SCREEN_HEIGHT_HALF, 10, 100)

ball_rect = pygame.Rect(0, 0, 16, 16)
ball_rect.center = (SCREEN_WIDTH_HALF, SCREEN_HEIGHT_HALF)

directiony = 2
directionx = 2

playerpoints = 0
aipoints = 0

# --- mainloop ---

clock = pygame.time.Clock()

run = True
while run:
    clock.tick(120) # 120 FPS
    #pygame.time.delay(5)

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # --- changes ---

    keys = pygame.key.get_pressed()

    if player_rect.top > 0 and keys[pygame.K_UP]:
        player_rect.y -= 2
    if player_rect.bottom < SCREEN_HEIGHT and keys[pygame.K_DOWN]:
        player_rect.y += 2

    ball_rect.x += directionx
    ball_rect.y += directiony

    # collision with player 
    if ball_rect.colliderect(player_rect):
        directionx = -directionx

    # collision with AI
    if ball_rect.colliderect(ai_rect):
        directionx = -directionx

    # collision with border        
    if ball_rect.top <= 0:
        directiony = -directiony
    if ball_rect.bottom >= SCREEN_HEIGHT:
        directiony = -directiony

    if ball_rect.left <= 0:
        directionx = -directionx
    if ball_rect.right >= SCREEN_WIDTH:
        directionx = -directionx

    # -- draws ---

    screen.fill(BLACK)

    pygame.draw.rect(screen, WHITE, player_rect)
    pygame.draw.rect(screen, WHITE, ai_rect)
    pygame.draw.circle(screen, WHITE, ball_rect.center, 8)
    pygame.draw.line(screen, WHITE, (SCREEN_WIDTH_HALF, 0), (SCREEN_WIDTH_HALF, SCREEN_HEIGHT))

    pygame.display.flip()

# --- end ---

pygame.quit()