pygame之Surface对象以及rect属性
1、创建Surface对象及打印rect属性
import pygame from pygame.locals import * pygame.init() # 初始化pygame screen = pygame.display.set_mode((200, 200)) # 创建窗口 face = pygame.Surface((50, 50)) # 创建一个50x50的平面 face.fill(color='pink') # 填充橙色 rect = face.get_rect() # 获取面积和X,Y坐标 print(f'top:{rect.top}') print(f'topleft:{rect.topleft}') print(f'topright:{rect.topright}') print(f'bottom:{rect.bottom}') print(f'bottomleft:{rect.bottomleft}') print(f'bottomright:{rect.bottomright}') print(f'left:{rect.left}') print(f'right:{rect.right}') print(f'midtop:{rect.midtop}') print(f'midbottom:{rect.midbottom}') print(f'midleft:{rect.midleft}') print(f'midright:{rect.midright}') print(f'center:{rect.center}') print(f'centerx:{rect.centerx}') print(f'centery:{rect.centery}') print(f'w:{rect.w}') print(f'width:{rect.width}') print(f'h:{rect.h}') print(f'height:{rect.height}') print(f'x:{rect.x}') print(f'y:{rect.y}') print(f'size:{rect.size}') running = True while running: for event in pygame.event.get(): # 获取所有事件 if event.type == KEYDOWN: # 获取按钮按下事件 if event.key == K_ESCAPE: # 如果是按下了ESC键 running = False if event.type == QUIT: # 如果是QUIT事件,如点击关闭窗口按钮 running = False pygame.quit() # 退出pygame
2、显示surface对象
import pygame from pygame.locals import * pygame.init() # 初始化pygame screen = pygame.display.set_mode((300, 300)) # 创建窗口 screen.fill(color='white') # 填充颜色 face = pygame.Surface((50, 50)) # 创建一个50x50的平面 face.fill(color='orange') # 填充颜色 running = True while running: for event in pygame.event.get(): # 获取所有事件 if event.type == KEYDOWN: # 获取按钮按下事件 if event.key == K_ESCAPE: # 如果是按下了ESC键 running = False if event.type == QUIT: # 如果是QUIT事件,如点击关闭窗口按钮 running = False screen.blit(face, (100, 100)) # 放置平面face pygame.display.flip() # 刷新整个平面 或者 pygame.display.update() pygame.quit() # 退出pygame
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示