Steps required to install Pygame and make it work with PyDev
- Open a command prompt and type the commands given below.
pip install pygame
To check if the installation was successful type the commands given below.
python import pygame
The above commands will show the Pygame version followed by a greeting message from the Pygame community.
- Now, cross-check if PyDev has recognized the Pygame library or not. To do so, Windows -> Preferences -> PyDev -> Interpreter -> Python Interpreter, under the Packages tab look for Pygame library.
Note: In case you could not find Pygame under the Packages tab then try to add it manually by clicking on the Libraries tab – > New Folder followed by adding the folder which contains the Pygame library installed earlier. ( For instance, mine one was present at
C:\Users\ipsit\AppData\Local\Programs\Python\Python38\lib\site-packages
- Finally, click on Apply and Close.
Example:
Now let us check if a Pygame code is working on our machine or not. The below code draws a rectangle that can be moved around the Pygame window using the keyboard arrow buttons.
Python3
# code import pygame import math import random from pygame. locals import * class Player(): def __init__( self ): self .rect = pygame.draw.rect(screen, ( 0 , 0 , 255 ), ( 30 , 30 , 60 , 60 )) self .dist = 20 def handle_keys( self ): for e in pygame.event.get(): if e. type = = QUIT: pygame.quit() exit() elif e. type = = KEYDOWN: key = e.key if key = = K_LEFT: self .draw_rect( - 5 , 0 ) elif key = = K_RIGHT: self .draw_rect( 5 , 0 ) elif key = = K_UP: self .draw_rect( 0 , - 5 ) elif key = = K_DOWN: self .draw_rect( 0 , 5 ) elif key = = K_ESCAPE: pygame.quit() exit() def draw_rect( self , x, y): screen.fill(( 0 , 0 , 0 )) self .rect = self .rect.move(x * self .dist, y * self .dist) pygame.draw.rect(screen, ( 0 , 255 , 255 ), self .rect) pygame.display.update() def draw( self , surface): pygame.draw.rect(screen, ( 0 , 255 , 255 ), ( 30 , 30 , 60 , 60 )) pygame.init() # initializing pygame screen = pygame.display.set_mode(( 600 , 600 )) pygame.display.set_caption( "MOVE THE BLOCK OVER THE WINDOW" ) clock = pygame.time.Clock() player = Player() screen.fill(( 0 , 0 , 0 )) player.draw(screen) pygame.display.update() while True : player.handle_keys() |
Output:
How to set up pygame with Eclipse?
In this article, we will see how to set up Pygame with Eclipse, Before moving ahead let us understand some of the basic terminologies to understand all things more clearly.
Contact Us