利用PyGame进行初步游戏界面尝试

import pygame
import sys

def main():
    #初始化
    pygame.init()
    pygame.display.set_caption("my little game")

    #创建屏幕 240*180
    screen = pygame.display.set_mode((1024,768))
    screen.fill((255,255,255))
    imagel_1 = pygame.image.load("C:\\Users\\Administrator\\Desktop\\python-lianxi\\move_game\\one.jpg")
    imagel_2 = pygame.image.load("C:\\Users\\Administrator\\Desktop\\python-lianxi\\move_game\\two.jpg")
    imagel_3 = pygame.image.load("C:\\Users\\Administrator\\Desktop\\python-lianxi\\move_game\\three.jpg")
    imagel_4 = pygame.image.load("C:\\Users\\Administrator\\Desktop\\python-lianxi\\move_game\\four.jpg")
    #缩小到50*50
    imagel_1 = pygame.transform.scale(imagel_1,(50,50))
    imagel_2 = pygame.transform.scale(imagel_2,(50,50))
    imagel_3 = pygame.transform.scale(imagel_3,(50,50))
    imagel_4 = pygame.transform.scale(imagel_4,(50,50))
    x = 50
    y = 50
    screen.blit(imagel_1,(x,50))
    pygame.display.flip()

    running  = True

    #游戏主循环
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()  #关闭并退出程序
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    x = x + 40
                    screen.fill((255,255,255))
                    screen.blit(imagel_1,(x,y))
                elif event.key == pygame.K_LEFT:
                    x = x - 40
                    screen.fill((255,255,255))
                    screen.blit(imagel_2,(x,y))
                elif event.key == pygame.K_UP:
                    y = y - 30
                    screen.fill((255,255,255))
                    screen.blit(imagel_3,(x,y))
                elif event.key == pygame.K_DOWN:
                    y = y + 30
                    screen.fill((255,255,255))
                    screen.blit(imagel_4,(x,y))
                pygame.display.flip()
        
if __name__ == "__main__":
    main()
 

你可能感兴趣的:(py)