簡體   English   中英

沒有精靈,我無法在pygame中檢測到碰撞

[英]I can't detect a collision in pygame without sprites

我試圖在不使用Sprite的情況下制作Pong游戲,但似乎無法正確檢測到碰撞。 第一個if語句正確地檢測到碰撞並適當地反彈球,但是第二個if語句根本沒有檢測到碰撞,我不知道為什么。

if ballx <= rectx2 and ((bally >= recty and bally <= recty2) or (bally2 >= recty and bally2 <= recty2)):
    ballright = True
    print("paddle 1 collision")

if ballx2 >= rect2x and ((bally >= rect2y and bally <= rect2y2) or (bally2 >= rect2y and bally2 <= rect2y2)): #DOES NOT DETECT COLLISION
    ballright = False
    print("paddle 2 collision")

這是完整的代碼。

import pygame, sys
from pygame.locals import *
import random

pygame.init()

import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0"

info = pygame.display.Info()

screenWidth = info.current_w
screenHeight = info.current_h
screenSize = [screenWidth, screenHeight]
screen = pygame.display.set_mode(screenSize)
pygame.display.set_caption("Pong")

clock = pygame.time.Clock()

#colors     R      G      B
white = (255, 255, 255)
black = (    0,     0,     0)
blue =  (    0,     0, 255)
red =    (255,     0,     0)

ballx = 30
bally = 345
balldiam = 10
bally2 = bally + balldiam
ballx2 = ballx + balldiam
if random.randint(0,100)> 50:
    balldown = True
else:
    balldown = False
ballright = True
ballspeed = 10
balllocation = (ballx, bally)

rectx = 5
recty = 290
rectx2 = 30
recty2 = 460
paddle01location = (rectx, recty, rectx2, recty2)

def drawPaddle01():
    pygame.draw.rect(screen, black, [rectx, recty, 25, 170], 0)

rect2x = 1335
rect2y = 290
rect2x2 = 1360
rect2y2 = 460
paddle02location = (rect2x, rect2y, rect2x2, rect2y2)

def drawPaddle02():
    pygame.draw.rect(screen, black, [rect2x, rect2y, 25, 170], 0)

def moveBall():
    global ballx, bally, balldown, ballright, bally2

    #Do we need to bounce?
    if balldown and bally >= screenHeight - balldiam:
        balldown = False
    if not balldown and bally <= 0:
        balldown = True

    if ballright and ballx >= screenWidth - balldiam:
        ballright = False
    if not ballright and ballx <= 0:
        ballright = True

    #Check for collision with paddle 01
    if ballx <= rectx2 and ((bally >= recty and bally <= recty2) or (bally2 >= recty and bally2 <= recty2)):
        ballright = True
        print("paddle 1 collision")

    #Check for collision with paddle 02
##    if ballx2 == 1270 and rect2y <= bally <= rect2y2 or rect2y <= bally2 <= rect2y2: DETECTS BALL COLLISION W/ PADDLE 02 WHEN THERE IS NO COLLISION
    if ballx2 >= rect2x and ((bally >= rect2y and bally <= rect2y2) or (bally2 >= rect2y and bally2 <= rect2y2)): #DOES NOT DETECT COLLISION
        ballright = False
        print("paddle 2 collision")

    #Move ball in correct directions
    if balldown == True and ballright == True:
        bally += ballspeed
        ballx += ballspeed
        bally2 += ballspeed
    if balldown == True and ballright == False:
        bally += ballspeed
        ballx -= ballspeed
        bally2 += ballspeed
    if balldown == False and ballright == True:
        bally -= ballspeed
        ballx += ballspeed
        bally2 -= ballspeed
    if balldown == False and ballright == False:
        bally -= ballspeed
        ballx -= ballspeed
        bally2 -= ballspeed

player1score = 0 
player2score = 0     

def score():
    global player1score, player2score
    if ballx == 0:
        player2score += 1
    if ballx + balldiam >= screenWidth:
        player1score += 1

def drawBall():
    pygame.draw.ellipse(screen, red, [ballx, bally, balldiam, balldiam], 0)

def pollKeys():
    global rectx, recty, recty2, rectx2, rect2x, rect2y, rect2y2, rect2x2
    keys = pygame.key.get_pressed()
    if keys[K_w] and recty - 10 > 0:
        recty -= 10
        recty2 -= 10
    if keys[K_s] and recty2 + 10 < 765:
        recty += 10
        recty2 += 10
    if keys[K_UP] and rect2y - 10 > 0:
        rect2y -= 10
        rect2y2 -= 10
    if keys[K_DOWN] and rect2y2 + 10 < 765:
        rect2y += 10
        rect2y2 += 10

def pollKeysEsc():
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        pygame.quit()

done = False

while not done:
    # 1. Process events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    pollKeys()

    # 2. Program logic, change variables, etc.
    moveBall()
    score()

    # 3. Draw stuff
    screen.fill(white)
    #Draw paddle 1st player
    drawPaddle01()
    #Draw paddle 2nd player
    drawPaddle02()
    #Draw ball
    drawBall()
    #Draw Scoreboards
    font = pygame.font.Font(None, 36)
    text = font.render("P1 = %d" % player1score, True, blue)
    screen.blit(text, [10, 10])

    text2 = font.render("P2 = %d" % player2score, True, blue)
    screen.blit(text2, [1200, 10])
    #Draw winning text
    if player1score >= 1: #MAKE SURE YOU CHANGE THIS BEFORE TURNING IT IN!!!!!
        font2 = pygame.font.Font(None, 50)
        text3 = font2.render("Player 1 wins!!! Press ESC to quit.", True, red)
        pollKeysEsc()
        screen.blit(text3, [400, 300])
    elif player2score >= 1: #MAKE SURE YOU CHANGE THIS BEFORE TURNING IT IN!!!!!
        font2 = pygame.font.Font(None, 50)
        text3 = font2.render("Player 2 wins!!! Press ESC to quit", True, red)
        pollKeysEsc()
        screen.blit(text3, [400, 300])

    pygame.display.flip()
    clock.tick(30)

pygame.quit()
bally2 = bally + balldiam
ballx2 = ballx + balldiam

這些全局變量定義一次。 由於它們ballx與變化的ballyballx ,因此當球改變其位置時,必須在循環內更新它們。

bally <= rect2y2 or rect2y <= bally2 ,沒有碰撞的檢測可能是因為或比其他運算符具有更高的優先級。

在向球中添加速度時,您永遠不會更新ballx2,這里:

bally += ballspeed
ballx -= ballspeed
bally2 += ballspeed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM