2016-05-05 18 views
0

Ich versuche, ein Pong-Spiel ohne Sprites zu machen, aber ich kann nicht scheinen, die Kollisionen richtig zu erkennen. Die erste if-Anweisung erkennt eine Kollision korrekt und prallt den Ball entsprechend auf, aber die zweite if-Anweisung erkennt überhaupt keine Kollision und ich kann nicht herausfinden warum.Ich kann keine Kollision in Pygame ohne Sprites erkennen

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") 

Hier ist der vollständige Code.

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() 
+0

Ich habe den vollständigen Code auf den Posten so sollte es jetzt mehr Sinn machen. –

Antwort

0
bally2 = bally + balldiam 
ballx2 = ballx + balldiam 

diese Globals werden einmal definiert. Da sie relativ zu bally und ballx sind, die sich ständig ändern, müssen Sie sie innerhalb der Schleife aktualisieren, wenn der Ball seine Position ändert.

0

Die Erkennung, wenn keine Kollision könnte, weil oder hat eine höhere Priorität als die anderen Operatoren in bally <= rect2y2 or rect2y <= bally2.

0

Sie nie ballx2 aktualisieren, wenn Sie die Geschwindigkeit auf den Ball hinzufügen, hier:

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