代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import pygame
from pygame.locals import *
from sys import exit

background_image = 'img/bk.jpeg'
screen = pygame.display.set_mode((650, 443), 0, 32)
background = pygame.image.load(background_image).convert()

x, y = 0, 0
move_x, move_y = 0, 0
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                move_x = -1
            elif event.key == K_RIGHT:
                move_x = 1
            elif event.key == K_UP:
                move_y = -1
            elif event.key == K_DOWN:
                move_y = 1
        elif event.type == KEYUP:
            move_x = 0
            move_y = 0

    x += move_x
    y += move_y

    screen.fill((0, 0, 0))
    screen.blit(background, (x,y))

    pygame.display.update()

按下上下左右键,就可以移动对应图片:

pygame入门 Deme_02-键盘

相关文章:

  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2021-04-29
  • 2022-02-11
  • 2021-05-20
  • 2022-12-23
  • 2021-12-15
猜你喜欢
  • 2022-01-13
  • 2021-07-01
  • 2021-07-20
  • 2022-12-23
  • 2021-04-18
  • 2021-05-18
  • 2022-12-23
相关资源
相似解决方案