Skip to content

Commit 329ac0f

Browse files
committed
click outside object to deselect
1 parent 490287d commit 329ac0f

File tree

10 files changed

+59
-58
lines changed

10 files changed

+59
-58
lines changed

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
sys.path.insert(0, os.path.abspath('.'))
1818
sys.path.insert(0, os.path.abspath('..'))
1919
sys.path.insert(0, os.path.abspath('./tutorial1'))
20-
sys.path.insert(0, os.path.abspath('../tutorial2'))
20+
sys.path.insert(0, os.path.abspath('./tutorial2'))
2121
sys.path.insert(0, os.path.abspath('./tutorial3'))
22+
sys.path.insert(0, os.path.abspath('./tutorial4'))
2223

2324
# -- Project information -----------------------------------------------------
2425

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This tutorial explains how to make interactive games and applications with Pygam
1717
tutorial3/tutorial3
1818
tutorial4/board
1919
sandbox
20+
todo
2021

2122
Indices and tables
2223
==================

docs/todo.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
To do
2+
=====
3+
4+
* cmd-click : select an item
5+
* alt-click : move selection
6+
* Board games table: number, visibilty, text, color, image
7+
* Selection : arrows and mouse clic
8+
* Multiple selection : cmd+click, drag, (border margins)
9+
* Games: Pong, Snake, Bricks, Space invader,
10+
* Memory, 2048, Wordament
11+
* Astroid, bullets, gravity
12+
* Platformer games
13+
* Dame, Go, Chess
14+
* Button, ListMenu, TextMenu, CheckBox, Slider
15+
* Select, move and resize an objet
16+
* Edit a polygon
17+
* Collision between objects
18+
* Schedule : one-time and regular
19+
* Music, sounds, images
20+
* Object : x, y, z, scale, rotation, visible, anchor
21+
22+
object selection
23+
24+
* click outside to deselect
25+
* TAB to select next (shift-TAB) to select previous
26+
* draw rect to make multiple selection

docs/tutorial3/gui1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
class ShortcutDemo(Game):
1818
def __init__(self):
1919
super(ShortcutDemo, self).__init__()
20-
Text('Shortcuts', size=100)
21-
Text('Press A, with shift, ctrl, alt and cmd', size=50)
20+
Text('Shortcuts')
21+
Text('Press A, with shift, ctrl, alt and cmd', size=24)
2222

2323
def on_event(self, event):
2424
if event.type == KEYDOWN:

docs/tutorial3/gui2.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
"""Menu elements
2-
* Button
3-
* ListMenu
4-
* Var
5-
"""
1+
"""Place clickable buttons on the screen."""
62

73
import pygame
84
from random import randint
@@ -19,13 +15,12 @@ class ButtonDemo(Game):
1915
def __init__(self):
2016
super(ButtonDemo, self).__init__()
2117
self.shortcuts.update(cmd)
22-
Text('Button Demo', size=48)
18+
Text('Button Demo')
2319
Text('Press A to add, BACK to remove', size=24)
2420

25-
Button('Start 1', 'print("start 1")')
21+
Button('Start 1', 'print("start 1")', pos=(10, 100))
2622
Button('Start 2', 'print("start 2")')
27-
Button('Stop', 'Game.running=False', pos=(200, 100), color=YELLOW)
28-
Button('Quit', 'sys.exit()')
23+
Button('Stop', 'sys.exit()', pos=(200, 100), color=YELLOW)
2924

3025
if __name__ == '__main__':
3126
ButtonDemo().run()

docs/tutorial3/gui3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self):
1717
self.var2 = 'hello'
1818
self.var3 = True
1919

20-
Text('Display variables', size=48)
20+
Text('Display variables')
2121
Text('Press Up/Down to inc by 1', size=24)
2222
Text('Press shift+Up/Down to inc by 10')
2323
Text('var1 = ' + str(self.var1))

docs/tutorial3/gui4.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
"""Editing graphical shapes.
2-
* placing rectangles"""
1+
"""Editing graphical shapes. placing rectangles"""
32

43
import pygame
54
from random import randint
65
from pygame.locals import *
76
from pygamelib import *
87

