Table of contents
1.
Introduction
2.
Pygame
2.1.
OUTPUT
3.
Frequently Asked Questions.
3.1.
How do you create objects in Pygame?
3.2.
What are the arrow keys called in Python?
3.3.
How do you input a keyboard in Python?
3.4.
In Python Pygame, how do you draw a circle?
3.5.
How do you press arrow keys in Python?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Drawing the design using arrow keys in pygame

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pygame is a multimedia toolkit for making games and other multimedia applications based on Python. In this lesson, we'll use the pygame module to draw various forms on the screen while considering their height, width, and location within the pygame window.

SDL stands for Simple DirectMedia Layer, and Pygame is a Python wrapper for it. SDL allows you to utilize any platform to access your system's multimedia hardware, such as sound, video, mouse, keyboard, and joystick. The pygame project was created to replace the now-defunct PySDL project. Because SDL and pygame are cross-platform, you can create games and rich multimedia Python programs on any platform that supports them!

Pygame

Pygame is a Python library that focuses on game development and design. Pygame only works with 2D games that use a variety of sprites. Pygame isn't suitable for game development because it's challenging to use and doesn't have a graphical user interface like Unity, but it does give logic for more complex applications.

Pygame is a collection of cross-platform Python modules for game development. It includes sound and graphics libraries for use with the Python programming language. Now it's up to the developer's imagination or requirement to decide what kind of game to make using this toolbox.

In this post, we'll look at how to construct a design in PyGame using keys such that the design, or marker, travels horizontally when you press the right or left arrow keys on the keyboard and vertically when you use the up or down arrow keys. We can do this by placing a spot (marker) on the appropriate coordinates, which may be adjusted using keys.

Change in Coordinates of marker for respective keys pressed :
Left arrow key: Decrement in x coordinate
Right arrow key: Increment in x coordinate
Up arrow key: Decrement in the y coordinate
Down arrow key: Increment in the y coordinate

Below is the implementation

# importing the pygame module in the program
import pygame
# activates the pygame library .
# initiating pygame and gave permission
# to use the pygame functionality.
pygame.init()
# creating the display surface object
# of specific dimension..e(500, 500).
wn = pygame.display.set_mode((500, 500))
# setting the pygame window name
pygame.display.set_caption("Moving rectangle")
# marker current coordinates
x_new = 200
y_new = 200
# dimensions of the marker
wdth = 10
hght = 10
# velocity / speed of movement
velocity = 10
# Indicates pygame is running
rn = True
# infinite loop
while rn:
# creates time delay of 10ms
pygame.time.delay(10)
# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for vnt in pygame.event.get():
# if event object type is QUIT
# then quitting the pygame
# and program both.
if vnt.type == pygame.QUIT:
# it will make exit the while loop
rn = False
# stores keys pressed
kys = pygame.key.get_pressed()
# if left arrow key is pressed
if kys[pygame.K_LEFT] and x_new > 0:
# decrement in x co-ordinate
x_new -= velocity
# if left arrow key is pressed
if kys[pygame.K_RIGHT] and x_new < 500 - width:
# increment in x co-ordinate
x_new += velocity
# if left arrow key is pressed
if kys[pygame.K_UP] and y_new > 0:
# decrement in y co-ordinate
y_new -= velocity
# if left arrow key is pressed
if kys[pygame.K_DOWN] and y_new < 500 - height:
# increment in y co-ordinate
y += velocity

# drawing spot on screen which is rectangle here
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
# it refreshes the window
pygame.display.update()
# closes the pygame window
pygame.quit()
You can also try this code with Online Python Compiler
Run Code

OUTPUT

Frequently Asked Questions.

How do you create objects in Pygame?

The simplest way to make a Rect object in Pygame is to pass two tuples into the Pygame. Class Rect(). The first tuple (left, top) represents the rect's position on the window, while the second tuple (width, height) represents the Rect's dimensions.

What are the arrow keys called in Python?

K UP, K DOWN, K LEFT, and K RIGHT correspond to the arrow keys on the keyboard. The key is down, and the player is moved if the dictionary entry for that key is True.

How do you input a keyboard in Python?

The input() built-in method can read user input from the keyboard in Python. The user's information is converted to a string and then assigned to a variable. After entering the value from the keyboard, we must push the "Enter" button. The input() function then reads the user's weight.

In Python Pygame, how do you draw a circle?

The draw. circle() function can draw a circle in your Python game. The entire technique is the same as before, except for the position and parameters.

How do you press arrow keys in Python?

Call the press() function with a string from the pyautogui to press these keys. KEYBOARD KEYS such as enter, ESC, and f1.

Conclusion

So that's the end of the article Drawing the design using arrow keys in pygame

After reading about the Drawing design using arrow keys in pygame, are you not feeling excited to read/explore more articles on the topic of pygame? Don't worry; Coding Ninjas has you covered.

However, you may want to pursue our premium courses to give your job an advantage over the competition!

With our Coding Ninjas Studio Guided Path, you may learn about Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and more! Check out the mock test series and participate in the contests on Coding Ninjas Studio if you want to put your coding talents to the test! However, if you've just started school and are looking for answers to concerns raised by digital behemoths such as Amazon, Microsoft, Uber, and others. As part of your placement preparations, you must evaluate the obstaclesinterview experiences, and interview package in this case. Please vote for our blogs if you find them valuable and exciting.

Happy studying!

Live masterclass