Introduction
Pygame is a Python-based multimedia toolkit for creating games and other multimedia applications. This lesson will utilize the pygame module to create various shapes on the screen while considering their height, width, and position in the pygame window.
Pygame is a Python wrapper for SDL, for Simple DirectMedia Layer. SDL allows you to access your system's multimedia hardware, such as sound, video, mouse, keyboard, and joystick, from any platform. To replace the now-defunct PySDL project, the pygame project was formed. You can construct games and rich multimedia Python applications on any platform that supports SDL and pygame since they are cross-platform!
Pygame
Pygame is a cross-platform video game development library written in Python. Pygame games may be played using a mouse, keyboard, or joystick, among other input devices. Do you want to create a game that the mouse controls? Do you have any idea how to move the image around with your mouse? Don't worry; all you have to do now is define the two variables: running and moving. Set what your app should do while it is running after declaring values.
Approach:
Step 1: Import the pygame library first.
import pygame
from pygame.locals import *
Step 2: Take the colors we wish to utilize in the game as input.
clr_1 = #RGB value of color 1
clr_2 = #RGB value of color 2
clr_n = #RGB value of color n
Step 3: Then, create a graphical user interface game.
pygame.init()
Step 4: Set the dimensions of your GUI game as well.
wi, hi = #dimension of Width, #dimension of height
screen = pygame.display.set_mode((wi, hi))
Step 5: Then, select the picture you wish to move using the mouse as an input
imge = pygame.image.load('#Enter the image')
imge.convert()
Step 6: By putting rectangular borders around your image, you may make it more appealing.
rct = img.get_rect()
rct.center = wi//2, hi//2
Step 7: Set the running value for the game to run and the moving value for the picture to move later.
rnnng = True
mvng = False
Step 8: Set the things you want your app to do when in a running state.
while rnnng:
for vnt in pygame.event.get():
Step 8.1: Once the app is running, Allow the user to exit if they so want.
if vnt.type == QUIT:
rnnng = False
Step 8.2: If the user refuses to exit, resize your image to fit the GUI app's dimensions.
elif vnt.type == MOUSEBUTTONDOWN:
if rct.collidepoint(vnt.pos):
mvng = True
Step 8.3: If you only want to move the picture with a mouse click, set the movement value to False. Else, If you want to move the image without using the mouse, move to True.
elif vnt.type == MOUSEBUTTONUP:
mvng = False
Step 8.4: Set your image in a moving state if it has already been moved.
elif vnt.type == MOUSEMOTION and mvng:
rct.move_ip(vnt.rel)
Step 9: The next step is to customize the screen's color and picture.
screen.fill(YELLOW)
screen.blit(img, rect)
Step 10: Make your image even more appealing by putting a border around it.
pygame.draw.rect(screen, BLUE, rect, 2)
Step 11: Furthermore, update the changes done in the GUI game.
pygame.display.update()
Step 12: Finally, exit the graphical user interface game.
pygame.quit()
CODE
# Move the image with the mouse with a Python program
# Importing the library pygame
import pygame
from pygame.locals import *
#Consider your color choices.
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
# Create the user interface for the game.
pygame.init()
# Set the size of the game's user interface.
wi, hi = 640, 350
scrn = pygame.display.set_mode((wi, hi))
# Take image as input
imge = pygame.image.load('cn_pygame.png')
imge.convert()
# Draw rectangle around the image
rct = img.get_rect()
rct.center = wi//2, hi//2
# Set moving and running values
rnnng = True
mvng = False
# Setting what happens when the game
# changes to running state
while rnnng:
for vnt in pygame.event.get():
# Close the app if user wants it
if vnt.type == QUIT:
rnnng = False
# Moving the image
elif vnt.type == MOUSEBUTTONDOWN:
if rct.collidepoint(vnt.pos):
mvng = True
# If you want to move the picture with a mouse click, set # to False. If you want to move the picture without using the #mouse, set moving to True.
elif vnt.type == MOUSEBUTTONUP:
mvng = False
# Make your image move continuously
elif vnt.type == MOUSEMOTION and mvng:
rct.move_ip(vnt.rel)
# Set image on screen and color
scrn.fill(YELLOW)
scrn.blit(img, rect)
# Construct the image's border
pygame.draw.rect(screen, BLUE, rect, 2)
# save the new changes in GUI pygame
pygame.display.update()
# Quit the GUI game
pygame.quit()