Wednesday, 9 December 2015

Pygame : Working with keyboard events

In this post, I am going to explain how to use key board UP, DOWN, LEFT and RIGHT arrow buttons to move a soldier to up, down left and right.

Step 1: First we should get notification, whenever key is pressed down and released. Event queue gets pygame.KEYDOWN, pygame.KEYUP events whenever key is pressed and released.

for event in pygame.event.get():
 if event.type == pygame.QUIT:
     dead = True

 if event.type == pygame.KEYDOWN:
    # Write logic

    if event.type == pygame.KEYUP:
    # Write logic

Step 2: We need to identify whether user pressed Left Arrow, Right Arrow, Down Arrow, UP Arrow. pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, and pygame.K_DOWN constants are used to represent these keyboard buttons.

Following are the constants used to represent keys in the keyboard.
constant
ASCII
Key name
K_BACKSPACE
\b
backspace
K_TAB
\t
tab
K_CLEAR

clear
K_RETURN
\r
return
K_PAUSE

pause
K_ESCAPE
^[
escape
K_SPACE

space
K_EXCLAIM
!
exclaim
K_QUOTEDBL
"
quotedbl
K_HASH
#
hash
K_DOLLAR
$
dollar
K_AMPERSAND
&
ampersand
K_QUOTE

quote
K_LEFTPAREN
(
left parenthesis
K_RIGHTPAREN
)
right parenthesis
K_ASTERISK
*
asterisk
K_PLUS
+
plus sign
K_COMMA
,
comma
K_MINUS
-
minus sign
K_PERIOD
.
period
K_SLASH
/
forward slash
K_0
0
0
K_1
1
1
K_2
2
2
K_3
3
3
K_4
4
4
K_5
5
5
K_6
6
6
K_7
7
7
K_8
8
8
K_9
9
9
K_COLON
:
colon
K_SEMICOLON
;
semicolon
K_LESS
< 
less-than sign
K_EQUALS
=
equals sign
K_GREATER
> 
greater-than sign
K_QUESTION
?
question mark
K_AT
@
at
K_LEFTBRACKET
[
left bracket
K_BACKSLASH
\
backslash
K_RIGHTBRACKET
]
right bracket
K_CARET
^
caret
K_UNDERSCORE
_
underscore
K_BACKQUOTE
`
grave
K_a
a
a
K_b
b
b
K_c
c
c
K_d
d
d
K_e
e
e
K_f
f
f
K_g
g
g
K_h
h
h
K_i
i
i
K_j
j
j
K_k
k
k
K_l
l
l
K_m
m
m
K_n
n
n
K_o
o
o
K_p
p
p
K_q
q
q
K_r
r
r
K_s
s
s
K_t
t
t
K_u
u
u
K_v
v
v
K_w
w
w
K_x
x
x
K_y
y
y
K_z
z
z
K_DELETE

delete
K_KP0

keypad 0
K_KP1

keypad 1
K_KP2

keypad 2
K_KP3

keypad 3
K_KP4

keypad 4
K_KP5

keypad 5
K_KP6

keypad 6
K_KP7

keypad 7
K_KP8

keypad 8
K_KP9

keypad 9
K_KP_PERIOD
.
keypad period
K_KP_DIVIDE
/
keypad divide
K_KP_MULTIPLY
*
keypad multiply
K_KP_MINUS
-
keypad minus
K_KP_PLUS
+
keypad plus
K_KP_ENTER
\r
keypad enter
K_KP_EQUALS
=
keypad equals
K_UP

up arrow
K_DOWN

down arrow
K_RIGHT

right arrow
K_LEFT

left arrow
K_INSERT

insert
K_HOME

home
K_END

end
K_PAGEUP

page up
K_PAGEDOWN

page down
K_F1

F1
K_F2

F2
K_F3

F3
K_F4

F4
K_F5

F5
K_F6

F6
K_F7

F7
K_F8

F8
K_F9

F9
K_F10

F10
K_F11

F11
K_F12

F12
K_F13

F13
K_F14

F14
K_F15

F15
K_NUMLOCK

numlock
K_CAPSLOCK

capslock
K_SCROLLOCK

scrollock
K_RSHIFT

right shift
K_LSHIFT

left shift
K_RCTRL

right ctrl
K_LCTRL

left ctrl
K_RALT

right alt
K_LALT

left alt
K_RMETA

right meta
K_LMETA

left meta
K_LSUPER

left windows key
K_RSUPER

right windows key
K_MODE

mode shift
K_HELP

help
K_PRINT

print screen
K_SYSREQ

sysrq
K_BREAK

break
K_MENU

menu
K_POWER

power
K_EURO

euro



# import pygame
import pygame

# initialize game engine
pygame.init()

window_width=500
window_height=500

animation_increment=10
clock_tick_rate=10

# Open a window
size = (window_width, window_height)
screen = pygame.display.set_mode(size)

# Set title to the window
pygame.display.set_caption("Hello World")

dead=False

# Initialize values for color (RGB format)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
BLACK=(0,0,0)

# Moving object
x_direction=0
y_direction=0
move_x_left=-3
move_x_right=3
move_y_down=3
move_y_up=-3

x_currentPos = 0
y_currentPos = 0

clock = pygame.time.Clock()

# Constants related to soldiers
soldier_head_width=10
soldier_head_height=10
soldier_start_x_position_incr=5

def draw_soldier(soldier_start_x_position, soldier_start_y_position):
    # Soldier head
    pygame.draw.ellipse(screen, BLACK, [soldier_start_x_position+soldier_start_x_position_incr,soldier_start_y_position,soldier_head_width,soldier_head_height], 0)

    # Soldier Body
    pygame.draw.line(screen, RED, [10+soldier_start_x_position, 9+soldier_start_y_position], [10+soldier_start_x_position, 20+soldier_start_y_position], 3)

    # Soldier hands
    pygame.draw.line(screen, RED, [10+soldier_start_x_position, 9+soldier_start_y_position], [5+soldier_start_x_position, 15+soldier_start_y_position], 3)
    pygame.draw.line(screen, RED, [10+soldier_start_x_position, 9+soldier_start_y_position], [15+soldier_start_x_position, 15+soldier_start_y_position], 3)

    #Soldier legs
    pygame.draw.line(screen, BLACK, [10+soldier_start_x_position, 20+soldier_start_y_position], [5+soldier_start_x_position, 26+soldier_start_y_position], 3)
    pygame.draw.line(screen, BLACK, [10+soldier_start_x_position, 20+soldier_start_y_position], [15+soldier_start_x_position, 26+soldier_start_y_position], 3)


while(dead==False):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_direction=move_x_left
            elif event.key == pygame.K_RIGHT:
                x_direction=move_x_right
            elif event.key == pygame.K_DOWN:
                y_direction = move_y_down
            elif event.key == pygame.K_UP:
                y_direction = move_y_up

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_direction=0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_direction=0

        screen.fill(WHITE)
        x_currentPos += x_direction
        y_currentPos += y_direction

        if x_currentPos > window_width :
            x_currentPos=0

        if x_currentPos < 0 :
            x_currentPos = window_width-10

        if y_currentPos > window_height:
            y_currentPos=10

        if y_currentPos < 0:
            y_currentPos=window_height-10

        draw_soldier(x_currentPos, y_currentPos)


    pygame.display.flip()
    clock.tick(clock_tick_rate)





Previous                                                 Next                                                 Home

No comments:

Post a Comment