-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacidbase.py
95 lines (77 loc) · 2.8 KB
/
acidbase.py
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
from bge import logic
import types
gdict = logic.globalDict
def hydron(particle):
particle.acid = types.MethodType(acid,particle)
particle.collisionCallbacks.append(particle.acid)
def hydroxide(particle):
particle.base = types.MethodType(base,particle)
particle.collisionCallbacks.append(particle.base)
def acid(self,other):
if "doubles" in other and other.get_charge() == 0:
self.collisionCallbacks.remove(self.acid)
self.remove_glow()
other.link(self)
electron_movement(other)
elif other.name == "Lone Pair":
self.scene.addObject("reaction_sound",self)
bond = list(other["bonds"])[0]
bond.unlink(other)
other.self_destruct() # destroy lone pair
self.link(bond)
def electron_movement(particle):
if particle.name == "Oxygen":
carbon = None
for bond in particle.get_bonds():
if bond.name == "Carbon":
if carbon:
if carbons(bond) > carbons(carbon):
carbon = bond
else:
carbon = bond
# unlinks carbon with highest degree
if carbon:
particle.unlink(carbon)
return
if particle["doubles"]:
particle.scene.addObject("fizzle_sound",particle)
double = next(iter(particle["doubles"]))
particle.unlink(double)
return
elif particle["triple"]:
particle.scene.addObject("fizzle_sound",particle)
particle.unlink(particle["triple"])
return
else:
for bond in particle["bonds"]:
if bond.name == "Hydrogen":
# create another hydron
particle.unlink(bond)
bond.add_glow()
return
def base(self,other):
if other.name == "Hydrogen" and other not in self["bonds"]:
self.scene.addObject("reaction_sound",self)
self.molecule.damp()
carbon = next(iter(other["bonds"]))
for bond in iter(self["bonds"]):
if bond.name == "Lone Pair":
lonepair = bond
break
# acidbase
carbon.unlink(other)
self.unlink(lonepair)
self.link(other)
carbon.link(lonepair)
# elimination
for atom in carbon["bonds"]:
if atom.name == "Carbon":
for atom2 in atom["bonds"]:
if atom2.name == "Bromine":
atom.unlink(atom2)
carbon.unlink(lonepair)
# destroy lonepair
lonepair.self_destruct()
carbon.multiply_bonds(atom)
return
self.collisionCallbacks.remove(self.base)