IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python游戏开发入门课程(pygame)学习笔记(01) -> 正文阅读

[Python知识库]Python游戏开发入门课程(pygame)学习笔记(01)

import sys,pygame
pygame.init()
size=w,h=800,400
speed=[1,1]
color=0,0,0
fps=100
fclock=pygame.time.Clock()
screen =pygame.display.set_mode(size)

pygame.display.set_caption("pygame游戏之旅")
image=pygame.image.load(r"D:\1.0\4.png")
#image = pygame.transform.scale(image, (80, 40))
imagerect=image.get_rect()

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
             if event.key==pygame.K_LEFT:
                 speed[0]=speed[0]-1 if speed[0]==0 else (abs(speed[0])-1)*int(speed[0]/abs(speed[0]))
             elif event.key==pygame.K_RIGHT:
                 speed[0] = speed[0]+1 if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
             elif event.key == pygame.K_DOWN:
                 speed[1] = speed[1] +1if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
             elif event.key == pygame.K_UP:
                 speed[1] = speed[1]-1 if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
    imagerect=imagerect.move(speed[0], speed[1])
    if imagerect.left<0 or imagerect.right>w:
        speed[0]=-speed[0]
    if imagerect.top<0 or imagerect.bottom>h:
        speed[1]=-speed[1]
    screen.fill(color)
    screen.blit(image, imagerect)
    fclock.tick(fps)
    pygame.display.update()

第一部分pygame的引用和初始化

import sys,pygame
pygame.init()

初始化pygame建立一个框架

第二部分窗体大小和标题的设置

screen =pygame.display.set_mode(size)

pygame.display.set_caption("pygame游戏之旅")

第三部分事件的监听

 for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()

游戏中不断监听事件进行反馈

第四部分图片的导入和rect的使用

image=pygame.image.load(r"D:\1.0\4.png")
imagerect=image.get_rect()
screen.blit(image, imagerect)

其中bilt函数将图像文件导入框架中,使图像可以在屏幕上显示和移动

第六部分移动函数和背景颜色的填充

imagerect=imagerect.move(speed[0], speed[1])
    if imagerect.left<0 or imagerect.right>w:
        speed[0]=-speed[0]
    if imagerect.top<0 or imagerect.bottom>h:
        speed[1]=-speed[1]
size=w,h=800,400
speed=[1,1]
color=0,0,0
screen.fill(color)

包含移动函数和碰撞检测。当小球跃出屏幕外时,可以返回。背景填充的设定使用fill

相当相当于用坐标系来表示这个图形。当小球的左边界小于零或者右边界大于所规定的坐标坐标系时,它会返回。其中移动函数。当小球的上边界小于零,或者它的下部分部分大于小图像的宽度时。小球便会返回,其中pygame的Move函数规定了它的左移动和上移动。

第七部分小球的键盘,监听和移动。

        elif event.type == pygame.KEYDOWN:
             if event.key==pygame.K_LEFT:
                 speed[0]=speed[0]-1 if speed[0]==0 else (abs(speed[0])-1)*int(speed[0]/abs(speed[0]))
             elif event.key==pygame.K_RIGHT:
                 speed[0] = speed[0]+1 if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
             elif event.key == pygame.K_DOWN:
                 speed[1] = speed[1] +1if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
             elif event.key == pygame.K_UP:
                 speed[1] = speed[1]-1 if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
    imagerect=imagerect.move(speed[0], speed[1])

其中我们对事件的键盘进行一个监听。规定左移动和上移动的移动范围为一个像素。

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-08-06 10:40:12  更:2022-08-06 10:43:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 13:25:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码