9-
words = ['beauty', 'strength', 'love', 'dream', 'silence']
10-
cmd = {
11-
K_BACKSPACE:'Game.objects.pop()',
12-
K_p:'Game.capture(self)',
13-
K_t:'print("test")',}
14-
158
class GuiDemo(Game):
169
"""Draw text in different sizes and colors."""
1710
def __init__(self):
1811
super(GuiDemo, self).__init__()
12+
self.shortcuts[K_BACKSPACE] = 'Game.objects.pop()'
1913
Text('Placing rectangles', size=50)
2014
Text('Press A to add, BACK to remove', size=24)
2115
self.editing = False
2216
self.cmd = cmd
2317

24-
2518
def on_event(self, event):
2619
if event.type == MOUSEBUTTONDOWN:
2720
if self.key == K_a:

docs/tutorial3/gui5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class GuiDemo5(Game):
88
"""Make a subclass of the Game class."""
99
def __init__(self):
1010
super(GuiDemo5, self).__init__()
11-
Text('Selecting objects', size=48)
11+
Text('Selecting objects')
1212
Text('Click to select', size=24)
13-
Text('Cmd-drag to move')
13+
Text('Ctrl-drag to move')
1414
Text('Type to edit text')
1515
Text('Cmd-V to animate')
1616

docs/tutorial4/board.rst

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,4 @@ Adding background color
3434
.. autoclass:: BoardDemo
3535
:members:
3636

37-
.. image:: BoardDemo.png
38-
39-
To do
40-
=====
41-
42-
GUI
43-
44-
* cmd-click : select an item
45-
* alt-click : move selection
46-
* Board games table: number, visibilty, text, color, image
47-
* Selection : arrows and mouse clic
48-
* Multiple selection : cmd+click, drag, (border margins)
49-
* Games: Pong, Snake, Bricks, Space invader,
50-
* Memory, 2048, Wordament
51-
* Astroid, bullets, gravity
52-
* Platformer games
53-
* Dame, Go, Chess
54-
* Button, ListMenu, TextMenu, CheckBox, Slider
55-
* Select, move and resize an objet
56-
* Edit a polygon
57-
* Collision between objects
58-
* Schedule : one-time and regular
59-
* Music, sounds, images
60-
* Object : x, y, z, scale, rotation, visible, anchor
37+
.. image:: BoardDemo.png

pygamelib.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def draw(self):
173173
class Text(Shape):
174174
"""Draw a line of text on the screen."""
175175
fontcolor = BLACK
176-
fontsize = 24
176+
fontsize = 48
177177
fontname = None
178178
bgcolor = None
179179

@@ -267,7 +267,7 @@ def __init__(self, msg, cmd, size=None, color=None, d=None, **kwargs):
267267
Button.size = list(size)
268268
self.size = Button.size
269269
self.rect.size = self.size
270-
Game.pos[1] += Button.size[1]
270+
Shape.pos[1] += Button.size[1]
271271

272272
if d != None:
273273
Button.d = d
@@ -432,13 +432,7 @@ def run(self):
432432
self.current_obj.on_key(event)
433433

434434
elif event.type == MOUSEBUTTONDOWN:
435-
for obj in Game.objects:
436-
if obj.rect.collidepoint(event.pos):
437-
if self.current_obj:
438-
self.current_obj.is_active = False
439-
self.current_obj = obj
440-
obj.is_active = True
441-
obj.on_click(event)
435+
self.select_objects(event)
442436

443437
elif event.type == MOUSEMOTION:
444438
if self.current_obj:
@@ -482,6 +476,18 @@ def find_objects(self, pos):
482476
"""Return the objects at position."""
483477
return [obj for obj in Game.objects if obj.rect.collidepoint(pos)]
484478

479+
def select_objects(self, event):
480+
"""Select objects at position pos."""
481+
objs = self.find_objects(event.pos)
482+
for obj in objs:
483+
if self.current_obj:
484+
self.current_obj.is_active = False
485+
self.current_obj = obj
486+
obj.is_active = True
487+
obj.on_click(event)
488+
if len(objs) == 0:
489+
self.current_obj.is_active = False
490+
485491
def do_shortcuts(self, event):
486492
"""Check if the key/mod combination is part of the shortcuts
487493
dictionary and execute it. More shortcuts can be added
@@ -493,6 +499,8 @@ def do_shortcuts(self, event):
493499
exec(self.shortcuts[k])
494500
elif (k, m) in self.shortcuts:
495501
exec(self.shortcuts[k, m])
496-
502+
503+
504+
497505
if __name__ == '__main__':
498506
Game().run()

0 commit comments

Comments
 (0)