当前位置:网站首页>20 pygame模块制作一个跳跃的小球游戏
20 pygame模块制作一个跳跃的小球游戏
2022-06-21 16:02:00 【So come on】
pygame模块制作一个跳跃的小球游戏
小球图形
import sys # 导入sys模块
import pygame # 导入pygame模块
pygame.init() # 初始化pygame
size = width, height = 640, 480 # 设置窗口
screen = pygame.display.set_mode(size) # 显示窗口
# color = (0, 0, 0) # 设置颜色
color = (255, 0, 0) # 设置颜色
ball = pygame.image.load("ball.png") # 加载图片
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() # 更新全部显示
显示结果

边栏推荐
- 牛客网:大数加法
- Design and implementation of face verification system for floating population management
- Serialization and deserialization of binary tree
- [ros2 basics] Introduction to navigation2 navigation system
- Résolution des erreurs signalées par qtcreator
- 很多軟件公司,其實都是“笑話”
- Oracle中实现获取指定行内容——Rownum和Row_number()
- Application of integrated servo motor and Schneider PLC tm241cec24t under CANopen Protocol
- Yaml data driven demo
- Cloud native monitoring system - Nightingale's recent list of new functions to solve multiple production pain points
猜你喜欢

exness:美国通货膨胀影响太大,美联储大佬纷纷表态
![[ROS2 基础] Navigation2 导航系统介绍](/img/7f/ad6af972460d7a78fb28d9b4c24477.png)
[ROS2 基础] Navigation2 导航系统介绍

一体化伺服电机与施耐德PLC TM241CEC24T在Canopen协议下的应用

使用 Guzzle 中间件进行优雅的请求重试

新增Razor组件支持代理连接RDP,JumpServer堡垒机v2.23.0发布

Use picgo core and Alibaba cloud to automatically upload typera pictures

阿里云服务器+宝塔面板+无域名部署web项目

Pytest框架

疫情数据对应的大陆和全球的矢量数据下载,基于geojson转shp

Comparison of UDP and TCP
随机推荐
从北京“润”到芝加哥,工程师宝玉“滋润”成长的秘诀
Reinforcement learning introductory project spinning up (1) installation
4. construction [LR (1) analysis table (including construction project specification family)]
Online shopping website (final assignment)
Machine learning model monitoring (Apria)
面向流动人口管理的人脸验证系统设计及实现 论文+答辩PPT+项目工程文件
Unittest framework
Interface automatic encryption and decryption
第13周总结博客(校历第15周)动态规划总结
牛客网:验证IP地址
LeetCode 25:K个一组翻转链表
UDP和TCP的对比
快速排序简单思路及程序
Alibaba cloud server + pagoda panel + no domain name deployment web project
二叉树的层序遍历
接口自动化加解密
[observation] Microsoft's "cloud + end" comprehensive innovation makes hybrid cloud simpler, more flexible and more secure
建立自己的网站(11)
Elegant request retry using guzzle Middleware
How to judge DNS resolution failure? How to resolve DNS resolution errors?