def __new__(cls, *args, **kwargs): instance= Vec2.__new__(cls, *args, **kwargs ) instance.ID= Centroid.ID ; Centroid.ID+=1 instance.clear() return instance
def setX(self, x): self.setPos(Vec2(x, self.pos.y))
def setY(self, y): self.setPos(Vec2(self.pos.x, y))
def __new__(self, r1, r2=None): if type(r2) is None: r1, r2 = r1 r1x, r1y = r1 r2x, r2y = r2 return tuple.__new__(Seg, (Vec2(r1x, r1y), Vec2(r2x, r2y)))
from Guarana import Guarana from Human import Human from Sheep import Sheep from SowThisle import SowThisle from Turtle import Turtle from Wolf import Wolf from CyberSheep import CyberSheep from Hogweed import Hogweed from World import World from Vec2 import Vec2 pygame.init() clock = pygame.time.Clock() world = World(Vec2(10, 10)) world.spawnEntity(Vec2(0, 0), Human); world.spawnEntity(Vec2(10, 10), Wolf); world.spawnEntity(Vec2(10, 11), Wolf); world.spawnEntity(Vec2(2, 2), Sheep); world.spawnEntity(Vec2(2, 3), Sheep); world.spawnEntity(Vec2(19, 7), Fox); world.spawnEntity(Vec2(18, 6), Fox); world.spawnEntity(Vec2(6, 19), Turtle); world.spawnEntity(Vec2(7, 18), Turtle); world.spawnEntity(Vec2(13, 19), Antelope); world.spawnEntity(Vec2(14, 18), Antelope); world.spawnEntity(Vec2(19, 2), Belladona); world.spawnEntity(Vec2(18, 3), Grass); world.spawnEntity(Vec2(2, 19), Guarana); world.spawnEntity(Vec2(3, 18), Hogweed);
def clear_force(self): self.force = Vec2(0, 0)
def nothing(self): self.overlap = -math.inf self.normal = Vec2(0,0)
def __init__(self): pygame.init() self.Screen = pygame.display.set_mode((1200, 600)) self.ScreenRect = self.Screen.get_rect() pygame.display.set_caption("School") self.Font = pygame.sysfont.SysFont(None, 48) self.Backgrounds = ( (0, 0, 0), (0, 255, 0), (0, 0, 255), ) self.BackgroundIndex = 0 # This will be hard-coded until the map creator is made: self.Points = ((77, 552), (131, 170), (386, 270), (487, 83), (640, 321), (742, 529), (478, 481), (1108, 28), (1100, 251), (971, 412)) self.Shapes = [] for x in range(10): self.Shapes.append(cCircle(Vec2(self.Points[x]), randint(10, 50), Vec2(0, 0), 400, (255, 255, 255), self.Screen, self.ScreenRect)) self.Shapes[-1].ClampToScreen() self.SelectedShape = None self.fStartingTime = time() nFrameCounter = 0 self.FPS = 0 while True: fOld = self.fStartingTime self.fStartingTime = time() self.fElapsedTime = self.fStartingTime - fOld nFrameCounter += 1 if nFrameCounter % 10 == 0: try: self.FPS = round(1 / self.fElapsedTime) except ZeroDivisionError: self.FPS = self.FPS self.MousePos = Vec2(pygame.mouse.get_pos()) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: self.KeydownEvents(event) elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP: self.MouseEvents(event) Collided = False for shape in self.Shapes: if shape is not self.SelectedShape and self.SelectedShape is not None: if shape.CheckCollision(self.SelectedShape): self.SelectedShape.Color = self.SelectedShape.CollisionColor shape.Color = shape.CollisionColor Collided = True else: shape.Color = shape.InitColor if self.SelectedShape is not None: self.SelectedShape.Update(self.fElapsedTime, self.MousePos) self.SelectedShape.ClampToScreen() if Collided: self.SelectedShape.Color = self.SelectedShape.CollisionColor else: self.SelectedShape.Color = self.SelectedShape.SelectedColor self.Screen.fill(self.Backgrounds[self.BackgroundIndex]) for shape in self.Shapes: if shape is not self.SelectedShape or self.SelectedShape is None: shape.Draw() if self.SelectedShape is not None: self.SelectedShape.Draw() self.Screen.blit(pygame.font.Font.render(self.Font, str(self.FPS), True, (255, 255, 255)), (0, 0)) pygame.display.flip()
def main(): # set up pygame and window pygame.init() bg_color = WHITE screen.fill(bg_color) # variables for determining if red/green light is on start_value = random.randrange( 1, 10) # random determination if red or green starts if start_value % 2 == 0: green_on = True red_on = False else: green_on = False red_on = True change_light = True tot_seconds = 0 # start value # timer start_ticks = pygame.time.get_ticks() # player dot player = Circle(radius=20, color=(0, 50, 255), pos=Vec2(400, 600), mass=float('inf')) goal = Circle(radius=20, color=(255, 0, 0), pos=Vec2(400, 100), mass=float('inf')) objects.append(player) objects.append(goal) # game loop running = True idle = True # set idle state to true initially # clock within pygame clock = pygame.time.Clock() fps = 60 dt = 1 / fps screen.fill(bg_color) for o in objects: o.update(dt) o.draw(screen) pygame.display.flip() idle_lights() pir.wait_for_motion() countdown() while running: seconds = (pygame.time.get_ticks() - start_ticks) / 1000 screen.fill(bg_color) if tot_seconds < seconds: # once we surpass the time limit for the light, set to true and swap lights change_light = True if change_light: num_seconds = random.randrange( 3, 10) # random duration between 3 and 10 seconds for each light tot_seconds = seconds + num_seconds # number to reach before print("wait for: ", num_seconds) print("target time: ", tot_seconds) print("green: ", green_on) print("red: ", red_on) if green_on: # if green light is on, turn it off and turn red on GREEN.off() RED.on() green_on = False red_on = True else: GREEN.on() RED.off() red_on = False green_on = True change_light = False # don't go in this if statement while we're counting #if pir.motion_detected: # print("motion") #else: # print("no motion") for o in objects: o.update(dt) o.draw(screen) if button.is_pressed: player.pos.y -= 1 if green_on and pir.motion_detected: player.pos.y -= 1 if red_on and pir.motion_detected: move_player(player) distanceFromGoal = player.pos - goal.pos if distanceFromGoal.mag() < 39: print("You win!") running = False for e in pygame.event.get(): # user clicks closed button if e.type == pygame.QUIT: running = False pygame.display.flip() clock.tick(fps) / 1000 RED.off() GREEN.off() pygame.quit()
def set_pos(self, pos): self.min = Vec2(pos.x - self.width / 2, pos.y - self.height / 2) self.max = Vec2(pos.x + self.width / 2, pos.y + self.height / 2)
def __init__(self, pos, width, height): self.width = width self.height = height self.min = Vec2(pos.x - width / 2, pos.y - height / 2) self.max = Vec2(pos.x + width / 2, pos.y + height / 2)
from Plant import Plant from Antelope import Antelope from Belladona import Belladona from Fox import Fox from Grass import Grass from Guarana import Guarana from Human import Human from Sheep import Sheep from SowThisle import SowThisle from Turtle import Turtle from Wolf import Wolf from CyberSheep import CyberSheep from Hogweed import Hogweed from Vec2 import Vec2 ScreenSize = Vec2(1000, 800) CellSize = 30 CellDist = CellSize + 5 class World: font = None def __init__(this, size): World.font = pygame.font.Font(None, 15) this.screen = pygame.display.set_mode((ScreenSize.x, ScreenSize.y)) this.reset(size) def input(this): for event in pygame.event.get(): if event.type == pygame.KEYDOWN:
def __new__(cls, *args, **kwargs): instance = Vec2.__new__(cls, *args, **kwargs) instance.ID = Centroid.ID Centroid.ID += 1 instance.clear() return instance
def __init__(self, m=1, v=Vec2(0, 0), pos=Vec2(0, 0), r=0): self.__m = m self.__v = dc(v) self.__pos = dc(pos) self.__r = r
def v(self): return self.__v @property def pos(self): return self.__pos @property def r(self): return self.__r def __repr__(self): return self.__str__() @pos.setter def pos(self, pos): self.__pos = pos @v.setter def v(self, v): self.__v = v # # @y.setter # def y(self, y): # self.__y = y if __name__ == '__main__': a = Atom(v=Vec2(1, 2), pos=Vec2(1, 1)) print(a)
from Vec2 import Vec2 a = Vec2(3, 4) print("a =", a) b = Vec2(2, -1) print("b =", b) s = 5 print("a*s =", a * s) print("s*a =", s * a) print("a @ b =", a @ b) print("a =", a) print("b =", b) z = Vec2(0, 0) if a: print("True") else: print("False") if b: print("True") else: print("False") if z: print("True")