Python-2 - Unit-6,7,8 - Game Development
Python-2 - Unit-6,7,8 - Game Development
Input
Button
Text Box
Keyboard
key down
key up
Mouse
Click
Drag
Timer
One of the event handlers executes and the other waits in the event queue until the first handler finishes.
You can’t control the order that the system inserts events into the event queue and only one event handler
executes at a time.
In [ ]:
In [ ]:
1 import SimpleGUICS2Pygame.simpleguics2pygame
Timers
Tick
Tick
Tick
Tick
Tick
In [2]:
Tick
Tick
Tick
Tick
Tick
In [1]:
tick
tick
tick
tick
tick
In [2]:
False
True
False
Frames
Create Frame-> simplegui.create_frame()
BG Color -> frame.set_canvas_background()
Frame Start -> frame.start()
Frame Text Width -> frame.get_canvas_textwidth()
In [ ]:
Given a text string, a font size, and a font face, this returns the width of the text in pixels. It does not draw
the text. This is useful in computing the position to draw text when you want it centered or right justified in
some region.
The supported font faces are the default "serif", "sans-serif", and "monospace".
In [ ]:
Adding Button
In [ ]:
In [3]:
Add Input ¶
In [4]:
In [4]:
In [5]:
In [8]:
In [6]:
In [1]:
In [1]:
Each point is a 2-element tuple or list of screen coordinates. The line's width is given in pixels, and must be
positive.
The fill color defaults to None. If the fill color is specified, then the interior of the polygon is colored.
In [3]:
1 #Drawing polygon with polyline to connect last and first point by just connecting
2 #last and first
3 #Drawing connected lines on canvas
4 import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
5 def draw_handler(canvas):
6 canvas.draw_polyline([(10, 20), (30, 20), (90, 70),(10,20)], 5, 'Red')
7 canvas.draw_polyline([[40, 20], [80, 40], [30, 90],[40,20]], 6, 'Blue')
8 #It can pass tuple or list
9 frame = simplegui.create_frame('Testing', 400, 400)
10 frame.set_draw_handler(draw_handler)
11 frame.start()
In [7]:
In [23]:
In [22]:
In [2]:
In [2]:
Draw Image
In [11]:
In [ ]:
1 #bit.ly/cards_back
The handler for each should be defined with one parameter, as in the above example. This parameter will
receive an integer representing a keyboard character.
Keyboard Echo
In [1]:
In [3]:
In [5]:
1 print(simplegui.KEY_MAP)
{'space': 32, 'left': 37, 'up': 38, 'right': 39, 'down': 40, '0': 48, '1':
49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55, '8': 56, '9': 5
7, 'A': 65, 'B': 66, 'C': 67, 'D': 68, 'E': 69, 'F': 70, 'G': 71, 'H': 72,
'I': 73, 'J': 74, 'K': 75, 'L': 76, 'M': 77, 'N': 78, 'O': 79, 'P': 80,
'Q': 81, 'R': 82, 'S': 83, 'T': 84, 'U': 85, 'V': 86, 'W': 87, 'X': 88,
'Y': 89, 'Z': 90, 'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70,
'g': 71, 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78,
'o': 79, 'p': 80, 'q': 81, 'r': 82, 's': 83, 't': 84, 'u': 85, 'v': 86,
'w': 87, 'x': 88, 'y': 89, 'z': 90}
Ball Motion
In [4]:
In [5]:
In [8]:
Sound
sound.play()
sound.pause()
sound.rewind()
sound.set_volume()
In [ ]:
1 #bit.ly/game_sound
In [1]:
In [2]: