Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/physics.py
blob: 726ed077d36e72735fa7b9f554f9611cd70170ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#==================================================================
#                           Physics.activity
#     An attempt at a Phun / Crayon Physics style physics game
#                  By Alex Levenson and Brian Jordan
#==================================================================

import sys
import math
import pygame
from pygame.locals import *
from pygame.color import *
from sugar.activity import activity
import elements
from elements import Elements
from tools import *

# =======================================Classes==================================
# tools that can be used superlcass
class Tool(object):
    def __init__(self):
        # default tool name
        self.name = "Tool"
    def handleEvents(self,event):
        # default event handling
        global currentTool
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            # bye bye! Hope you had fun!
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                #space pauses
                world.run_physics = not world.run_physics  
            elif event.key == K_c:
                currentTool.cancel()
                currentTool = tools["circle"]
            elif event.key == K_b:
                currentTool.cancel()
                currentTool = tools["box"]
            elif event.key == K_t: 
                currentTool.cancel()
                currentTool = tools["triangle"]
            elif event.key == K_j:
                currentTool.cancel()
                currentTool = tools["joint"]
            elif event.key == K_g:
                currentTool.cancel()
                currentTool = tools["grab"]
        else:
            # let the subclasses know that no events were handled yet
            return False  
        return True                                     
    def draw(self):
        # default drawing method is don't draw anything
        pass
    def cancel(self):
        # default cancel doesn't do anything
        pass

# The circle creation tool        
class CircleTool(Tool):    
    def __init__(self):
        self.name = "Circle"
        self.pt1 = None
        self.radius = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(CircleTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.pt1 = pygame.mouse.get_pos()                    
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1:                    
                    if self.radius > 1: # elements doesn't like tiny shapes :(
                        world.add.ball(self.pt1,self.radius, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                    self.pt1 = None        
                    self.radius = None            
    def draw(self):
        # draw a circle from pt1 to mouse
        if self.pt1 != None:
            self.radius = distance(self.pt1,pygame.mouse.get_pos())
            if self.radius > 3: 
                thick = 3
            else:
                thick = 0
            pygame.draw.circle(screen, (100,180,255),self.pt1,self.radius,thick) 
            pygame.draw.line(screen,(100,180,255),self.pt1,pygame.mouse.get_pos(),3)  
    def cancel(self):
        self.pt1 = None  
        self.radius = None
"""        
# The box creation tool        
class BoxTool(Tool):    
    def __init__(self):
        self.name = "Box"
        self.pt1 = None        
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(BoxTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.pt1 = pygame.mouse.get_pos()                    
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1 and self.pt1!=None:                          
                    world.add.ball(self.pt1,self.radius, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                    self.pt1 = None        
                    self.radius = None            
    def draw(self):
        # draw a box from pt1 to mouse
        if self.pt1 != None:           
            pygame.draw.box(screen, (100,180,255),self.pt1,,thick)   
    def cancel(self):
        self.pt1 = None  
        self.radius = None        
"""            
# The triangle creation tool        
class TriangleTool(Tool):    
    def __init__(self):
        self.name = "Triangle"
        self.pt1 = None
        self.vertices = None
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if not super(TriangleTool,self).handleEvents(event):
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.pt1 = pygame.mouse.get_pos()                    
            elif event.type == MOUSEBUTTONUP:
                if event.button == 1 and self.pt1!= None:                    
                    if distance(self.pt1,pygame.mouse.get_pos()) > 10: # elements doesn't like tiny shapes :(
                        world.add.convexPoly(self.vertices, dynamic=True, density=1.0, restitution=0.16, friction=0.5)                    
                    self.pt1 = None
                    self.vertices = None                    
    def draw(self):
        # draw a triangle from pt1 to mouse
        if self.pt1 != None:
            self.vertices = constructTriangleFromLine(self.pt1,pygame.mouse.get_pos())
            pygame.draw.polygon(screen, (100,180,255),self.vertices, 3) 
            pygame.draw.line(screen,(100,180,255),self.pt1,pygame.mouse.get_pos(),3)  
    
    def cancel(self):
        self.pt1 = None        
        self.vertices = None


# set up pygame
pygame.init()
size = (700,700)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
font = pygame.font.Font(None, 24) # font object
    
# set up the world
world = elements.Elements(size)
world.renderer.set_surface(screen)

# load enviornment
world.add.ground()

# setup tools
tools = {
    "triangle": TriangleTool(),
   # "box": BoxTool(),
    "circle": CircleTool(),
   # "joint": JointTool(),
   # "grab": GrabTool()
}
currentTool = tools["triangle"]

# Main Loop:    
while True:
    for event in pygame.event.get():
        currentTool.handleEvents(event)
                        
    # Clear Display
    screen.fill((255,255,255))

    # Update & Draw World
    world.update()
    world.draw()
    
    # draw output from tools
    currentTool.draw()

    #Print all the text on the screen
    text = font.render("Current Tool: "+currentTool.name, True, (100,100,255))
    textpos = text.get_rect(left=1,top=1)  
    screen.blit(text,textpos)        

    # Flip Display
    pygame.display.flip()
    
    # Try to stay at 30 FPS
    clock.tick(30) # originally 50