def pop_cb(): x = random.randint(-WIDTH / 2, WIDTH / 2) y = random.randint(-HEIGHT / 2, HEIGHT / 2) kind = random.choice(POPS) size = random.randint(1, 3) color = random.choice([POPCOLOR, FGCOLOR]) engine.add_obj(Pop(x, y, size, kind, color))
def maketilegrid(): for row in range(GRIDROWS): for col in range(GRIDCOLS): x = col * B - WIDTH//2 y = HEIGHT//2 - row * B tile = Tile(x, y) engine.add_obj(tile)
def place_default_post_cb(): ddy = 13 dy = 0 y = 126 ddx = 6 dx= 0 x = 10 dds = 0.03 ds = 0 s = 0.3 count = 0 while(count < 7): obj = Post(x, y, 0, 0, 1) engine.add_obj(obj) obj = Post(-x, y, 0, 0, -1) engine.add_obj(obj) dy += ddy y -= dy dx += ddx x += dx ds += dds s += ds count += 1
def fire_cb(x, y): if y < GUNY: return theta = math.atan2(y - GUNY, x - GUNX) if theta == 0: return # from http://en.wikipedia.org/w/index.php?title=Projectile_motion&oldid=540166443#The_maximum_height_of_projectile, rearranging for v0 h = y - GUNY v0 = math.sqrt((2 * g * h) / (math.sin(theta) ** 2)) engine.add_obj(Missile(GUNX, GUNY, v0, theta)) return if x != 0: # from http://www.physicsforums.com/showthread.php?t=419561, # in part m = (y - GUNY) / (x - GUNX) if x - GUNX < 0: deltax = GUNX - (CLIMBRATE / math.sqrt(1 + m**2)) else: deltax = GUNX + (CLIMBRATE / math.sqrt(1 + m**2)) deltay = abs(m * deltax) else: deltax = 0 deltay = CLIMBRATE engine.add_obj(Missile(GUNX, GUNY, deltax, deltay))
def play(postfn=None): global score, cities score = 0 cities = NCITIES engine.add_obj(Ground(MINX, GROUNDY, MAXX, MINY)) draw_score() draw_stars() # we built this city on rock and roll xcoords = list(range(MINX, MAXX, WIDTH // (NCITIES+1)))[1:-1] for x in xcoords: engine.add_obj(City(x, GROUNDY)) engine.add_random_event(NEWENEMYPROB, newenemy_cb) engine.register_collision(City, Enemy, coll_city1) engine.register_collision(Enemy, City, coll_city2) engine.register_collision(Ground, Enemy, coll_ground1) engine.register_collision(Enemy, Ground, coll_ground2) engine.register_collision(Enemy, Missile, coll_air2air) engine.register_collision(Missile, Enemy, coll_air2air) engine.register_collision(Ground, Missile, coll_groundnoboom1) engine.register_collision(Missile, Ground, coll_groundnoboom2) if postfn: postfn() engine.engine()
def coll_circle(obj1, obj2): if iscoll_circle(obj1, obj2): x1, y1, r1 = obj1.get_bc() x2, y2, r2 = obj2.get_bc() engine.add_obj(Boom(x1, y1, r1)) engine.add_obj(Boom(x2, y2, r2)) engine.del_obj(obj1) engine.del_obj(obj2)
def spawn_post_cb(): #only spawns when an even number of posts exists if s.numPosts % 2 == 0: obj = Post(s.horizonX+10 - s.space, s.horizonY, 0, 0, -1) engine.add_obj(obj) obj = Post(s.horizonX+10, s.horizonY, 0, 0, 1) engine.add_obj(obj) s.numPosts = s.numPosts + 2
def keyboard_cb(key): if key == 'space': box = Box() engine.add_obj(box) elif key == 'Escape': print("Au revoir... Giscard!") engine.exit_engine() else: print(key)
def ufo_cb(): if s.ufoactive: return s.ufoactive = True # change the horizontal direction up if random.random() < 0.5: engine.add_obj(UFO(MINX, UFOY, UFOSPEED, 0)) else: engine.add_obj(UFO(MAXX, UFOY, -UFOSPEED, 0))
def newpipe_cb(): s.pipecounter -= 1 if s.pipecounter <= 0: s.pipecounter = PIPEFREQ lheight = random.randint(PIPEMIN, PIPEUNITS-PIPEGAP-PIPEMIN) uheight = PIPEUNITS-lheight-PIPEGAP l = lowerpipe(ht2name(lheight), lheight*PIPEUNIT) u = upperpipe(ht2name(uheight), uheight*PIPEUNIT) engine.add_obj(l) engine.add_obj(u)
def shoot(sender): bh = sender.heading() abs_spd = math.sqrt(sender.xspeed ** 2 + sender.yspeed ** 2) + 8 bxs = abs_spd * math.sin(-3.1415926535 * bh / 180) bys = abs_spd * math.cos(-3.1415926535 * bh / 180) bx = sender.x + 4 * basesize * \ math.sin(-3.1415926535 * sender.heading() / 180) by = sender.y + 4 * \ basesize * math.cos(-3.1415926535 * sender.heading() / 180) bullet = Bullet(bxs, bys, bx, by, bh) engine.add_obj(bullet)
def spawn_asteroid_cb(): if(s.curas >= MAXAS): return xpos = random.randint(MINX, MAXX) ypos = random.randint(MINY, MAXY) xsize = random.randint(1, 5) ysize = random.randint(1, 5) deltx = random.randint(-5, 5) delty = random.randint(-5, 5) obj = Asteroid(xpos, ypos, deltx, delty, xsize, ysize) engine.add_obj(obj) s.curas += 1
def input_cb(key): x, y, r = s.me.get_bc() if key == "q" or key == "Q": engine.exit_engine() elif key == "Left" and x - r > MINX: s.me.setx(x - MOVELEN) s.me.update() elif key == "Right" and x + r < MAXX: s.me.setx(x + MOVELEN) s.me.update() elif key == "space": engine.add_obj(MyShot(x, y, 0, MYSHOTSPEED))
def play(): global s s = S() engine.init_engine() engine.set_keyboard_handler(input_cb) engine.add_random_event(1.0, newpipe_cb) engine.register_collision(Me, Pipe, coll_cb) engine.register_collision(Pipe, Me, coll_cb2) s.me = Me() engine.add_obj(s.me) s.playing = True engine.engine()
def move(self): self.countdown += 1 if self.countdown > self.seuil: self.seuil = random.randrange(50, 70) self.countdown = 0 enplus = 0 if self.x < rocket.x: enplus = 180 if self.x == rocket.x: angle = 0 else: angle = enplus + math.degrees( math.atan((self.y - rocket.y) / (self.x - rocket.x))) engine.add_obj(Bullet(self.x, self.y, angle))
def inputs(key): global count global county if key == 'Q' or key == 'q': engine.exit_engine() if key == 'space': if buggy1.deltay == 0: buggy1.deltay = 5 if key == 'Return' and jumping == False and count != 3 and count < 4 and county < 2: county = county + 1 count = count + 2 rocket1 = _rocket() rocket2 = _rocket2() engine.add_obj(rocket2) engine.add_obj(rocket1)
def __init__(self, L, x, y, color): # init parent object first super().__init__(x, y, 0, 0, 'none', BGCOLOR) # create all segments; each segment gets a list of just # its tweening data - ideally a game object should be # able to catch notifications of being added to the # game engine and we could move the add_obj to there, # but that doesn't happen at present for i in range(len(L[0])): segL = [X[i] for X in L] seg = Segment(segL, self, color) engine.add_obj(seg) # create head engine.add_obj(Head(self, color))
def __init__(self, L, x, y, color): # init parent object first super().__init__(x, y, 0, 0, 'none', BGCOLOR) # create all segments; each segment gets a list of just # its tweening data - ideally a game object should be # able to catch notifications of being added to the # game engine and we could move the add_obj to there, # but that doesn't happen at present for i in range(len(L[0])): segL = [ X[i] for X in L ] seg = Segment(segL, self, color) engine.add_obj(seg) # create head engine.add_obj(Head(self, color))
def input_cb(key): if key == 'q' or key == 'Q': exit() if key == 'space': if not s.playing: # replay raise Replay() else: if(s.shots <= MAXSHOTS): engine.add_obj(MyShot(s.me.getx(), s.me.gety(), MYSHOTSPEED * s.me.getdirx() + s.me.getdelx(), MYSHOTSPEED * s.me.getdiry() + s.me.getdely())) s.shots += 1 if key == 'Up': s.me.moveu() elif key =='Right': s.me.mover() elif key =='Left': s.me.movel()
def keyboard_lvl(key): """keyboard manager for the level selection menu""" total_lvl_number = len(listdir("Files/lvls/")) y0 = 4 * Menu.LEVEL_LINE_HEIGHT + Menu.FONT_SIZE if key == "Up": if Menu.cursor_position_on_screen > 0: Menu.cursor_position_on_screen -= 1 elif Menu.cursor_position == 0: # and Menu.cursor_position_on_screen == 0 Menu.cursor_position_on_screen = Menu.MAX_NB_LVL_PER_PAGE - 1 # in other cases, the arrow remains on the top Menu.cursor_position = (Menu.cursor_position - 1) % (total_lvl_number + 1) # + 1 for the return button Menu.select_arrow.y = y0 - Menu.LEVEL_LINE_HEIGHT * Menu.cursor_position_on_screen Menu.display_level() if key == "Down": if Menu.cursor_position_on_screen < Menu.MAX_NB_LVL_PER_PAGE - 1: Menu.cursor_position_on_screen += 1 elif Menu.cursor_position == total_lvl_number: # and Menu.cursor_position_on_screen == MAX_NB_LVL_PER_PAGE - 1 Menu.cursor_position_on_screen = 0 # in other cases, the arrow remains on the bottom Menu.cursor_position = (Menu.cursor_position + 1) % (total_lvl_number + 1) # + 1 for the return button Menu.select_arrow.y = y0 - Menu.LEVEL_LINE_HEIGHT * Menu.cursor_position_on_screen Menu.display_level() if key == "Return": engine.del_obj(Menu.select_arrow) engine.del_obj(Menu.lvl_rect) Menu.select_arrow = Menu.lvl_rect = None if Menu.cursor_position == 0: Menu.load_main_menu() else: lvl = "lvl" + str(Menu.cursor_position) engine.init_engine() game.Game.init_all(lvl) engine.add_obj(game.Game.ground) engine.add_obj(game.Game.rocket) game.cheat() engine.set_keyboard_handler(game.keyboard_cb) game.load()
def __init__(self, L, x, y, color): # keep weak references to component segments; it doesn't # prevent GC from removing them, but allows us to keep # this parent class around until all the segments are gone - # otherwise, the Bird parent class gets deleted once it's # OOB, and the last part of the flapping wing (no longer # seeing the parent's motion updates) just stays at the # edge of the screen flapping away import weakref self.components = weakref.WeakSet() # init parent object first super().__init__(x, y, -0.1, -0.025, 'none', BGCOLOR) # create all segments; see comments in Figure class for i in range(len(L[0])): segL = [X[i] for X in L] seg = Segment(segL, self, color) engine.add_obj(seg) self.components.add(seg)
def input_cb(key): if key == 'q' or key == 'Q': exit() if key == 'space': if not s.playing: # replay raise Replay() else: if (s.shots <= MAXSHOTS): engine.add_obj( MyShot(s.me.getx(), s.me.gety(), MYSHOTSPEED * s.me.getdirx() + s.me.getdelx(), MYSHOTSPEED * s.me.getdiry() + s.me.getdely())) s.shots += 1 if key == 'Up': s.me.moveu() elif key == 'Right': s.me.mover() elif key == 'Left': s.me.movel()
def __init__(self, L, x, y, color): # keep weak references to component segments; it doesn't # prevent GC from removing them, but allows us to keep # this parent class around until all the segments are gone - # otherwise, the Bird parent class gets deleted once it's # OOB, and the last part of the flapping wing (no longer # seeing the parent's motion updates) just stays at the # edge of the screen flapping away import weakref self.components = weakref.WeakSet() # init parent object first super().__init__(x, y, -0.1, -0.025, 'none', BGCOLOR) # create all segments; see comments in Figure class for i in range(len(L[0])): segL = [ X[i] for X in L ] seg = Segment(segL, self, color) engine.add_obj(seg) self.components.add(seg)
def play(): global s s = S() engine.init_engine() engine.set_keyboard_handler(input_cb) s.me = Me() engine.add_obj(s.me) engine.add_random_event(0.01, spawn_asteroid_cb) engine.register_collision(Asteroid, MyShot, col_asteroid) engine.register_collision(MyShot, Asteroid, col_asteroid2) engine.register_collision(Me, Asteroid, col_gameover) engine.register_collision(Asteroid, Me, col_gameover) draw_score() s.playing = True engine.engine()
def keyboard_cb(key): global rocket, COUNTDOWN, gravity if key == 'Escape': engine.exit_engine() if key == 'Up': rocket.speed -= SPEEDBOOST rocket.shape = skin + " powered" COUNTDOWN = 20 gravity = max(0, gravity - 1) if key == 'Left': rocket.angle += 30 if key == 'Right': rocket.angle -= 30 if key == 'space': engine.add_obj(NiceBullet(rocket.x, rocket.y, 90 + rocket.angle))
def col_asteroid(obj1, obj2): if iscoll_circle(obj1, obj2): x1, y1, r1 = obj1.get_bc() x2, y2, r2 = obj2.get_bc() engine.del_obj(obj1) engine.del_obj(obj2) s.shots -= 1 s.curas -= 1 s.score += 1 draw_score() #create 2 new asteroids if (obj1.sizex > 2 or obj1.sizey > 2): xpos = obj1.x ypos = obj1.y if (obj1.sizex == 1): xsize = 1 else: xsize = random.randint(1, obj1.sizex - 1) if (obj1.sizey == 1): ysize = 1 else: ysize = random.randint(1, obj1.sizey - 1) deltx = random.randint(-1, 1) delty = random.randint(-1, 1) obj = Asteroid(xpos, ypos, deltx, delty, xsize, ysize) engine.add_obj(obj) xpos = obj1.x ypos = obj1.y if (obj1.sizex == 1): xsize = 1 else: xsize = random.randint(1, obj1.sizex - 1) if (obj1.sizey == 1): ysize = 1 else: ysize = random.randint(1, obj1.sizey - 1) deltx = random.randint(-1, 1) delty = random.randint(-1, 1) obj = Asteroid(xpos, ypos, deltx, delty, xsize, ysize) engine.add_obj(obj)
def col_asteroid(obj1, obj2): if iscoll_circle(obj1, obj2): x1, y1, r1 = obj1.get_bc() x2, y2, r2 = obj2.get_bc() engine.del_obj(obj1) engine.del_obj(obj2) s.shots -= 1 s.curas -= 1 s.score += 1 draw_score() #create 2 new asteroids if(obj1.sizex > 2 or obj1.sizey > 2): xpos = obj1.x ypos = obj1.y if(obj1.sizex == 1): xsize = 1 else: xsize = random.randint(1, obj1.sizex-1) if(obj1.sizey == 1): ysize = 1 else: ysize = random.randint(1, obj1.sizey-1) deltx = random.randint(-1, 1) delty = random.randint(-1, 1) obj = Asteroid(xpos, ypos, deltx, delty, xsize, ysize) engine.add_obj(obj) xpos = obj1.x ypos = obj1.y if(obj1.sizex == 1): xsize = 1 else: xsize = random.randint(1, obj1.sizex-1) if(obj1.sizey == 1): ysize = 1 else: ysize = random.randint(1, obj1.sizey-1) deltx = random.randint(-1, 1) delty = random.randint(-1, 1) obj = Asteroid(xpos, ypos, deltx, delty, xsize, ysize) engine.add_obj(obj)
def play(): global s s = S() engine.init_engine() engine.set_keyboard_handler(input_cb) s.me = Me() engine.add_random_event(0.01, leftturn_straight_cb) engine.add_random_event(0.01, rightturn_straight_cb) engine.add_obj(s.me) place_default_post_cb() engine.register_collision(Me, Post, col_post) engine.register_collision(Post, Me, col_post2) draw_text() draw_score() s.playing = True engine.engine()
def keyboard_cb(key): global speed, rocket, countdown, angle, gravity if key == 'Escape': engine.exit_engine() if key == 'Up': speed -= speedboost rocket.shape = skin + " powered" countdown = 20 gravity = 0 if key == 'Left': angle += 30 if key == 'Right': angle -= 30 if key == 'm': speed = 0 rocket = Rocket() engine.add_obj(rocket)
def keyboard_cb(key): """keyboard manager""" turtle.setx(0) turtle.sety(0) Stats.key_pressed += 1 Game.freeze_spawn = False if key == 'Return': # Enter Game.pause = not Game.pause if key == 'Escape': engine.exit_engine() Stats.display_stats() if not Game.pause: if key == 'Up' or key == 'z': Game.rocket.rocket_up() if key == 'Left' or key == 'q': Game.rocket.rocket_left() if key == 'Right' or key == 'd': Game.rocket.rocket_right() if key == 'space': engine.add_obj( bullets.Bullet(Game.rocket.x, Game.rocket.y, 90 + Game.rocket.angle, True)) Stats.bullets_fired += 1
def isoob(self): global ground, posi, posj if super().isoob(): for door in LDOOR[posi][posj]: engine.del_obj(door) if self.y < -300: posi += 1 self.y = 280 elif self.y > 300: posi -= 1 self.y = -280 elif self.x < -300: posj -= 1 self.x = 280 elif self.x > 300: posj += 1 self.x = -280 engine.del_obj(ground) ground = Ground(level[posi][posj]) for door in LDOOR[posi][posj]: engine.add_obj(door) return False
def display_skin(): y_arrow = - game.Game.LENGTH / 6 arrow_height = 34.6 y0 = y_arrow + arrow_height / 2 + 20 r = Menu.skin_radius_list[Menu.skin_cursor] pos_skin_unpowered = 0, y0 + (Menu.SKIN_WINDOW_HEIGHT - r) / 3 pos_skin_powered = 0, pos_skin_unpowered[1] + (Menu.SKIN_WINDOW_HEIGHT + 2 * r) / 3 font_size = 15 txt_pos = 0, y_arrow - font_size / 2 - 5 if Menu.current_skin is not None: engine.del_obj(Menu.current_skin) engine.del_obj(Menu.current_skin_powered) Menu.current_skin = engine.GameObject(*pos_skin_unpowered, 0, 0, Menu.skin_list[Menu.skin_cursor], "white", True) Menu.current_skin_powered = engine.GameObject(*pos_skin_powered, 0, 0, Menu.skin_list[Menu.skin_cursor] + "_powered", "white", True) Menu.current_skin.angle = 90 Menu.current_skin_powered.angle = 180 engine.add_obj(Menu.current_skin) Menu.skin_rect.draw() turtle.setpos(*txt_pos) turtle.pencolor("black") turtle.write(Menu.skin_list[Menu.skin_cursor], align="center", font=("Arial", font_size, "normal"))
def load_main_menu(): print("Loading the main menu...") pos_arrow = - game.Game.LENGTH / 5, game.Game.LENGTH / 9 pos_title = 0, game.Game.LENGTH / 3 x0, y0 = pos_arrow[0] + 40, pos_arrow[1] - Menu.LINE_HEIGHT / 2 + 5 # + 5 to have arrow and text aligned font_size_title = 55 engine.set_keyboard_handler(Menu.keyboard_main) assert Menu.select_arrow is None, "Select arrow already initialized" Menu.select_arrow = engine.GameObject(*pos_arrow, 0, 0, "select_arrow", "black") engine.add_obj(Menu.select_arrow) Menu.cursor_position = 0 turtle.clear() turtle.setpos(*pos_title) # options to select turtle.write("Name of the game", align="center", font=("Arial", font_size_title, "normal")) str_list = ["Choose a level", "Tutorial", "Choose a skin", "Quit game"] for i in range(len(str_list)): turtle.setpos(x0, y0 - i * Menu.LINE_HEIGHT) turtle.write(str_list[i], font=("Arial", Menu.FONT_SIZE, "normal")) if not Menu.engine_launched: Menu.engine_launched = True engine.engine()
def init(): global score score = 0 engine.init_screen(W, H) engine.init_engine() turtle.bgcolor('black') engine.set_keyboard_handler(inputs) engine.add_random_event(0.01, rock) engine.add_random_event(0.01, hole) engine.add_random_event(0.1, newStar) engine.add_obj(buggy1) engine.add_obj(_ground(120, 320, -320, 240)) draw_score() engine.add_obj(_starstuff(1, 1)) engine.register_collision(_buggy, _rock, collbr) engine.register_collision(_rock, _buggy, collrb) engine.register_collision(_buggy, _hole, collbh) engine.register_collision(_hole, _buggy, collhb) engine.register_collision(_rocket, _rock, collrtr) engine.register_collision(_rock, _rocket, collrrt)
def play(): global s s = S() engine.init_engine() engine.set_keyboard_handler(input_cb) engine.add_obj(Ground(MINX, GROUNDY, MAXX, MINY)) s.me = Me(0, GROUNDY + 10) engine.add_obj(s.me) # the grid o' evil for i in range(INVADERROWS): for j in range(INVADERCOLS): enemy = Invader(XGRIDSTART + j * INVADERHSPACE, YGRIDSTART - i * INVADERVSPACE, INVADERSPEED, 0) s.invaders.append(enemy) engine.add_obj(enemy) engine.add_random_event(FIREPROB, fire_cb) engine.add_random_event(UFOPROB, ufo_cb) # not quite so random - do this at start of each time step # order is important - if no invaders left, gridmove won't be happy engine.add_random_event(1.0, winlose_cb) engine.add_random_event(1.0, gridmove_cb) engine.register_collision(Ground, Bomb, coll_ground1) engine.register_collision(Bomb, Ground, coll_ground2) engine.register_collision(UFO, MyShot, coll_air2air_bonus) engine.register_collision(MyShot, UFO, coll_air2air_bonus) engine.register_collision(Invader, MyShot, coll_air2air) engine.register_collision(MyShot, Invader, coll_air2air) engine.register_collision(Ground, Invader, coll_gameover1) engine.register_collision(Invader, Ground, coll_gameover2) engine.register_collision(Me, Bomb, coll_loselife1) engine.register_collision(Bomb, Me, coll_loselife2) draw_score() engine.engine()
def hole(): hole1 = _hole() engine.add_obj(hole1)
def newenemy_cb(): xpos = random.randint(MINX, MAXX) obj = Enemy(xpos, MAXY, math.copysign(XFALLRATE, xpos)*-1.25, YFALLRATE) engine.add_obj(obj)
def fire_cb(): # pick an invader randomly obj = random.choice(s.invaders) x, y = obj.get_bc()[:2] # bombs away! engine.add_obj(Bomb(x, y))
def rock(): rock1 = _rock() engine.add_obj(rock1)
def collbr(buggy1, rock1): if buggy1.intersect(rock1.getx(), rock1.gety()): engine.del_obj(buggy1) engine.add_obj(Boom(0, -120, 20))
def collbh(buggy1, hole1): if buggy1.intersect(hole1.getx(), hole1.gety()): engine.del_obj(buggy1) engine.add_obj(Boom(0, -120, 20))
turtle.fd(B) # roof turtle.rt(45) turtle.fd(B * 3/4) # windshield turtle.lt(45) turtle.fd(B) # hood turtle.rt(90) turtle.fd(B * 3/4) # front turtle.rt(90) turtle.fd(B * 1/7) turtle.lt(90) turtle.circle(-B/2, 180) # front tire turtle.lt(90) turtle.fd(B) turtle.lt(90) turtle.circle(-B/2, 180) # back tire turtle.lt(90) turtle.fd(B * 1/7) turtle.rt(90) turtle.fd(B * 5/6) # back turtle.end_poly() poly = turtle.get_poly() turtle.register_shape('car', poly) if __name__ == '__main__': engine.init_screen(WIDTH, HEIGHT) engine.init_engine() makeshape() car = Car() engine.add_obj(car) engine.engine()
def keyboard_cb(key): if key == 'space': box = Box() engine.add_obj(box) if key == 'Escape' : engine.exit_engine()
def makescenery(): turtle.bgcolor(BGCOLOR) makeshape() # ground makebigrect(GRHEIGHT, HEIGHT - abs(GRHEIGHT), GRCOLOR) # ceiling makebigrect(HEIGHT / 2, HEIGHT / 2 - (TOPROW + SPACER), ROOFCOLOR) # top row engine.add_obj(TargetWindow(TOPROW)) for i in range(NTARGETS): spacing = WIDTH / NTARGETS x = i * spacing - WIDTH / 2 engine.add_obj(Target(x, TOPROW, TOPROWDELTA)) engine.add_obj(Obscurer(TWWIDTH/-2 - TWWIDTH, TOPROW)) engine.add_obj(Obscurer(TWWIDTH/2, TOPROW)) # bottom row engine.add_obj(TargetWindow(BOTROW)) for i in range(NTARGETS): spacing = WIDTH / NTARGETS x = i * spacing - WIDTH / 2 engine.add_obj(Target(x, BOTROW, BOTROWDELTA)) engine.add_obj(Obscurer(TWWIDTH/-2 - TWWIDTH, BOTROW)) engine.add_obj(Obscurer(TWWIDTH/2, BOTROW)) # walls - need to be created after obscuring panels turtle.register_shape("LeftWall", ( (0,0), (HEIGHT,0), (TWWIDTH, TOPROW - SPACER), (BOTROW - SPACER, TOPROW - SPACER) )) turtle.register_shape("RightWall", ( (0,0), (HEIGHT,0), (TWWIDTH, -TOPROW + SPACER), (BOTROW - SPACER, -TOPROW + SPACER) )) engine.add_obj(LeftWall()) engine.add_obj(RightWall())
def mouse_cb(x, y): box = Box() engine.add_obj(box) print('Mouse click at', x, y)
def circle_cb(): obj = Circle(0, 0, 1, 1) engine.add_obj(obj) def quit_cb(key): if key == 'q' or key == 'Q': engine.exit_engine() # main routine if __name__ == '__main__': #random.seed(86753) engine.init_screen(WIDTH, HEIGHT) engine.init_engine() engine.set_keyboard_handler(quit_cb) engine.add_random_event(0.005, circle_cb) engine.register_collision(Circle, Circle, coll_circle) engine.register_collision(Boom, Circle, coll_boom1) engine.register_collision(Circle, Boom, coll_boom2) for i in range(4): ypos = engine.MAXY - i * HEIGHT/3 obj = DelayedBox(engine.MINX, ypos, i*250) engine.add_obj(obj) engine.add_obj(PulsingCircle(50, 50, 100)) engine.add_obj(BigRect(engine.MINX + 25, 0, engine.MAXX - 25, engine.MINY + 25)) engine.add_obj(BigRect(0, 100, 25, 25)) engine.add_obj(BigRect(-200, 150, -100, 125)) engine.engine()
turtle.rt(45) turtle.fd(B * 3 / 4) # windshield turtle.lt(45) turtle.fd(B) # hood turtle.rt(90) turtle.fd(B * 3 / 4) # front turtle.rt(90) turtle.fd(B * 1 / 7) turtle.lt(90) turtle.circle(-B / 2, 180) # front tire turtle.lt(90) turtle.fd(B) turtle.lt(90) turtle.circle(-B / 2, 180) # back tire turtle.lt(90) turtle.fd(B * 1 / 7) turtle.rt(90) turtle.fd(B * 5 / 6) # back turtle.end_poly() poly = turtle.get_poly() turtle.register_shape('car', poly) if __name__ == '__main__': engine.init_screen(WIDTH, HEIGHT) engine.init_engine() makeshape() car = Car() engine.add_obj(car) engine.engine()
def sidemissile_cb(): obj = missileside(0, 0, 0, +1) engine.add_obj(obj)
# secondary ground sec_ground = Ground() sec_ground.gndIdentifier = 2 sec_ground.ground = lvl sec_ground.x = wlength / 2 # for now, the ground is assumed to be the primary gnd = pri_ground sol = Sun() # these are level bars ess = GreenBarFuel() spd = SpeedBar() engine.add_obj(pri_ground) engine.add_obj(sec_ground) engine.add_obj(sol) engine.add_obj(ess) engine.add_obj(spd) # display the gifs on screen logo = FuelLogo() accl = AccelerationLogo() engine.add_obj(logo) engine.add_obj(accl) engine.add_obj(ship) # create a random number of enemies located at random places nb_enemies = random.randint(5, 10)
def coll_city1(city, obj): if iscoll_circle(city, obj): x, y, r = city.get_bc() engine.add_obj(Boom(x, y, r)) engine.del_obj(obj) city.hit()
def coll_boom1(obj1, obj2): # circle getting drawn into an explosion in progress - obj1 is Boom inst if iscoll_circle(obj1, obj2): x, y, r = obj2.get_bc() engine.add_obj(Boom(x, y, r)) engine.del_obj(obj2)
engine.del_obj(target) ntargets -= 1 if ntargets == 0: win(timer.gettime()) def draw_score(time): turtle.goto(0, HEIGHT//2 - 25) turtle.dot(50, ROOFCOLOR) turtle.color('black') turtle.write(time, align='center', font=('Arial', 14, 'normal')) def win(time): mesg = 'Time to complete: %d' % time turtle.goto(0, -30) turtle.color('black') turtle.write(mesg, True, align='center', font=('Arial', 24, 'italic')) if __name__ == '__main__': engine.init_screen(WIDTH, HEIGHT) engine.init_engine() engine.set_keyboard_handler(input_cb) makescenery() makeguns() me = Gun() engine.add_obj(me) timer = Timer() engine.add_obj(timer) engine.register_collision(Shot, Target, coll1) engine.register_collision(Target, Shot, coll2) engine.engine()
# moving box, shifting colors by overriding draw() method # XXX doesn't work if draw() is used instead; internal turtle module issue? import random import engine WIDTH = 640 HEIGHT = 480 class Box(engine.GameObject): def __init__(self): super().__init__(0, 0, +1, 0, 'square', 'red') def update(self): if random.random() < 0.1: self.color = random.choice([ 'red', 'blue', 'green', 'yellow', 'black', 'orange', 'purple' ]) super().update() if __name__ == '__main__': engine.init_screen(WIDTH, HEIGHT) engine.init_engine() box = Box() engine.add_obj(box) engine.engine()
def circle_cb(): obj = Circle(0, 0, 1, 1) engine.add_obj(obj)
# basic moving box import engine WIDTH = 640 HEIGHT = 480 class Box(engine.GameObject): def __init__(self): super().__init__(0, 0, +1, 0, 'square', 'red') if __name__ == '__main__': engine.init_screen(WIDTH, HEIGHT) engine.init_engine() box = Box() engine.add_obj(box) engine.engine()
Test and deal with for the landing/crashing of the ship. :param groundInst: Instance of the Ground class. :param landerInst: Instance of the Lander class. :return: None """ collision_cb_GL(groundInst, landerInst) if __name__ == '__main__': # Create the various elements of the game engine.init_screen(WIDTH, HEIGHT) engine.init_engine() makeshape() lander = Lander() engine.set_keyboard_handler(keyboard_cb) sun = Sun() ground = Ground(groundS1) engine.add_obj(sun) engine.add_obj(lander) engine.add_obj(ground) # Call collision_cb_SL() each step for each pair of {Sun, Lander} engine.register_collision(Sun, Lander, collision_cb_SL) # Call collision_cb_LS() each step for each pair of {Lander, Sun} engine.register_collision(Lander, Sun, collision_cb_LS) engine.register_collision(Ground, Lander, collision_cb_GL) engine.register_collision(Lander, Ground, collision_cb_LG) # Start the game engine.engine()