首页 > 其他分享 >会动的球

会动的球

时间:2022-12-01 22:33:32浏览次数:20  
标签:ball 会动 ballrect pygame 窗口 speed event

代码:

import sys     # 导入sys模块
import pygame     # 导入pygame模块

pygame.init()        # 初始化pygame
size = width,height= 600,400  # 设置窗口
screen = pygame.display.set_mode(size)    # 显示窗口
color = (0,0,0)      # 设置颜色
ball = pygame.image.load("YuMao.jpg")     # 加载图片
ballrect = ball.get_rect()       # 获取矩形区域
speed = [5,5]    # 设置移动的X轴,Y轴的距离
clock = pygame.time.Clock()       # 设置始终
# 执行死循环,确保窗口一直显示
while True:
    clock.tick(60)       # 每秒执行60次
    for event in pygame.event.get():     # 检查事件
        if event.type == pygame.QUIT:    # 如果单击关闭窗口,则退出
            pygame.quit()         # 退出pygame
            sys.exit()
    ballrect = ballrect.move(speed)      # 移动小球
    # 碰到左右边缘
    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(color)  # 填充颜色
    screen.blit(ball,ballrect)        # 将图片画到窗口上
    pygame.display.flip()     # 更新全部提示

 

 运行:

 

 

标签:ball,会动,ballrect,pygame,窗口,speed,event
From: https://www.cnblogs.com/zzby/p/16943010.html

相关文章

  • 实现一个会动的鸿蒙 LOGO
    本文将带大家简单实现一个会动的鸿蒙LOGO。emmm,写本文的动机是之前看到一篇实现鸿蒙LOGO的文章--产品经理:鸿蒙那个开场动画挺帅的给咱们页面也整一个呗鸿蒙的LOGO本......