📜  Pygame——输入处理

📅  最后修改于: 2022-05-13 01:55:06.459000             🧑  作者: Mango

Pygame——输入处理

Pygame是一组用于编写视频游戏的跨平台Python模块。它包括旨在与Python编程语言一起使用的计算机图形和声音库。

Python中的sys模块提供了各种函数和变量,用于操作Python运行时环境的不同部分。它允许在解释器上进行操作,因为它提供了对与解释器进行强烈交互的变量和函数的访问。

处理键盘输入

处理键盘输入的基本步骤:

  • 导入所需的库。
  • 使用 pygame 的 display.set_mode() 方法创建一个显示表面对象。
  • 加载图像/对象。
  • 创建一个点击事件,即KEYDOWN
  • 定义所有事件键并执行任务。
  • 创建一个暂停事件,即KEYUP
  • 使用 pygame 显示表面对象的 blit() 方法将文本表面对象复制到显示表面对象。
  • 使用 pygame 的 display.update() 方法在 pygame 窗口上显示显示表面对象。

例子:

Python3
# importing all the required libraries
import pygame
from pygame.locals import *
from sys import exit
 
# initiating pygame library to use it's
# functions
pygame.init()
 
# declaring windows/surface width and height
size = width, height = 740, 480
screen = pygame.display.set_mode(size)
 
# loads a new image from a file and convert()
# will create a copy of image on surface
img = pygame.image.load("char.png").convert()
 
# declaring value to variables
x, y = 0, 0
move_x, move_y = 0, 0
 
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # pygame.QUIT deactivates pygame
            exit()
            # exit() is sys function used to
            # kill the program
             
     # KEYDOWN event will be triggered everytime
    # we press a button
    if event.type == KEYDOWN: 
       
        if event.key == K_LEFT: 
            move_x = -0.3  # object moves -0.3 towards x axis
            print("pressed LEFT")
        elif event.key == K_RIGHT: 
            move_x = +0.3  # object moves 0.3 towards x axis
            print("pressed RIGHT")
        elif event.key == K_UP:
            move_y = -0.3  # object moves -0.3 towards y axis
            print("pressed UP")
        elif event.key == K_DOWN:
            move_y = +0.3  # object moves 0.3 towards y axis
            print("pressed DOWN")
             
        # K_LCTRL event will be triggered everytime we
        # press left CTRL button
        elif event.key == K_LCTRL:
           
            # declaring new image file to update image
            # everytime left CTRL is pressed
            img = pygame.image.load("char1.png")
            pygame.display.update()  # update image
        elif event.key == K_BACKSPACE:
           
            # this the default file we declared in start
            # and it will restore it everytime we press
            # backspace
            img = pygame.image.load("char.png")
            pygame.display.update()  # update image
             
     # it will get triggered when left key is released
    if event.type == KEYUP:
        if event.key == K_LEFT:
            move_x = 0  # movement stops
        elif event.key == K_RIGHT: 
            move_x = 0  # movement stops
        elif event.key == K_UP:
            move_y = 0  # movement stops
        elif event.key == K_DOWN: 
            move_y = 0  # movement stops
            """KEYUP event will be triggered when the release the keys
            and x,y coordinates will not change anymore"""
 
    x += move_x
    y += move_y
    # updating coordinate values of x,y
    screen.fill((255, 255, 255))
     
    # the function will fill the background with white color
    screen.blit(img, (x, y))
     
    # blit() function will copy image file to x,y coordinates.
    pygame.display.update()
    # draw the objects on screen


Python3
# importing all the required libraries
import pygame
from pygame.locals import *
from sys import exit
 
# initiating pygame library to use it's functions
pygame.init()
 
# declaring windows/surface width and height
size = width, height = 740, 480
screen = pygame.display.set_mode(size)
 
 
# loads a new image from a file and convert()
# will create a copy of image on surface
img = pygame.image.load("char.png").convert()
 
# declaring value to variables
clicking = False
right_clicking = False
middle_click = False
 
