Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #By Mr.A AKA A-MAN (c)2012-2013
- #All rights reserved
- #The A-Wave Simulator
- #Feel free to use any part of this code, but don't claim as your own.
- import pygame
- from pygame.locals import *
- import time, math, sys
- class SIN:
- def sin(self, x):
- return math.sin(math.radians(x))
- def inv(self, x):
- return math.degrees(math.asin(math.radians(x)))
- class SIM:
- global SIN
- def __init__(self):A
- self.FPS = 100
- self.FPS_CLOCK = pygame.time.Clock()
- pygame.init()
- self.SCREEN = pygame.display.set_mode((900, 670))
- pygame.display.set_caption("The A-Atom Simulator: Bhor's Model")
- self.E_COLORS = [(0, 0, 255), (0, 0, 128)]
- self.ELECTRONSET = []
- self.P_COLORS = [(255, 0, 0), (128, 0, 0)]
- self.PROTONSET = []
- self.CHARGE = 4.0
- self.FONT = pygame.font.SysFont(None, 20, False, False)
- self.CFONT = pygame.font.SysFont(None, 12, False, False)
- self.RTEXT= self.FONT.render("Reset All: ", True, (0, 255, 0))
- self.RTEXTbdy = self.RTEXT.get_rect()
- self.RTEXTbdy.right = self.SCREEN.get_rect().right - 95
- self.RTEXTbdy.top=self.SCREEN.get_rect().top + 10
- self.CTEXT= self.CFONT.render("By A-Studios. All Rights Reserved. (c)2012-2013", True, (255, 255, 0))
- self.CTEXTbdy = self.CTEXT.get_rect()
- self.CTEXTbdy.right = self.SCREEN.get_rect().right - 10
- self.CTEXTbdy.bottom=self.SCREEN.get_rect().bottom - 7
- self.PNTEXT= self.FONT.render("Positive Nucleon mass: ", True, (0, 255, 0))
- self.PNTEXTbdy = self.PNTEXT.get_rect()
- self.PNTEXTbdy.left = self.SCREEN.get_rect().left + 10
- self.PNTEXTbdy.bottom=self.SCREEN.get_rect().bottom - 15
- self.APNTEXT= self.FONT.render("Allow repulsion between Nucleons: ", True, (0, 255, 0))
- self.APNTEXTbdy = self.APNTEXT.get_rect()
- self.APNTEXTbdy.left = self.SCREEN.get_rect().left + 10
- self.APNTEXTbdy.bottom=self.SCREEN.get_rect().bottom - 35
- self.BAPNTEXT= self.FONT.render("Allow Electrons bypass Nucleons: ", True, (0, 255, 0))
- self.BAPNTEXTbdy = self.BAPNTEXT.get_rect()
- self.BAPNTEXTbdy.left = self.SCREEN.get_rect().left + 10
- self.BAPNTEXTbdy.bottom=self.SCREEN.get_rect().bottom - 55
- self.NAPNTEXT= self.FONT.render("No. of active Nucleons: ", True, (0, 255, 0))
- self.NAPNTEXTbdy = self.NAPNTEXT.get_rect()
- self.NAPNTEXTbdy.left = self.SCREEN.get_rect().left + 400
- self.NAPNTEXTbdy.bottom=self.SCREEN.get_rect().bottom - 35
- self.NAENTEXT= self.FONT.render("No. of active Electrons: ", True, (0, 255, 0))
- self.NAENTEXTbdy = self.NAENTEXT.get_rect()
- self.NAENTEXTbdy.left = self.SCREEN.get_rect().left + 400
- self.NAENTEXTbdy.bottom=self.SCREEN.get_rect().bottom - 15
- self.BUTTONBDYS = []
- self.BUTTONBDYS.append((self.SCREEN.get_rect().right - 90,self.SCREEN.get_rect().top+10, 20, 15)) #RESET all
- self.BUTTONBDYS.append((self.SCREEN.get_rect().right - 40,self.SCREEN.get_rect().top+10, 20, 15)) #CLOSE X
- self.BUTTONBDYS.append((self.PNTEXTbdy[0]+200+17+17,self.PNTEXTbdy[1], 24, 12))
- self.BUTTONBDYS.append((self.PNTEXTbdy[0]+200+17+17+29,self.PNTEXTbdy[1], 24, 12))
- self.BUTTONBDYS.append((self.APNTEXTbdy[0]+200+17+17+15,self.APNTEXTbdy[1]-3, 20, 15))
- self.BUTTONBDYS.append((self.BAPNTEXTbdy[0]+200+17+17+15,self.BAPNTEXTbdy[1]-3, 20, 15))
- #DRAGGIN'
- self.original_pos_E = None
- self.drag_pos_E = None
- self.original_pos_P = None
- self.drag_pos_P = None
- #END;
- self.RESET = False
- self.KONSTANT = "2 * 10^4"
- self.E_MASS = 4
- self.P_MASS = 40
- self.ALLOW_REPULSION = False
- self.E_BYPASS = False
- self.KTEXT= self.FONT.render("For k*(Q1*Q2)/(d^2), constant <k> => " + str(self.KONSTANT) + " (N.m^2/C^2)" , True, (255, 255, 255))
- self.KTEXTbdy = self.KTEXT.get_rect()
- self.KTEXTbdy.left = self.SCREEN.get_rect().left + 20
- self.KTEXTbdy.top=self.SCREEN.get_rect().top + 10
- self.KONSTANT = self.KONSTANT.replace("^", "**")
- self.KONSTANT = eval(self.KONSTANT)
- def collide_w_mouse(self, x, y, recto):
- rect =pygame.Rect(recto)
- if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
- return True
- else:
- return False
- def draw_inc_dec_buttons(self, window, bdy, buttontype, color):
- if buttontype == 0:
- pygame.draw.rect(window, color, bdy, 2)
- pygame.draw.line(window, (255,255,255), (bdy[0]+2, (bdy[1]+(bdy[3]/2))), (bdy[0]+bdy[2]-2,(bdy[1]+(bdy[3]/2))), 1)
- pygame.draw.line(window, (255,255,255), ((bdy[0]+(bdy[2]/2)), bdy[1]+2), ((bdy[0]+(bdy[2]/2)),bdy[1]+bdy[3]-2), 1)
- if buttontype == 1:
- pygame.draw.rect(window, color, bdy, 2)
- pygame.draw.line(window, (255,255,255), (bdy[0]+2, (bdy[1]+(bdy[3]/2))), (bdy[0]+bdy[2]-2,(bdy[1]+(bdy[3]/2))))
- if buttontype == 2:
- pygame.draw.rect(window, color, bdy, 2)
- pygame.draw.line(window, (255,255,255), (bdy[0]+2, (bdy[1]+(bdy[3]/2))), (bdy[0]+(bdy[2]/2)-2,(bdy[1]+(bdy[3]/2))), 1)
- pygame.draw.line(window, (255,255,255), ((bdy[0]+(bdy[2]/4)), bdy[1]+2), ((bdy[0]+(bdy[2]/4)),bdy[1]+bdy[3]-1), 1)
- pygame.draw.line(window, (255,255,255), (bdy[0]+(bdy[2]/2)+2, (bdy[1]+(bdy[3]/2))), (bdy[0]+bdy[2]-2,(bdy[1]+(bdy[3]/2))), 1)
- pygame.draw.line(window, (255,255,255), ((bdy[0]+((bdy[2]/2)+(bdy[2]/4))), bdy[1]+2), ((bdy[0]+((bdy[2]/2)+(bdy[2]/4))),bdy[1]+bdy[3]-1), 1)
- if buttontype == 3:
- pygame.draw.rect(window, color, bdy, 2)
- pygame.draw.line(window, (255,255,255), (bdy[0]+2, (bdy[1]+(bdy[3]/2))), (bdy[0]+(bdy[2]/2)-2,(bdy[1]+(bdy[3]/2))), 1)
- pygame.draw.line(window, (255,255,255), (bdy[0]+(bdy[2]/2)+2, (bdy[1]+(bdy[3]/2))), (bdy[0]+bdy[2]-2,(bdy[1]+(bdy[3]/2))), 1)
- if buttontype == 4:
- pygame.draw.rect(window, color, bdy, 2)
- if self.E_BYPASS:
- pygame.draw.lines(window, (255,255,255), False, [(bdy[0]+2, bdy[1]+2), (bdy[0]+6, bdy[1]+bdy[3]) ,(bdy[0]+bdy[2], bdy[1])], 2)
- if buttontype == 5:
- pygame.draw.rect(window, color, bdy, 2)
- if DEFLECT == True:
- pygame.draw.lines(window, (255,255,255), False, [(bdy[0]+2, bdy[1]+2), (bdy[0]+6, bdy[1]+bdy[3]) ,(bdy[0]+bdy[2], bdy[1])], 2)
- if buttontype == 6:
- pygame.draw.rect(window, color, bdy, 2)
- if LONGITUDINAL == True:
- pygame.draw.lines(window, (255,255,255), False, [(bdy[0]+2, bdy[1]+2), (bdy[0]+6, bdy[1]+bdy[3]) ,(bdy[0]+bdy[2], bdy[1])], 2)
- if buttontype == 7:
- pygame.draw.rect(window, color, bdy, 2)
- pygame.draw.line(window, (255,255,255), (bdy[0]+8, (bdy[1]+(bdy[3]/2))), (bdy[0]+bdy[2]-8,(bdy[1]+(bdy[3]/2))), 6)
- if buttontype == 8:
- pygame.draw.rect(window, color, bdy, 2)
- pygame.draw.line(window, (255,0,0), (bdy[0]+5, bdy[1]+2), (bdy[0]+bdy[2]-5,(bdy[1]+bdy[3]-2)), 2)
- pygame.draw.line(window, (255,0,0), ((bdy[0]+5), bdy[1]+bdy[3]-2), (bdy[0]+bdy[2]-5, bdy[1]+2), 2)
- if buttontype == 9:
- pygame.draw.rect(window, color, bdy, 2)
- if self.ALLOW_REPULSION == True:
- pygame.draw.lines(window, (255,255,255), False, [(bdy[0]+2, bdy[1]+2), (bdy[0]+6, bdy[1]+bdy[3]) ,(bdy[0]+bdy[2], bdy[1])], 2)
- if buttontype == 10:
- pygame.draw.rect(window, color, bdy, 2)
- if PAUSE == False:
- pygame.draw.lines(window, (255,255,255), False, [(bdy[0]+2, bdy[1]+2), (bdy[0]+6, bdy[1]+bdy[3]) ,(bdy[0]+bdy[2], bdy[1])], 2)
- def draw_ELECTRON(self, posx, posy):
- pygame.draw.circle(self.SCREEN, self.E_COLORS[0], (posx, posy), 7, 7)
- def draw_PROTON(self, posx, posy):
- pygame.draw.circle(self.SCREEN, self.P_COLORS[0], (posx, posy), 12, 12)
- def cal_acceleration(self, axis, pos, pos2, charge):
- Dx = float(pos[0] - pos2[0])
- Dy = float(pos[1] - pos2[1])
- s = float(math.sqrt((Dx**2)+(Dy**2)))
- if s < 60: s = 60
- F = float(self.KONSTANT*(self.CHARGE**2)/(s**2))
- if charge == "++":
- a = float(-F / self.P_MASS)
- elif charge == "--":
- a = float(F / self.E_MASS)
- #print a
- XComponent = float(Dx/s)
- YComponent = float(Dy/s)
- if axis == "x":
- return - (a * XComponent)
- else:
- return - (a * YComponent)
- #return int((self.CHARGE**2) /math.sqrt(((pos[0]-pos2[0])**2)+((pos[1]-pos2[1]))**2))
- def set_D_vars(self):
- self.P_M= self.FONT.render(str(self.P_MASS), True, (255, 255, 255))
- self.P_Mbdy = self.P_M.get_rect()
- self.P_Mbdy.left = self.PNTEXTbdy.right + 20
- self.P_Mbdy.bottom=self.PNTEXTbdy.bottom
- self.N_P= self.FONT.render(str(len(self.PROTONSET)), True, (255, 255, 255))
- self.N_Pbdy = self.N_P.get_rect()
- self.N_Pbdy.left = self.NAPNTEXTbdy.right + 15
- self.N_Pbdy.bottom=self.NAPNTEXTbdy.bottom
- self.N_E= self.FONT.render(str(len(self.ELECTRONSET)), True, (255, 255, 255))
- self.N_Ebdy = self.N_E.get_rect()
- self.N_Ebdy.left = self.NAENTEXTbdy.right + 10
- self.N_Ebdy.bottom=self.NAENTEXTbdy.bottom
- def main(self):
- while True:
- self.set_D_vars()
- if self.RESET == True:
- self.ELECTRONSET = []
- self.PROTONSET = []
- self.original_pos_E = None
- self.drag_pos_E = None
- self.original_pos_P = None
- self.drag_pos_P = None
- self.RESET = False
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- if self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[1]):
- pygame.quit()
- sys.exit()
- elif self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[0]):
- self.RESET = True
- elif self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[2]) and self.P_MASS < 1020:
- self.P_MASS += 30
- elif self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[3]) and self.P_MASS > 30:
- self.P_MASS -= 30
- elif self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[4]):
- if self.ALLOW_REPULSION:
- self.ALLOW_REPULSION = False
- else:
- self.ALLOW_REPULSION = True
- elif self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[5]):
- if self.E_BYPASS:
- self.E_BYPASS = False
- else:
- self.E_BYPASS = True
- else:
- self.original_pos_E = ((event.pos[0], event.pos[1]))
- if event.button == 3:
- self.original_pos_P = ((event.pos[0], event.pos[1]))
- if event.type == MOUSEMOTION:
- if self.original_pos_E != None:
- self.drag_pos_E = ((event.pos[0], event.pos[1]))
- if self.original_pos_P != None:
- self.drag_pos_P = ((event.pos[0], event.pos[1]))
- if event.type == MOUSEBUTTONUP:
- if event.button == 1:
- if self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[1]):
- pygame.quit()
- sys.exit()
- elif self.collide_w_mouse(event.pos[0], event.pos[1], self.BUTTONBDYS[0]):
- self.RESET = True
- else:
- if self.original_pos_E != None:
- self.ELECTRONSET.append([self.original_pos_E[0], self.original_pos_E[1], 0, 0])
- if self.drag_pos_E != None:
- self.ELECTRONSET[self.ELECTRONSET.index([self.original_pos_E[0], self.original_pos_E[1], 0, 0])] = [self.original_pos_E[0], self.original_pos_E[1], self.drag_pos_E[0]-self.original_pos_E[0],self.drag_pos_E[1]-self.original_pos_E[1]]
- self.original_pos_E = None
- self.drag_pos_E = None
- if event.button == 3:
- if self.original_pos_P != None:
- self.PROTONSET.append([self.original_pos_P[0], self.original_pos_P[1], 0, 0])
- if self.drag_pos_P != None:
- self.PROTONSET[self.PROTONSET.index([self.original_pos_P[0], self.original_pos_P[1], 0, 0])] = [self.original_pos_P[0], self.original_pos_P[1], self.drag_pos_P[0]-self.original_pos_P[0],self.drag_pos_P[1]-self.original_pos_P[1]]
- self.original_pos_P = None
- self.drag_pos_P = None
- for posx1, posy1, vx1, vy1 in self.ELECTRONSET:
- posx, posy, vx, vy = posx1, posy1, vx1, vy1
- self.ELECTRONSET[self.ELECTRONSET.index([posx, posy, vx, vy])][0] += vx/5
- posx += vx/5
- self.ELECTRONSET[self.ELECTRONSET.index([posx, posy, vx, vy])][1] += vy/5
- posy += vy/5
- for posx2, posy2, vx2, vy2 in self.PROTONSET:
- self.ELECTRONSET[self.ELECTRONSET.index([posx, posy, vx, vy])][2] += self.cal_acceleration("x", (posx, posy), (posx2, posy2), "--")
- vx += self.cal_acceleration("x", (posx, posy), (posx2, posy2), "--")
- self.ELECTRONSET[self.ELECTRONSET.index([posx, posy, vx, vy])][3] += self.cal_acceleration("y", (posx, posy), (posx2, posy2), "--")
- vy += self.cal_acceleration("y", (posx, posy), (posx2, posy2), "--")
- self.draw_ELECTRON(int(posx), int(posy))
- posx, posy, vx, vy = 0, 0, 0, 0
- if self.E_BYPASS == False:
- for posx1, posy1, vx1, vy1 in self.ELECTRONSET:
- for posx2, posy2, vx2, vy2 in self.PROTONSET:
- Dx = float(posx1 - posx2)
- Dy = float(posy1 - posy2)
- s = float(math.sqrt((Dx**2)+(Dy**2)))
- if s < 13:
- del self.ELECTRONSET[self.ELECTRONSET.index([posx1, posy1, vx1, vy1])]
- break
- for posx1, posy1, vx1, vy1 in self.PROTONSET:
- posx, posy, vx, vy = posx1, posy1, vx1, vy1
- self.PROTONSET[self.PROTONSET.index([posx, posy, vx, vy])][0] += vx/5
- posx += vx/5
- self.PROTONSET[self.PROTONSET.index([posx, posy, vx, vy])][1] += vy/5
- posy += vy/5
- if self.ALLOW_REPULSION:
- for posx2, posy2, vx2, vy2 in self.PROTONSET:
- self.PROTONSET[self.PROTONSET.index([posx, posy, vx, vy])][2] += (self.cal_acceleration("x", (posx, posy), (posx2, posy2), "++"))
- vx += self.cal_acceleration("x", (posx, posy), (posx2, posy2), "++")
- self.PROTONSET[self.PROTONSET.index([posx, posy, vx, vy])][3] += (self.cal_acceleration("y", (posx, posy), (posx2, posy2), "++"))
- vy += self.cal_acceleration("y", (posx, posy), (posx2, posy2), "++")
- self.draw_PROTON(int(posx), int(posy))
- posx, posy, vx, vy = 0, 0, 0, 0
- if self.drag_pos_E != None and self.original_pos_E != None:
- pygame.draw.line(self.SCREEN, (255, 255, 255), self.original_pos_E, self.drag_pos_E, 1)
- if self.drag_pos_P != None and self.original_pos_P != None:
- pygame.draw.line(self.SCREEN, (255, 255, 255), self.original_pos_P, self.drag_pos_P, 1)
- for posx1, posy1, vx1, vy1 in self.ELECTRONSET:
- if posx1 > 900 or posx1 < -100 or posy1 > 700 or posy1 < -100:
- del self.ELECTRONSET[self.ELECTRONSET.index([posx1, posy1, vx1, vy1])]
- for posx1, posy1, vx1, vy1 in self.PROTONSET:
- if posx1 > 900 or posx1 < -100 or posy1 > 700 or posy1 < -100:
- del self.PROTONSET[self.PROTONSET.index([posx1, posy1, vx1, vy1])]
- self.draw_inc_dec_buttons(self.SCREEN, self.BUTTONBDYS[1], 8, (255,0,0))
- self.draw_inc_dec_buttons(self.SCREEN, self.BUTTONBDYS[0], 7, (255,255,255))
- self.draw_inc_dec_buttons(self.SCREEN, self.BUTTONBDYS[2], 2, (255,255,255))
- self.draw_inc_dec_buttons(self.SCREEN, self.BUTTONBDYS[3], 3, (255,255,255))
- self.draw_inc_dec_buttons(self.SCREEN, self.BUTTONBDYS[4], 9, (255,255,255))
- self.draw_inc_dec_buttons(self.SCREEN, self.BUTTONBDYS[5], 4, (255,255,255))
- self.SCREEN.blit(self.RTEXT, self.RTEXTbdy)
- self.SCREEN.blit(self.CTEXT, self.CTEXTbdy)
- self.SCREEN.blit(self.KTEXT, self.KTEXTbdy)
- self.SCREEN.blit(self.PNTEXT, self.PNTEXTbdy)
- self.SCREEN.blit(self.APNTEXT, self.APNTEXTbdy)
- self.SCREEN.blit(self.P_M, self.P_Mbdy)
- self.SCREEN.blit(self.NAENTEXT, self.NAENTEXTbdy)
- self.SCREEN.blit(self.NAPNTEXT, self.NAPNTEXTbdy)
- self.SCREEN.blit(self.BAPNTEXT, self.BAPNTEXTbdy)
- self.SCREEN.blit(self.N_P, self.N_Pbdy)
- self.SCREEN.blit(self.N_E, self.N_Ebdy)
- pygame.display.update()
- self.SCREEN.fill((0, 0, 0))
- self.FPS_CLOCK.tick(self.FPS)
- self.E_COLORS = [self.E_COLORS[1], self.E_COLORS[0]]
- self.P_COLORS = [self.P_COLORS[1], self.P_COLORS[0]]
- SIN = SIN()
- SIMULATOR = SIM()
- SIMULATOR.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement