我试图在不使用精灵的情况下进行乒乓球比赛,但我似乎无法正确地发现碰撞。第一个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()
答案 0 :(得分:0)
bally2 = bally + balldiam
ballx2 = ballx + balldiam
这些全局变量定义一次。因为它们相对于bally
和ballx
一直在变化,所以当球改变位置时,你必须在循环内更新它们。
答案 1 :(得分:0)
检测何时没有碰撞可能是因为或优先级高于bally <= rect2y2 or rect2y <= bally2
中的其他运算符。
答案 2 :(得分:0)
在向球添加速度时,你永远不会更新ballx2,这里:
bally += ballspeed
ballx -= ballspeed
bally2 += ballspeed