我有一个if语句不断地检查球和乒乓球的x和y值是否相同。然而,它并没有改变方向。为什么会这样?if语句是“bally+=directiony”之后的第一个语句
import pygame
import random
pygame.init()
screenL = 1000
screenW = 600
screen = pygame.display.set_mode([screenL, screenW])
playerpoints = 0
aipoints = 0
run = True
playery = 50
playerx = 10
aiy = screenW/2
ballx = screenL//2
bally = screenW//2
directiony = 2
directionx = 2
while run:
pygame.time.delay(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill((0,0,0))
pygame.draw.rect(screen, (255, 255, 255), (playerx, playery, 10, 100))
keys = pygame.key.get_pressed()
if playery > 0 and keys[pygame.K_UP]:
playery -= 2
if playery < screenW - 100 and keys[pygame.K_DOWN]:
playery += 2
pygame.draw.line(screen, [255, 255, 255], [screenL/2, 0], [screenL/2, screenW])
pygame.draw.rect(screen, (255, 255, 255), (screenL - 60, aiy, 10, 100))
keys = pygame.key.get_pressed()
pygame.draw.circle(screen, [255, 255, 255], [ballx, bally], 8)
ballx += directionx
bally += directiony
if ballx == playerx + 10 and bally == playery + 100:
directionx = 2
if bally == 0:
directiony = 2
if bally == screenW:
directiony = -2
if ballx == 0:
directionx = 2
if ballx == screenL:
directionx = -2
pygame.display.flip()
您应该使用pygame.Rect来保持
x,y,width,height
。它具有检查两个矩形(或矩形和点-即当您单击菜单中的按钮时)之间冲突的功能,因此它将检查pong
不同位置的冲突此代码检查与
pongs
的冲突相关问题 更多 >
编程相关推荐