import pygame
import sys
pygame.init()
size = width, height = 600, 400
speed = [1, 1]
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] == 0:
speed[0] = speed[0]
else:
speed[0] = (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
if speed[0] > 0:
speed[0] += 1
else:
speed[0] -= 1
elif event.key == pygame.K_UP:
if speed[1] > 0:
speed[1] += 1
else:
speed[1] -= 1
elif event.key == pygame.K_DOWN:
if speed[1] == 0:
speed[1] = speed[1]
else:
speed[1] = (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
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[0] - ballrect.left, event.pos[1] - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
print(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0], speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = -speed[1]
screen.fill(Black)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
# pygame.time.Clock().tick(fps)
上面的代码中的pygame.MOUSEMOTION事件我不是很理解,(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)的结果不是(0,0)吗,
为什么球会跟着鼠标一起移动
|