while True:
    mx, my = pygame.mouse.get_pos()  # gets mouse x,y coordinates
    location = [mx, my]
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           
            # pygame.QUIT deactivates pygame
            exit()
            # exit() is sys function used to kill the program
 
    # MOUSEBUTTONDOWN event is triggered when a button is pressed
    if event.type == MOUSEBUTTONDOWN:
       
        # returns true when mouse left button is clicked
        if event.button == 1: 
            clicking = True
             
            # declaring new image file to update image
            # everytime left button clicking is true
            img = pygame.image.load("char1.png")
            pygame.display.update()  # update image
         
        # returns true when mouse right button is clicked
        if event.button == 3: 
            right_clicking = True
             
            # declaring new image file to update image
            # everytime right button is clicked
            img = pygame.image.load("char.png")
            pygame.display.update()  # update image
             
        # returns true when mouse middle button is clicked
        if event.button == 2: 
            middle_click = middle_click
             
            # rescale image when middle button clicking is true
            img = pygame.transform.scale(img, (100, 100))
            pygame.display.update()  # update image
 
    # MOUSEBUTTONUP is triggered when mouse button
    # is released(not clicked)
    if event.type == MOUSEBUTTONUP:
        if event.button == 1:
            clicking = False
 
    screen.fill((255, 255, 255))
     
    # the function will fill the background
    # with white color
    screen.blit(img, (location[0], location[1]))
     
    # blit() function will copy image file
    # to x,y coordinates.
    pygame.display.update()
    # draw the objects on screen


输出:

处理鼠标输入:

处理鼠标输入的基本步骤:

  • 导入所需的库。
  • 使用 pygame 的 display.set_mode() 方法创建一个显示表面对象。
  • 加载图像/对象。
  • 创建一个单击事件,即 MOUSEBUTTONDOWN。
  • 定义所有事件键并执行任务。
  • 创建一个暂停事件,即 MOUSEBUTTONUP。
  • 使用 pygame 显示表面对象的 blit() 方法将文本表面对象复制到显示表面对象。
  • 使用 pygame 的 display.update() 方法在 pygame 窗口上显示显示表面对象。

例子:

Python3

# importing all the required libraries
import pygame
from pygame.locals import *
from sys import exit
 
# initiating pygame library to use it's functions
pygame.init()
 
# declaring windows/surface width and height
size = width, height = 740, 480
screen = pygame.display.set_mode(size)
 
 
# loads a new image from a file and convert()
# will create a copy of image on surface
img = pygame.image.load("char.png").convert()
 
# declaring value to variables
clicking = False
right_clicking = False
middle_click = False
 
while True:
    mx, my = pygame.mouse.get_pos()  # gets mouse x,y coordinates
    location = [mx, my]
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           
            # pygame.QUIT deactivates pygame
            exit()
            # exit() is sys function used to kill the program
 
    # MOUSEBUTTONDOWN event is triggered when a button is pressed
    if event.type == MOUSEBUTTONDOWN:
       
        # returns true when mouse left button is clicked
        if event.button == 1: 
            clicking = True
             
            # declaring new image file to update image
            # everytime left button clicking is true
            img = pygame.image.load("char1.png")
            pygame.display.update()  # update image
         
        # returns true when mouse right button is clicked
        if event.button == 3: 
            right_clicking = True
             
            # declaring new image file to update image
            # everytime right button is clicked
            img = pygame.image.load("char.png")
            pygame.display.update()  # update image
             
        # returns true when mouse middle button is clicked
        if event.button == 2: 
            middle_click = middle_click
             
            # rescale image when middle button clicking is true
            img = pygame.transform.scale(img, (100, 100))
            pygame.display.update()  # update image
 
    # MOUSEBUTTONUP is triggered when mouse button
    # is released(not clicked)
    if event.type == MOUSEBUTTONUP:
        if event.button == 1:
            clicking = False
 
    screen.fill((255, 255, 255))
     
    # the function will fill the background
    # with white color
    screen.blit(img, (location[0], location[1]))
     
    # blit() function will copy image file
    # to x,y coordinates.
    pygame.display.update()
    # draw the objects on screen

输出: