Problem Solving and Python Programming: Laboratory Programs

Laboratory Python Programs Set 2

Problem Solving and Python Programming

Engineering Python : UNIT V : Files, Modules, Packages : Experiment and Solution

Experiment 11: Exploring Pygame tool

Solution :

Program : Before actual coding in Pygame, let us understand what is Pygame. Pygame is basically a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language.

We can draw various graphics or we can create simple games using Pygame.

How to Download Pygame ?

We can download Pygame using following steps

Step 1: Open the website http://www.pygame.org/download.shtml. Depending upon your operating system, download appropriate version of Pygame. For my Windows operating system, I have chosen following link


On the web site http://www.lfd.uci.edu/~gohlke/pythonlibs /#pygame search for Pygame, Since I am using Windows 32 bit and Python 3.6, I have chosen the release marked as below


Get it downloaded on your computer.

Step 2: For installing this version, open command prompt and type the command

Prompt :> pip install wheel

This command will download required additional libraries.

Step 3: Then type the command

Prompt : > pip install wheel -upgrade

It will display the message “Requirements already up-to-date"

Step 4: Now change the directory on the command prompt window to the directory where you have downloaded Pygame. Issue the command for installation


Finally the Pygame gets successfully installed.

How to write Pygame program ?

The Pygame program can be written with the help of Python IDE. Open the new File and write the code for Pygame program. You should save your code with .py extension. The following is the simplest anatomy of writing any Pygame program

import pygame

 

pygame.init()

screen = pygame.display.set_mode((400, 300))

done = False

 

while not done:

for event in pygame.event.get():

if event.type == pygame. QUIT:

done = True

pygame.display.flip()

Code Explanation :

import pygame - This is the most required statement at the beginning of any Pygame program.

pygame.init() - It initializes all the modules required for PyGame.

pygame.display.set_mode((width, height)) - This will launch a window of the desired size. The return value is a Surface object which is the object you will perform graphical operations on.

pygame.event.get() - This empties the event queue. If you do not call this, the windows messages will start to pile up and your game will become unresponsive in the opinion of the operating system.

pygame.QUIT - This is the event type that is fired when you click on the close button in the corner of the window.

pygame.display.flip() - PyGame is double-buffered. This swaps the buffers. All you need to know is that this call is required in order for any updates that you make to the game screen to become visible.

When we run the above code we will get


We can draw rectangles, circles, ellipse and so on using various commands. Before you draw any object fill the screen with suitable color.

screen.fill(color)

The color is in the form of R,G,B values. For example

red = (255,0,0)

green = (0,255,0)

blue = (0,0,255)

darkBlue = (0,0,128)

white = (255,255,255)

black = (0,0,0)

pink = (255,200,200)

Here are the commands for drawing lines, rectangles, and so on

(1) pygame.draw.lines(screen, color, closed, pointlist, thickness)

• draws a series of lines, connecting the points specified in pointlist

• pointlist is a list of tuples, specifying a series of points.

• closed should be either True or False, indicating whether to connect the last point back to the first

• thickness is the thickness of the line (in pixels).

• Example : pygame.draw.lines(screen, black, False, [(100,100), (150,200), (200,100)], 1)

(2) pygame.draw.rect(screen, color, (x,y,width,height), thickness)

• draws a rectangle

•  (x,y,width, height) is a Python tuple

• x,y are the coordinates of the upper left hand corner

• width, height are the width and height of the rectangle

• thickness is the thickness of the line.

(3) pygame.draw.circle(screen, color, (x,y), radius, thickness)

• draws a circle

•  (x,y) is a Python tuple for the center, radius is the radius

• thickness is the thickness of the line.

Simulation of Elliptical Orbits

import pygame

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((640,480))

white = (255,255,255)

black = (0,0,0)

screen.fill(white)

while True:

for event in pygame.event.get():

if event.type = = QUIT:

pygame.quit() pygame.draw.ellipse(screen, black,(260,160,60,100),2)#innermost ellipse

pygame.draw.ellipse(screen, black,(240,140,100,140),2)

pygame.draw.ellipse(screen, black,(220,120,140,180),2)

pygame.draw.ellipse(screen,black,(200,100,180,220),2)

pygame.draw.ellipse(screen, black,(180,80,220,260),2)

pygame.draw.ellipse(screen, black,(160,60,260,300),2)

pygame.draw.ellipse(screen, black,(140,40,300,340),2)

pygame.draw.ellipse(screen, black,(120,20,340,380),2)

pygame.draw.ellipse(screen, black,(100,00,380,420),2)#outermost ellipse

pygame.display.update()

Output


 

Problem Solving and Python Programming: Laboratory Programs : Tag: Engineering Python : Problem Solving and Python Programming - Laboratory Python Programs Set 2