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.

Similar Reads

What is Pygame?

Pygame is an open-source, cross-platform library that was designed for coding video games. Building 2-D games using Pygame with added-on features can be a fascinating short project for a fresher in the game development domain. Pygame is beginner-friendly and also it works on all kinds of operating systems....

What is Eclipse IDE?

Eclipse is an integrated development environment used (IDE) for developing Java applications. It can also be used for developing applications in 44 different programming languages via plug-ins (a plug-in is software that adds a specific feature on top of an existing computer program). To set up Pygame in Eclipse IDE we first need to set up the python development environment in it. Now, assuming that you have Eclipse already installed in your system, let us see how we can set up the python environment. We shall use PyDev which is a well-known third-party plug-in for Eclipse.  Let us first install PyDev in Eclipse....

Steps required to install PyDev in Eclipse

Step 1: Open Eclipse on your computer....

Steps required to install Pygame and make it work with PyDev

Open a command prompt and type the commands given below....

Contact Us