2016-05-05 13 views
0

スプライトを使用せずにポンゲームを作ろうとしていますが、衝突を正しく検出できないようです。最初のif文は正しく衝突を検出し、適切にボールをバウンスしますが、2番目のif文は衝突を全く検出せず、理由を特定できません。スプライトを使わずにpygameの衝突を検出できません

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

それが今より多くの意味を作る必要がありますので、私はポストに完全なコードを追加しました。 –

答えて

0
bally2 = bally + balldiam 
ballx2 = ballx + balldiam 

これらのグローバル変数は一度定義されています。彼らはいつも変化するballyballxに相対的なので、ボールがその位置を変えると、ループ内でそれらを更新する必要があります。

0

bally <= rect2y2 or rect2y <= bally2の他の演算子よりも優先順位が高いために衝突が発生していないか検出されています。

0

あなたはここに、ボールに回転を追加するときに、ballx2を更新したことがない:

bally += ballspeed 
ballx -= ballspeed 
bally2 += ballspeed 
関連する問題