import sys
import pygame
#使用pygame之前必须初始化
pygame.init()
#设置主屏窗口
screen = pygame.display.set_mode((400,400))
#设置窗口标题
pygame.display.set_caption('')
# source = pygame.Surface((50, 50), flags=pygame.HWSURFACE)
# 如果没有下列主循环代码,运行结果会一闪而过
x0, y0 = 0,0
x1, y1 = 50, 50
x2, y2 = 150, 150
x3, y3 = 200, 200
x4, y4 = 0, 200
x5, y5 = 50, 150
x6, y6 = 200, 0
x7, y7 = 150, 50
w,w1 = 200,100
x,y = 0,0
xx,yy = 50,50
# 更新屏幕内容
pygame.display.flip()
while True:
# #填充主窗口的背景颜色,参数值RGB(颜色元组)
screen.fill((255, 255, 255))
# 循环获取事件,监听事件
for event in pygame.event.get():
# 判断用户是否点了关闭按钮
if event.type == pygame.QUIT:
#卸载所有模块
pygame.quit()
#终止程序
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
print('右')
x += 100
xx += 100
x0 += 100
x1 += 100
x2 += 100
x3 += 100
x4 += 100
x5 += 100
x6 += 100
x7 += 100
elif event.key == pygame.K_LEFT:
print('左')
x -= 100
xx -= 100
x0 -= 100
x1 -= 100
x2 -= 100
x3 -= 100
x4 -= 100
x5 -= 100
x6 -= 100
x7 -= 100
elif event.key == pygame.K_UP:
print('上')
y += 100
yy += 100
y0 += 100
y1 += 100
y2 += 100
y3 += 100
y4 += 100
y5 += 100
y6 += 100
y7 += 100
elif event.key == pygame.K_DOWN:
print('下')
y -= 100
yy -= 100
y0 -= 100
y1 -= 100
y2 -= 100
y3 -= 100
y4 -= 100
y5 -= 100
y6 -= 100
y7 -= 100
pygame.draw.line(screen, (1, 1, 1), (x0,y0 ), (x1,y1), 2)
pygame.draw.line(screen, (1, 1, 1), (x2,y2), ( x3,y3 ), 2)
pygame.draw.line(screen, (1, 1, 1), (x4,y4), (x5,y5), 2)
pygame.draw.line(screen, (1, 1, 1), (x6,y6), (x7,y7), 2)
pygame.draw.rect(screen, (1, 1, 1), (x, y,w, w) ,2)
pygame.draw.rect(screen, (1, 1, 1), (xx, yy, w1, w1), 2)
screen.blit(screen, (0, 0))
# 定义频率
clock = pygame.time.Clock()
# 设定刷新帧率
clock.tick(60) # 越大刷新的越快
pygame.display.update()