【pygame学习_2】基本壁球运动

# Unit PYG02: Pygame Wall Ball Game version 1  展示型
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") # 球的图片
ballrect = ball.get_rect() # 生成对应矩形

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    ballrect = ballrect.move(speed[0], speed[1]) # 球的移动,横向和纵向两个速度
    if ballrect.left < 0 or ballrect.right > width: # 判断是否到达边缘,如果到了,则将速度取反
        speed[0] = - speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = - speed[1]

    # 刷新,将球绘制在对应的矩形上
    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    pygame.display.update()

猜你喜欢

转载自blog.csdn.net/weixin_44727682/article/details/128495930
今日推荐