目录
一、基本框架
二、代码段
1、附注释
2、无注释
三、效果展示
备注:pygame - 图片移动优化中未对属性和方法进行封装,本篇文章将引入面向对象思想
一、基本框架
import sys
import time
import pygame
pygame.init()
SCREEN_WIDTH = 680
SCREEN_LENGTH = 1000
BG_COLOR = pygame.Color(0,0,0)
class picture():
def __init__(self,left,top):
def display(self):
def move(self):
class main():
screen = None
my_picture = None
def __init__(self):
pass
def getEvent(self):
def start(self):
def end(self):
if __name__ == '__main__':
main().start()
二、代码段
1、附注释
import sys
import time
import pygame
pygame.init()
# 使用pygame之前必须初始化
SCREEN_WIDTH = 680 # 主屏窗口宽度
SCREEN_LENGTH = 1000 # 主屏窗口长度
BG_COLOR = pygame.Color(0,0,0) # 主屏窗口背景颜色
class picture(): # 图片类
def __init__(self,left,top):
self.img = pygame.image.load("t02_img.png") # 加载本地图片
self.rect = self.img.get_rect() # 获取图片区域
self.rect.left = left # 设置显示位置
self.rect.top = top # 设置显示位置
self.speed = 5 # 移动速度
self.stop = True # 图片移动开关
self.direction = 'U' # 图片移动方向
def display(self): # 图片显示函数
main.screen.blit(self.img,self.rect) # 传送
def move(self): # 图片移动函数
# 说明:图片移动实质是改变rect.top或rect.left,并确保图片不会移出主屏窗口
if self.rect.top - self.speed >= 0 and self.direction == 'U':
self.rect.top -= self.speed
elif self.rect.top + self.rect.height + self.speed <= SCREEN_WIDTH and self.direction == 'D':
self.rect.top += self.speed
elif self.rect.left - self.speed >= 0 and self.direction == 'L':
self.rect.left -= self.speed
elif self.rect.left + self.rect.width + self.speed <= SCREEN_LENGTH and self.direction == 'R':
self.rect.left += self.speed
class main(): # 主类
screen = None # 接收pygame.display.set_mode()的返回值
my_picture = None # picture的实例
def __init__(self):
pass
def getEvent(self): # 获取事件函数
for event in pygame.event.get(): # 循环获取事件并监听事件状态
if event.type == pygame.KEYDOWN: # 按下了按键
if event.key == pygame.K_UP: # 上
main.my_picture.stop = False
main.my_picture.direction = 'U'
elif event.key == pygame.K_DOWN: # 下
main.my_picture.stop = False
main.my_picture.direction = 'D'
elif event.key == pygame.K_LEFT: # 左
main.my_picture.stop = False
main.my_picture.direction = 'L'
elif event.key == pygame.K_RIGHT: # 右
main.my_picture.stop = False
main.my_picture.direction = 'R'
elif event.type == pygame.KEYUP: # 松开了按键
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
main.my_picture.stop = True
elif event.type == pygame.QUIT: # 点击了关闭按钮
self.end()
def start(self):
main.screen = pygame.display.set_mode((SCREEN_LENGTH, SCREEN_WIDTH))
# 说明:set_mode()可以短时间显示主屏窗口
pygame.display.set_caption("图片移动优化测试")
# 说明:若不设置窗口标题,则窗口标题默认为“pygame window”
main.my_picture = picture(500,340)
# 说明:实例化picture,并指定显示位置
while True:
# 说明:while True循环可以使得主屏窗口得以保留
time.sleep(0.02) # 使得循环速度变慢,从而使得图片移动速度变慢
main.screen.fill(BG_COLOR) # 填充
self.getEvent() # 获取事件
main.my_picture.display() # 图片显示
if not self.my_picture.stop:
self.my_picture.move() # 图片移动
pygame.display.update() # 更新
def end(self):
print('感谢体验图片移动优化测试!')
exit() # 退出程序
if __name__ == '__main__':
main().start()
2、无注释
import sys
import time
import pygame
pygame.init()
SCREEN_WIDTH = 680
SCREEN_LENGTH = 1000
BG_COLOR = pygame.Color(0,0,0)
class picture():
def __init__(self,left,top):
self.img = pygame.image.load("t02_img.png")
self.rect = self.img.get_rect()
self.rect.left = left
self.rect.top = top
self.speed = 5
self.stop = True
self.direction = 'U'
def display(self):
main.screen.blit(self.img,self.rect)
def move(self):
if self.rect.top - self.speed >= 0 and self.direction == 'U':
self.rect.top -= self.speed
elif self.rect.top + self.rect.height + self.speed <= SCREEN_WIDTH and self.direction == 'D':
self.rect.top += self.speed
elif self.rect.left - self.speed >= 0 and self.direction == 'L':
self.rect.left -= self.speed
elif self.rect.left + self.rect.width + self.speed <= SCREEN_LENGTH and self.direction == 'R':
self.rect.left += self.speed
class main():
screen = None
my_picture = None
def __init__(self):
pass
def getEvent(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
main.my_picture.stop = False
main.my_picture.direction = 'U'
elif event.key == pygame.K_DOWN:
main.my_picture.stop = False
main.my_picture.direction = 'D'
elif event.key == pygame.K_LEFT:
main.my_picture.stop = False
main.my_picture.direction = 'L'
elif event.key == pygame.K_RIGHT:
main.my_picture.stop = False
main.my_picture.direction = 'R'
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
main.my_picture.stop = True
elif event.type == pygame.QUIT:
self.end()
def start(self):
main.screen = pygame.display.set_mode((SCREEN_LENGTH, SCREEN_WIDTH))
pygame.display.set_caption("图片移动优化测试")
main.my_picture = picture(500,340)
while True:
time.sleep(0.02)
main.screen.fill(BG_COLOR)
self.getEvent()
main.my_picture.display()
if not self.my_picture.stop:
self.my_picture.move()
pygame.display.update()
def end(self):
print('感谢体验图片移动优化测试!')
exit()
if __name__ == '__main__':
main().start()
三、效果展示
pygame图片移动优化(面向对象)测试 20230108