为什么某个if语句在while循环中只触发一次?
Why does a certain if statement only trigger once in a while loop?
我正在尝试进入游戏编程,但我在制作第一款游戏时遇到了困难。我想制作乒乓球游戏,但我无法让碰撞检测工作两次。不管球往哪个方向走,它都起作用一次,但第二次不起作用。这是我的代码:
import pygame
import random
from random import uniform
class Game(object):
def main(self, screen):
rand = random.randint(100, 250)
clock = pygame.time.Clock()
icon = pygame.image.load('player.png').convert_alpha()
image = icon
ball = pygame.image.load('ball.png').convert_alpha() #10 x 10
bar = pygame.image.load('bar.png').convert_alpha() # 25 x 200
divider = pygame.image.load('divider.png').convert_alpha() # 8 x 40
pressed_up = False
pressed_down = False
pressed_up2 = False
pressed_down2 = False
ballActive = False
ballActive2 = False
ballCollide = False
ballCollide2 = False
ballCanCollide = False
ballCanCollide2 = False
ballIsMovingControlled = False
ballIsMovingControlled2 = False
x = 0
barY = 300
barY2 = 300
barHitX = 20
barHitY = 200
barHitX2 = 20
barHitY2 = 200
ballangle = 90
ballAccel = 1
ball_x = 530
ball_y = 390
white = (255, 255, 255)
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
pressed_up = True
if event.key == pygame.K_s:
pressed_down = True
if event.key == pygame.K_UP:
pressed_up2 = True
if event.key == pygame.K_DOWN:
pressed_down2 = True
if event.key == pygame.K_SPACE:
ballActive2 = True
if event.key == pygame.K_LEFT:
ballActive = False
ballActive2 = False
ballIsMovingControlled = True
if event.key == pygame.K_RIGHT:
ballActive = False
ballActive2 = False
ballIsMovingControlled2 = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
pressed_up = False
if event.key == pygame.K_s:
pressed_down = False
if event.key == pygame.K_UP:
pressed_up2 = False
if event.key == pygame.K_DOWN:
pressed_down2 = False
if event.key == pygame.K_LEFT:
ballActive = False
ballActive2 = False
ballIsMovingControlled = False
if event.key == pygame.K_RIGHT:
ballActive = False
ballActive2 = False
ballIsMovingControlled2 = False
if pressed_up == True:
barY -= 10
if pressed_down == True:
barY += 10
if pressed_up2 == True:
barY2 -= 10
if pressed_down2 == True:
barY2 += 10
if ballActive == True:
ball_x -= 5
ball_y = 390
print("moving")
if ball_x == barHitX + 20 and ball_y >= barY and ball_y <= barY + 200:
ball_x = 32
ballActive = False
ballCollide = True
print("stopped1")
if ballActive2 == True:
ball_x += 5
ball_y = 390
print("moving2")
if ball_x == barHitX2 + 1040 and ball_y >= barY2 and ball_y <= barY2 + 200:
ball_x = 1058
ballCollide2 = True
ballActive2 = False
print("stopped2")
if ball_x == 600:
print("why wont you collide")
if ballIsMovingControlled == True:
ball_x -= 5
if ballIsMovingControlled2 == True:
ball_x += 5
if ballCollide == True:
ballActive2 = True
print("stopping1")
ballCollide = False
if ballCollide2 == True:
ballActive = True
print("stopping2")
ballCollide2 = False
def blitLvl():
ballBlit = screen.blit(ball, (ball_x, ball_y))
barBlit = screen.blit(bar, (30, barY))
barBlit2 = barBlit = screen.blit(bar, (1050, barY2))
barHitBox = pygame.draw.rect(screen, white, (30, barY, barHitX, barHitY), 1)
barHitBox2 = pygame.draw.rect(screen, white, (1050, barY2, barHitX2, barHitY2), 1)
screen.blit(divider, (530,20))
screen.blit(divider, (530,100))
screen.blit(divider, (530,180))
screen.blit(divider, (530,260))
screen.blit(divider, (530,340))
screen.blit(divider, (530,420))
screen.blit(divider, (530,500))
screen.blit(divider, (530,580))
screen.blit(divider, (530,660))
screen.blit(divider, (530,740))
screen.blit(divider, (530,820))
pygame.display.update()
screen.fill((0, 0, 0))
blitLvl()
pygame.display.set_caption("Pong")
pygame.display.set_icon(icon)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((1080, 800))
Game().main(screen)
问题出在球处于活动状态时引用的部分。当球向左移动时,我检查左桨的碰撞,反之亦然。其中一个可以触发,但另一个无法识别。它只是直接穿过桨。
if ballActive == True:
ball_x -= 5
ball_y = 390
print("moving")
if ball_x == barHitX + 20 and ball_y >= barY and ball_y <= barY + 200:
ball_x = 32
ballActive = False
ballCollide = True
print("stopped1")
if ballActive2 == True:
ball_x += 5
ball_y = 390
print("moving2")
if ball_x == barHitX2 + 1040 and ball_y >= barY2 and ball_y <= barY2 + 200:
ball_x = 1058
ballCollide2 = True
ballActive2 = False
print("stopped2")
我认为这与你的条件语句有关:
if ball_x == barHitX2 + 1040 and ball_y >= barY2 and ball_y <= barY2 + 200:
ball_x = 1058
并递增 5 和 10。我认为您不想检查 ball_x 是否恰好是 barHitX2 + 1040,而是检查 ball_x 是否在 "barHitX2 range" 内。
(即)
if (ball_x >= BarHitX2 + 1040 and ball_x <= BarHitX2 + 1065) and (ball_y >= barY2 and ball_y <= barY2 + 200):
...
至少这是我的猜测。
我正在尝试进入游戏编程,但我在制作第一款游戏时遇到了困难。我想制作乒乓球游戏,但我无法让碰撞检测工作两次。不管球往哪个方向走,它都起作用一次,但第二次不起作用。这是我的代码:
import pygame
import random
from random import uniform
class Game(object):
def main(self, screen):
rand = random.randint(100, 250)
clock = pygame.time.Clock()
icon = pygame.image.load('player.png').convert_alpha()
image = icon
ball = pygame.image.load('ball.png').convert_alpha() #10 x 10
bar = pygame.image.load('bar.png').convert_alpha() # 25 x 200
divider = pygame.image.load('divider.png').convert_alpha() # 8 x 40
pressed_up = False
pressed_down = False
pressed_up2 = False
pressed_down2 = False
ballActive = False
ballActive2 = False
ballCollide = False
ballCollide2 = False
ballCanCollide = False
ballCanCollide2 = False
ballIsMovingControlled = False
ballIsMovingControlled2 = False
x = 0
barY = 300
barY2 = 300
barHitX = 20
barHitY = 200
barHitX2 = 20
barHitY2 = 200
ballangle = 90
ballAccel = 1
ball_x = 530
ball_y = 390
white = (255, 255, 255)
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
pressed_up = True
if event.key == pygame.K_s:
pressed_down = True
if event.key == pygame.K_UP:
pressed_up2 = True
if event.key == pygame.K_DOWN:
pressed_down2 = True
if event.key == pygame.K_SPACE:
ballActive2 = True
if event.key == pygame.K_LEFT:
ballActive = False
ballActive2 = False
ballIsMovingControlled = True
if event.key == pygame.K_RIGHT:
ballActive = False
ballActive2 = False
ballIsMovingControlled2 = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
pressed_up = False
if event.key == pygame.K_s:
pressed_down = False
if event.key == pygame.K_UP:
pressed_up2 = False
if event.key == pygame.K_DOWN:
pressed_down2 = False
if event.key == pygame.K_LEFT:
ballActive = False
ballActive2 = False
ballIsMovingControlled = False
if event.key == pygame.K_RIGHT:
ballActive = False
ballActive2 = False
ballIsMovingControlled2 = False
if pressed_up == True:
barY -= 10
if pressed_down == True:
barY += 10
if pressed_up2 == True:
barY2 -= 10
if pressed_down2 == True:
barY2 += 10
if ballActive == True:
ball_x -= 5
ball_y = 390
print("moving")
if ball_x == barHitX + 20 and ball_y >= barY and ball_y <= barY + 200:
ball_x = 32
ballActive = False
ballCollide = True
print("stopped1")
if ballActive2 == True:
ball_x += 5
ball_y = 390
print("moving2")
if ball_x == barHitX2 + 1040 and ball_y >= barY2 and ball_y <= barY2 + 200:
ball_x = 1058
ballCollide2 = True
ballActive2 = False
print("stopped2")
if ball_x == 600:
print("why wont you collide")
if ballIsMovingControlled == True:
ball_x -= 5
if ballIsMovingControlled2 == True:
ball_x += 5
if ballCollide == True:
ballActive2 = True
print("stopping1")
ballCollide = False
if ballCollide2 == True:
ballActive = True
print("stopping2")
ballCollide2 = False
def blitLvl():
ballBlit = screen.blit(ball, (ball_x, ball_y))
barBlit = screen.blit(bar, (30, barY))
barBlit2 = barBlit = screen.blit(bar, (1050, barY2))
barHitBox = pygame.draw.rect(screen, white, (30, barY, barHitX, barHitY), 1)
barHitBox2 = pygame.draw.rect(screen, white, (1050, barY2, barHitX2, barHitY2), 1)
screen.blit(divider, (530,20))
screen.blit(divider, (530,100))
screen.blit(divider, (530,180))
screen.blit(divider, (530,260))
screen.blit(divider, (530,340))
screen.blit(divider, (530,420))
screen.blit(divider, (530,500))
screen.blit(divider, (530,580))
screen.blit(divider, (530,660))
screen.blit(divider, (530,740))
screen.blit(divider, (530,820))
pygame.display.update()
screen.fill((0, 0, 0))
blitLvl()
pygame.display.set_caption("Pong")
pygame.display.set_icon(icon)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((1080, 800))
Game().main(screen)
问题出在球处于活动状态时引用的部分。当球向左移动时,我检查左桨的碰撞,反之亦然。其中一个可以触发,但另一个无法识别。它只是直接穿过桨。
if ballActive == True:
ball_x -= 5
ball_y = 390
print("moving")
if ball_x == barHitX + 20 and ball_y >= barY and ball_y <= barY + 200:
ball_x = 32
ballActive = False
ballCollide = True
print("stopped1")
if ballActive2 == True:
ball_x += 5
ball_y = 390
print("moving2")
if ball_x == barHitX2 + 1040 and ball_y >= barY2 and ball_y <= barY2 + 200:
ball_x = 1058
ballCollide2 = True
ballActive2 = False
print("stopped2")
我认为这与你的条件语句有关:
if ball_x == barHitX2 + 1040 and ball_y >= barY2 and ball_y <= barY2 + 200:
ball_x = 1058
并递增 5 和 10。我认为您不想检查 ball_x 是否恰好是 barHitX2 + 1040,而是检查 ball_x 是否在 "barHitX2 range" 内。
(即)
if (ball_x >= BarHitX2 + 1040 and ball_x <= BarHitX2 + 1065) and (ball_y >= barY2 and ball_y <= barY2 + 200):
...
至少这是我的猜测。