|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pygame,sys
- pygame.init()
- size = width,height = 600,400
- speed = [1,1]
- BLACK = 0,0,0
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("pygame壁球碰撞")
- ball = pygame.image.load('PYG02-ball.gif')
- ball_ract = ball.get_rect()
- fps = 300
- clock = pygame.time.Clock()
- still = False
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- still = True
- elif event.type == pygame.MOUSEBUTTONUP:
- still = False
- if event.button == 1:
- ball_ract = ball_ract.move(event.pos[0] - ball_ract.left,event.pos[1]-ball_ract.top)#就是这里,是为了防止鼠标点击到窗口旁边,卡在旁边不动,没懂为什么这么判断
- elif event.type == pygame.MOUSEMOTION:
- if event.buttons[0] == 1:
- ball_ract = ball_ract.move(event.pos[0]-ball_ract.left,event.pos[1]-ball_ract.top)
- if pygame.display.get_active() and not still:
- ball_ract = ball_ract.move(speed[0],speed[1])
- if ball_ract.right > width or ball_ract.left < 0:
- speed[0] = -speed[0]
- if ball_ract.right > width and ball_ract.right + speed[0] > ball_ract.right:
- speed[0] = -speed[0]
- if ball_ract.bottom > height or ball_ract.top <0:
- speed[1] = -speed[1]
- if ball_ract.bottom > height and ball_ract.bottom + speed[1] > ball_ract.bottom:
- speed[1] = -speed[1]
- screen.fill(BLACK)
- screen.blit(ball,ball_ract)
- pygame.display.update()
- clock.tick(fps)
复制代码 |
|