Open In App

How to set up pygame with Eclipse?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

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. 

Note: PyDdev requires Java 8 and Eclipse 4.6 onward to support Python 2.6 and newer versions. 

Steps required to install PyDev in Eclipse

Step 1: Open Eclipse on your computer. 

Step 2: Go to Help -> Eclipse Marketplace and search for PyDev in the search bar. 

Step 3: Click on Install Now at the bottom of the pop-up window.

How to set up pygame with Eclipse?

 

Step 4: Select the PyDev and PyDev Mylyn Integration checkbox on the next screen that arrives. Click on Finish.

Step 5: Go to Windows> Preferences> PyDev> Interpreter> Python Interpreter. Click on the Browse for python/pypy.exe button to add Python.exe as an Interpreter. Finally, click on Apply and Close.

How to set up pygame with Eclipse?

 

Step 6: After the following steps PyDev is configured in your Eclipse. Click on File> New> PyDev Project. Enter the project name and click Finish. You can find the newly created python package on the left pane of the Eclipse window under the package explorer tab. 

Step 7: Create a python module under that project by right-clicking on that project -> New -> Other ->Pydev Module -> Next followed by giving a name to your python module and clicking on the Finish button. The eclipse will open your PyDev workspace.

How to set up pygame with Eclipse?

 

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.
How to set up pygame with Eclipse?

 

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
How to set up pygame with Eclipse?

 

  • 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?

 



Last Updated : 25 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads