python pygame 图形的移动

2022/4/28 22:42:47

本文主要是介绍python pygame 图形的移动,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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 += 10
                xx += 10
                x0 += 10
                x1 += 10
                x2 += 10
                x3 += 10
                x4 += 10
                x5 += 10
                x6 += 10
                x7 += 10



            elif event.key == pygame.K_LEFT:
                print('左')
                x -= 10
                xx -= 10
                x0 -= 10
                x1 -= 10
                x2 -= 10
                x3 -= 10
                x4 -= 10
                x5 -= 10
                x6 -= 10
                x7 -= 10

            elif event.key == pygame.K_UP:
                print('上')
                y += 10
                yy += 10
                y0 += 10
                y1 += 10
                y2 += 10
                y3 += 10
                y4 += 10
                y5 += 10
                y6 += 10
                y7 += 10

            elif event.key == pygame.K_DOWN:
                print('下')
                y -= 10
                yy -= 10
                y0 -= 10
                y1 -= 10
                y2 -= 10
                y3 -= 10
                y4 -= 10
                y5 -= 10
                y6 -= 10
                y7 -= 10

    pygame.draw.line(screen, (1, 1, 1), (x0,y0 ), (x1,y1), 5)
    pygame.draw.line(screen, (1, 1, 1), (x2,y2), ( x3,y3 ), 5)

    pygame.draw.line(screen, (1, 1, 1), (x4,y4), (x5,y5), 5)
    pygame.draw.line(screen, (1, 1, 1), (x6,y6), (x7,y7), 5)

    pygame.draw.rect(screen, (1, 1, 1), (x, y,w, w) ,3)
    pygame.draw.rect(screen, (1, 1, 1), (xx, yy, w1, w1), 3)

    screen.blit(screen, (0, 0))
    # 定义频率
    clock = pygame.time.Clock()
    # 设定刷新帧率
    clock.tick(60) # 越大刷新的越快
    pygame.display.update()


这篇关于python pygame 图形的移动的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程