# GUI Application

Simulator is same as other GUI application with algorithms used in robotics and contain different robotic model in which you can play around.

So ,first we will follow the procedure for creating simple GUI application.

(adsbygoogle = window.adsbygoogle || []).push({});

# 1) Opening a window

    import 	pygame

    pygame.init()

    screen = pygame.display.set_mode((640,480))
1
2
3
4
5

First of all, to use pygame we have to import it. Then we create a Surface that is 640 pixels wide and 400 pixels high. There are a lot more things you can do with set_mode, but we will keep it simple at this point.

This code give you the window open for limited period of time.We have to add inifinite loop so the program never gets close without user wish.

# 2) Break to exit

   running = True
   while running:
      for event in pygame.event.get():
            if event.type == pygame.QUIT:
               running = false
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
               running = flase
1
2
3
4
5
6
7

The loop is controlled by a flag called running. As long as the flag is set the loop keeps running. Inside the loop we use the get() method to grab the event that occur since next execution.

There are several different event types that pygame knows about and can respond to. One of these is QUIT and KEYDOWN

QUIT : Gets triggered when the user clicks on the window’s close button.

KEYDOWN : Gets triggered when the user any keyboard button.When we receive a KEYDOWN event, the event object will hold the code of the key that was pressed in an attribute called ‘key’. So by comparing event.key to whichever key we are interested in, we can find out if that was the actual key that was pressed.

All we do if we get an event of this type is clear the running flag, which will exit the loop and cause the program to terminate. Still simple, isn’t it.

# 3) Structuring Program

import pygame

class Simulator(object):
	def main(self , screen):
		while 1:
          for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 return
             if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                 return

              

if __name__ == '__main__':
	pygame.init()
	screen = pygame.display.set_mode((640,480))
    Simulator().main(screen)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

We have clean our code and apply OOP concept . If you are not familiar with OOP , please prepare yourself about class , object .How to initialize and create them.

# Final Code

(adsbygoogle = window.adsbygoogle || []).push({});
import  pygame

pygame.init()

screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("Open Window")


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# EXECRISE:

  1. Create a window that is 320 pixels wide and 200 pixels high.
  2. Create a program where the user can specify the width and the height as command line arguments.
  3. Create a program that asks the users for the width and the height and then displays the window.
  4. Write a program that calls pygame.display.set_mode twice with different sizes. What do you expect should happen? Run the program. What actually happens? Can you explain why?