def花 发表于 2020-9-24 22:56:31

很简单的pygame游戏有一步我没懂

import pygame,sys

pygame.init()
size = width,height = 600,400
speed =
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 - ball_ract.left,event.pos-ball_ract.top)#就是这里,是为了防止鼠标点击到窗口旁边,卡在旁边不动,没懂为什么这么判断
      elif event.type == pygame.MOUSEMOTION:
            if event.buttons == 1:
                ball_ract = ball_ract.move(event.pos-ball_ract.left,event.pos-ball_ract.top)
    if pygame.display.get_active() and not still:
      ball_ract = ball_ract.move(speed,speed)
    if ball_ract.right > width or ball_ract.left < 0:
      speed = -speed
      if ball_ract.right > width and ball_ract.right + speed > ball_ract.right:
            speed = -speed
    if ball_ract.bottom > height or ball_ract.top <0:
      speed = -speed
      if ball_ract.bottom > height and ball_ract.bottom + speed > ball_ract.bottom:
            speed = -speed

    screen.fill(BLACK)
    screen.blit(ball,ball_ract)
    pygame.display.update()
    clock.tick(fps)
页: [1]
查看完整版本: 很简单的pygame游戏有一步我没懂