pygame壁球小游戏代码的疑问
import pygame
import sys
pygame.init()
size = width, height = 600, 400
speed =
Black = (0, 0, 0)
# screen = pygame.display.set_mode(size)
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption('Pygame壁球')
ball = pygame.image.load('C:\\Users\\admin\\Desktop\\PYG02-ball.gif')
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
still = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if speed == 0:
speed = speed
else:
speed = (abs(speed) - 1)*int(speed/abs(speed))
elif event.key == pygame.K_RIGHT:
if speed > 0:
speed += 1
else:
speed -= 1
elif event.key == pygame.K_UP:
if speed > 0:
speed += 1
else:
speed -= 1
elif event.key == pygame.K_DOWN:
if speed == 0:
speed = speed
else:
speed = (abs(speed) - 1)*int(speed/abs(speed))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size, event.size
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos - ballrect.left, event.pos - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons == 1:
ballrect = ballrect.move(event.pos - ballrect.left, event.pos - ballrect.top)
print(event.pos - ballrect.left, event.pos - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed, speed)
if ballrect.left < 0 or ballrect.right > width:
speed = -speed
if ballrect.right > width and ballrect.right + speed > ballrect.right:
speed = -speed
if ballrect.top < 0 or ballrect.bottom > height:
speed = -speed
if ballrect.bottom > height and ballrect.bottom + speed > ballrect.bottom:
speed = -speed
screen.fill(Black)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
# pygame.time.Clock().tick(fps)
上面的代码中的pygame.MOUSEMOTION事件我不是很理解,(event.pos - ballrect.left, event.pos - ballrect.top)的结果不是(0,0)吗,
为什么球会跟着鼠标一起移动
{:5_92:} 有没有人来管管我啊,卡这里了,找半天没结果{:5_100:} 来个人啊,球球了{:5_111:} {:10_256:} 在输出之前已经执行过ballrect.move(...)了,所以输出的是(0,0)
把ln58和ln59换一下输出的就不是0,0了
页:
[1]