首页 > 编程语言 >Pythontext_9

Pythontext_9

时间:2022-12-06 21:11:39浏览次数:42  
标签:25 ball 窗口 ballrect Pythontext pygame speed

 1 # -*- coding:utf-8 -*-
 2 import sys  # 导入sys模块
 3 import pygame  # 导入pygame模块
 4  
 5 pygame.init()  # 初始化pygame
 6 size = width, height = 1000, 700  # 设置窗口
 7 screen = pygame.display.set_mode(size)  # 显示窗口
 8 color = (0, 0, 0)  # 设置颜色
 9  
10 ball = pygame.image.load("ball.png")  # 加载图片
11 ballrect = ball.get_rect()  # 获取矩形区域
12  
13 speed = [25, 25]  # 设置移动的X轴、Y轴距离
14 clock = pygame.time.Clock()  # 设置时钟
15 # 执行死循环,确保窗口一直显示
16 while True:
17     clock.tick(20)  # 每秒执行60次
18     # 检查事件
19     for event in pygame.event.get():
20         if event.type == pygame.QUIT:  # 如果点击关闭窗口,则退出
21             sys.exit()
22  
23     ballrect = ballrect.move(speed)  # 移动小球
24     # 碰到左右边缘
25     if ballrect.left < 0 or ballrect.right > width:
26         speed[0] = -speed[0]
27     # 碰到上下边缘
28     if ballrect.top < 0 or ballrect.bottom > height:
29         speed[1] = -speed[1]
30  
31     screen.fill(color)  # 填充颜色
32     screen.blit(ball, ballrect)  # 将图片画到窗口上
33     pygame.display.flip()  # 更新全部显示
34 
35 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 

标签:25,ball,窗口,ballrect,Pythontext,pygame,speed
From: https://www.cnblogs.com/kimtaeyoan/p/16960555.html

相关文章

  • Pythontext_8
    1实例一:创建并打开记录蚂蚁庄园动态的文件2print("\n","="*10,"蚂蚁庄园动态","="*10)3file=open('message.txt','w')#创建或打开保存蚂蚁庄园动态信息......
  • Pythontext_4
    1实例一:使用字符串拼接输出一个关于程序员的笑话2programmer_1='程序员甲:搞IT太幸苦了,我想换行……怎么办?'3programmer_2='程序员乙:敲一下回车键'4print(progr......
  • Pythontext_3
    1实例一:输出每日一帖2importdatetime#导入日期时间表3#定义一个列表4mot=["今天星期一:\n坚持下去不是因为我很坚强,而是......