def test_scaledcirclecollision(self): s1 = Sprite(self.circ, (100, 80)) s2 = Sprite(self.rect, (161, 100)) c = s1.collidingWith(s2) self.assertFalse(c, msg="circle not colliding with rect") s1.x = 101 c = s1.collidingWith(s2) self.assertTrue(c, msg="circle colliding with rect") s1.x = 170 c = s1.collidingWith(s2) self.assertTrue(c, msg="circle colliding with rect on right side") s1.x = 172 c = s1.collidingWith(s2) self.assertFalse(c, msg="circle not colliding with rect on right side") # Now scale at 2x s1.scale = 2 s1.x = 40 c = s1.collidingWith(s2) self.assertFalse(c, msg="2x scaled circle not colliding with rect") s1.x = 41 c = s1.collidingWith(s2) self.assertTrue(c, msg="2x scaled circle colliding with rect") s1.x = 170 c = s1.collidingWith(s2) self.assertTrue( c, msg="2x scaled circle colliding with rect on right side") s1.x = 172 c = s1.collidingWith(s2) self.assertFalse( c, msg="2x scaled circle not colliding with rect on right side") s1.destroy() s2.destroy()
def test_spritecollision(self): s1 = Sprite(self.image, (51,52)) s2 = Sprite(self.image, (51, 52)) cl = s2.collidingWithSprites() self.assertEqual(len(cl), 1) self.assertEqual(s2.collidingWith(cl[0]), True) s2.x = 125 self.assertEqual(s2.collidingWith(cl[0]), False) s1.destroy() s2.destroy()
def test_spritecollision(self): s1 = Sprite(self.image, (51, 52)) s2 = Sprite(self.image, (51, 52)) cl = s2.collidingWithSprites() self.assertEqual(len(cl), 1) self.assertEqual(s2.collidingWith(cl[0]), True) s2.x = 125 self.assertEqual(s2.collidingWith(cl[0]), False) s1.destroy() s2.destroy()
def test_sprite(self): s = Sprite(self.image, (51,52)) self.assertEqual(s.x, 51) self.assertEqual(s.y, 52) self.assertEqual(s.width, 71) self.assertEqual(s.height, 100) self.assertEqual(s.position, (51,52)) self.assertEqual(s.fxcenter, 0.0) s.x = 41 self.assertEqual(s.x, 41) self.assertEqual(s.width, 71) s.destroy()
def test_sprite(self): s = Sprite(self.image, (51, 52)) self.assertEqual(s.x, 51) self.assertEqual(s.y, 52) self.assertEqual(s.width, 71) self.assertEqual(s.height, 100) self.assertEqual(s.position, (51, 52)) self.assertEqual(s.fxcenter, 0.0) s.x = 41 self.assertEqual(s.x, 41) self.assertEqual(s.width, 71) s.destroy()
def test_advancedspritecollision(self): class SpriteChild(Sprite): pass s1 = Sprite(self.image, (51, 52)) s2 = Sprite(self.image, (61, 52)) s3 = SpriteChild(self.image, (71, 52)) cl = s1.collidingWithSprites(SpriteChild) self.assertEqual(len(cl), 1) self.assertIs(cl[0], s3) cl = s1.collidingWithSprites() self.assertEqual(len(cl), 2) s1.destroy() s2.destroy() s3.destroy()
def test_advancedspritecollision(self): class SpriteChild(Sprite): pass s1 = Sprite(self.image, (51,52)) s2 = Sprite(self.image, (61,52)) s3 = SpriteChild(self.image, (71,52)) cl = s1.collidingWithSprites(SpriteChild) self.assertEqual(len(cl), 1) self.assertIs(cl[0], s3) cl = s1.collidingWithSprites() self.assertEqual(len(cl), 2) s1.destroy() s2.destroy() s3.destroy()
def test_enfoldingcollision(self): def step(): s1.x += 1 s2.x -= 1 c = s1.collidingWith(s2) self.assertTrue(c, msg="big sprite colliding with embedded sprite") c = s2.collidingWith(s1) self.assertTrue(c, msg="small sprite colliding with enfolding sprite") s1 = Sprite(self.rocket, (10, 10)) s2 = Sprite(self.image, (15, 15)) a = App() a.run(step) for i in range(10): a._animate(1) s1.destroy() s2.destroy()
def test_spritevariety(self): s1 = Sprite(self.multiimage) s2 = Sprite(self.rect) s3 = Sprite(self.circ) s4 = Sprite(self.ellipse) s5 = Sprite(self.line) s6 = Sprite(self.poly) s7 = Sprite(self.text) s1.destroy() s2.destroy() s3.destroy() s4.destroy() s5.destroy() s6.destroy() s7.destroy()
class Hangman(App): def __init__(self, width, height): super().__init__(width, height) #creating sprites bgasset = RectangleAsset(SCREEN_WIDTH, 700, noline, blue) Sprite(bgasset,(0,0)) self.gallows = Sprite(gallowsasset,(350,50)) self.gallows.scale = 1.5 self.letterbutton = Sprite(guessletterasset,(800,250)) self.wordbutton = Sprite(wordbuttonasset,(800,300)) self.gallows.hangingphase = 0 self.wordsprite = Sprite(wordasset,(500,525)) self.wordsprite.fxcenter = 0.5 self.guessedsprite = Sprite(guessedasset, (100,250)) #tracking mouse def mousemove(self, event): global mousex global mousey mousex = event.x-10 mousey = event.y-10 #sensing clicks def mousedown(self, event): global mousex global mousey if (mousex >= 800 and mousex <= 840) and (mousey >= 250 and mousey <= 275): self.guessletter() if (mousex >= 800 and mousex <= 840) and (mousey >= 300 and mousey <= 325): self.guessword() def guessletter(self): global wordinprogress global word global displayedword global wordsprite global alreadyguessedstring global guessedasset global wordasset global hangingphase global gallows global guessedsprite global allletters displayedword = '' guessedletter = input('Please guess a letter!') guessedletter = guessedletter.lower() while alreadyguessed.count(guessedletter) > 0: guessedletter = input('You already guessed that letter! Try again:') while allsymbols.count(guessedletter) > 0: guessedletter = input("That's not a letter! Try again:") while len(guessedletter) > 1: guessedletter = input("Please only input one letter:") if word.count(guessedletter) > 0: for x in range(len(word)): if guessedletter == word[x]: wordinprogress = wordinprogress[:x] + "{0}".format(guessedletter) + wordinprogress[x+1:] for x in range(len(wordinprogress)): displayedword = displayedword + "{0:<3}".format(wordinprogress[x]) self.wordsprite.destroy() wordasset = TextAsset(displayedword, style='60px Helvetica',align='center',width=1000) self.wordsprite = Sprite(wordasset,(500,525)) self.wordsprite.fxcenter = 0.5 alreadyguessed.append(guessedletter) alreadyguessedstring = alreadyguessedstring + "{0:<3}".format(guessedletter) else: alreadyguessed.append(guessedletter) alreadyguessedstring = alreadyguessedstring + "{0:<3}".format(guessedletter) self.gallows.hangingphase += 1 self.gallows.setImage(self.gallows.hangingphase) self.guessedsprite.destroy() guessedasset = TextAsset(alreadyguessedstring, style='20px Helvetica',align='center',width=100) self.guessedsprite = Sprite(guessedasset, (100,250)) self.endgame() self.wongame() def guessword(self): global word global hangingphase global gallows global finishedword global guessed guessedword = input("Guess the word!") guessedword = guessedword.lower() if guessedword == word: self.wordsprite.destroy() wordasset = TextAsset(finishedword, style='60px Helvetica',align='center',width=1000) self.wordsprite = Sprite(wordasset,(500,525)) self.wordsprite.fxcenter = 0.5 endscreen = Sprite(winscreenasset, (150,250)) guessed = True self.wongame() else: self.gallows.hangingphase += 1 self.gallows.setImage(self.gallows.hangingphase) self.endgame() def endgame(self): if self.gallows.hangingphase == 6: self.wordsprite.destroy() wordasset = TextAsset(finishedword, style='60px Helvetica',align='center',width=1000,fill=red) self.wordsprite = Sprite(wordasset,(500,525)) self.wordsprite.fxcenter = 0.5 Hangman.unlistenMouseEvent('mousemove', myapp.mousemove) Hangman.unlistenMouseEvent('mousedown', myapp.mousedown) def wongame(self): global displayedword, finishedword, guessed if displayedword == finishedword: endscreen = Sprite(winscreenasset, (150,250)) Hangman.unlistenMouseEvent('mousemove', myapp.mousemove) Hangman.unlistenMouseEvent('mousedown', myapp.mousedown) if guessed == True: Hangman.unlistenMouseEvent('mousemove', myapp.mousemove) Hangman.unlistenMouseEvent('mousedown', myapp.mousedown)
class Maze(): def __init__(self): self.mazeArray = [] self.ghostArray = [] self.cellStack = [] self.visitedCells = 1 self.score = 0 self.mazeDict = {} self.currentCell = 0 self.bubbleArray = random.sample(range(1, cTotalCells - 1), 1) self.trophyArray = [] aCellWallH = LineAsset(cCellSize, 0, thinline) aCellWallV = LineAsset(0, cCellSize, thinline) bg_asset = RectangleAsset(cWidth * cCellSize, cHeight * cCellSize, thinline, white) self.bg = Sprite(bg_asset, (0, 0)) self.footer = Sprite( TextAsset(text='Maze Runner. Level - ' + str(cLeveli), width=250, align='center', style='20px Arial', fill=blue), ((cWidth * cCellSize - 200) / 2, cHeight * cCellSize + 20)) self.gOver = TextAsset(text='Game Over!', width=140, align='center', style='20px Arial', fill=red) #----------borrowed, heavily reworked---------------------- for y in range(cHeight): for x in range(cWidth): self.mazeDict['H:' + str(x) + ':' + str(y)] = Sprite( aCellWallH, (x * cCellSize, y * cCellSize)) for x in range(cWidth): self.mazeArray.append(0) if (y == 0): for yi in range(cHeight): self.mazeDict['V:' + str(x) + ':' + str(yi)] = Sprite( aCellWallV, (x * cCellSize, yi * cCellSize)) while (self.visitedCells < cTotalCells): x = int(self.currentCell % cWidth) y = int(self.currentCell / cWidth) neighbors = [] for i in range(4): nx = x + compass[i][0] ny = y + compass[i][1] if ((nx >= 0) and (ny >= 0) and (nx < cWidth) and (ny < cHeight)): if (self.mazeArray[(ny * cWidth + nx)] & 0x000F) == 0: nidx = ny * cWidth + nx neighbors.append((nidx, 1 << i)) if len(neighbors) > 0: idx = random.randint(0, len(neighbors) - 1) nidx, direction = neighbors[idx] dx = x * cCellSize dy = y * cCellSize if direction & 1: self.mazeArray[nidx] |= (4) self.mazeDict['V:' + str(x) + ':' + str(y)].destroy() del self.mazeDict['V:' + str(x) + ':' + str(y)] elif direction & 2: self.mazeArray[nidx] |= (8) self.mazeDict['H:' + str(x) + ':' + str(y + 1)].destroy() del self.mazeDict['H:' + str(x) + ':' + str(y + 1)] elif direction & 4: self.mazeArray[nidx] |= (1) self.mazeDict['V:' + str(x + 1) + ':' + str(y)].destroy() del self.mazeDict['V:' + str(x + 1) + ':' + str(y)] elif direction & 8: self.mazeArray[nidx] |= (2) self.mazeDict['H:' + str(x) + ':' + str(y)].destroy() del self.mazeDict['H:' + str(x) + ':' + str(y)] self.mazeArray[self.currentCell] |= direction self.cellStack.append(self.currentCell) self.currentCell = nidx self.visitedCells = self.visitedCells + 1 else: self.currentCell = self.cellStack.pop() #-------------------------------- for i in range(cLeveli): self.ghostArray.append( Ghost(self.getMazeArray(), self.getCellStack())) for tCell in self.bubbleArray: self.trophyArray.append(Trophy(tCell)) def runGhosts(self): i = 0 for g in self.ghostArray: g.update() if g.myLocation() == cTotalCells - 1: g.destroy() self.ghostArray.append( Ghost(self.getMazeArray(), self.getCellStack())) del self.ghostArray[i] i += 1 def getMazeArray(self): return self.mazeArray[:] def getCellStack(self): return self.cellStack[:] def addRunner(self, pRunner): self.Runner = pRunner def checkCollisions(self): if len(self.Runner.collidingWithSprites(Ghost)) > 0: self.Runner.setState('Lost') Sprite(self.gOver, (cWidth * cCellSize / 2 - 40, cHeight * cCellSize / 2 - 20)) trophies = self.Runner.collidingWithSprites(Trophy) if len(trophies) > 0: for t in trophies: self.score += 1 t.destroy() self.bubbleArray.pop() if len(self.bubbleArray) == 0: self.Runner.setState('Won') def selfDestruct(self): self.bg.destroy() self.footer.destroy() for k in self.mazeDict: self.mazeDict[k].destroy() for g in self.ghostArray: g.destroy()
class Zoxy(App): def __init__(self, width, height): super().__init__(width, height) time.time() black = Color(1, 1) line = LineStyle(2, black) self.peanuts = 0 Score = str(self.peanuts) grass = Color(0x229954, 1) hedge = Color(0x145A32, 1) stone = Color(0xB2BABB, 1) road = Color(0x515A5A, 1) roof = Color(0x5F6A6A, 1) Grass = RectangleAsset(1279, 939, line, grass) Hedge = RectangleAsset(25, 700, line, hedge) Hedge2 = RectangleAsset(25, 340, line, hedge) Hedge3 = RectangleAsset(25, 290, line, hedge) Hedge4 = RectangleAsset(485, 25, line, hedge) Stone = RectangleAsset(20, 50, line, stone) Road = RectangleAsset(60, 940, line, road) Roof = RectangleAsset(30, 400, line, roof) Roof2 = RectangleAsset(30, 300, line, roof) self.score = TextAsset("Score:" + Score + "", style="40pt Comic Sans MS", fill=Color(0xD2B4DE, 1), width=200) self.waituntil = time.time() + 1 X = random.randrange(100) + 1280 X2 = random.randrange(100) + 1380 Y = random.randrange(940) Sprite(Grass, (0, 0)) Sprite(Roof2, (200, 0)) Sprite(Roof2, (200, 600)) Sprite(RectangleAsset(-400, 30, line, roof), (0, 0)) Sprite(CircleAsset(45, line, roof), (215, 40)) Sprite(CircleAsset(25, line, roof), (215, 40)) Sprite(CircleAsset(45, line, roof), (215, 900)) Sprite(CircleAsset(25, line, roof), (215, 900)) People((X2, 300)) People((X, 320)) People((X2, 340)) People((X, 360)) People((X2, 380)) People((X, 400)) People((X2, 420)) People((X, 440)) People((X2, 460)) People((X, 480)) People((X2, 500)) People((X, 520)) People((X2, 540)) People((X, 560)) People((X2, 580)) self.prompt = Sprite(self.score, (10, 10)) Bingo((640, 300), self) def step(self): thetime = time.time() for ship in self.getSpritesbyClass(Bingo): ship.step() for hip in self.getSpritesbyClass(People): hip.step() if hip.x <= 230: hip.x = 1280 self.peanuts = self.peanuts - 10 if thetime > self.waituntil: self.waituntil = thetime self.prompt.destroy() self.peanuts = self.peanuts + .01 peanuts = self.peanuts Score = str(peanuts) self.score = TextAsset("Score:" + Score + "", style="40pt Comic Sans MS", fill=Color(0xD2B4DE, 1), width=200) self.prompt = Sprite(self.score, (10, 10))
class Kong(App): def __init__(self, width, height): super().__init__(width, height) black = Color(0, 1) yellow = Color(0xffeb3b, 1.0) white = Color(0xfafafa, 1.0) liner = LineStyle(1, white) line = LineStyle(1, black) self.play = False self.ComicSans = True bg_asset = RectangleAsset(width, height, line, black) bg = Sprite(bg_asset, (0, 0)) text = TextAsset("Press ENTER To Start", style='40pt Comic Sans MS', fill=Color(0xffeb3b, 1), width=700) self.prompt = Sprite(text, (50, 250)) TEXT = TextAsset("Game Over", style='40pt Comic Sans Ms', fill=Color(0xffeb3b, 1), width=700) self.Prompt = Sprite(TEXT, (350, 350)) self.prompt.visible = True self.Prompt.visible = False self.count = 0 self.lost = False Kong.listenKeyEvent("keydown", "enter", self.playing) def playing(self, event): self.prompt.destroy() self.play = True if self.play == True: player((50, 640)) Barrel((70, 145)) black = Color(1, 0) Black = Color(0, 1) Red = Color(0xF44366, 1.0) noline = LineStyle(1000, Black) oline = LineStyle(0, Red) white = Color(0xfafafa, 1.0) yellow = Color(0xffca28, 1.0) liner = LineStyle(1, white) brown = Color(0x996633, 1.0) Blue = Color(0x558b24, 1.0) wall(RectangleAsset(700, 30, oline, Red), (0, 670)) wall(RectangleAsset(550, 30, oline, Red), (0, 500)) wall(RectangleAsset(550, 30, oline, Red), (150, 330)) wall(RectangleAsset(600, 30, oline, Red), (0, 160)) wall(RectangleAsset(100, 30, oline, Red), (100, 50)) ladder(RectangleAsset(10, 170, oline, Blue), (550, 500)) ladder(RectangleAsset(10, 170, oline, Blue), (140, 330)) ladder(RectangleAsset(10, 170, oline, Blue), (600, 160)) trophy(RectangleAsset(25, 25, liner, yellow), (100, 22)) def step(self): for ship in self.getSpritesbyClass(player): ship.step() if ship.x >= 680 or ship.x <= 0: ship.vx = 0 if ship.lives == 0 or ship.won == True: self.Prompt.visible = True if ship.won == True: self.Prompt.visible = True for hip in self.getSpritesbyClass(Barrel): hip.step() if hip.x <= 0: hip.destroy() self.count = self.count + 1 if self.count == 250: Barrel((70, 145)) self.count = 0
class Turtle: #defining colors black = Color(0x000000, 1.0) red = Color(0xF01414, 1.0) white = Color(0xFFFFFF, 1.0) blue = Color(0x1464F0, 1.0) green = Color(0x54C175, 1.0) yellow = Color(0xFAFF44, 1.0) orange = Color(0xFFA500, 1.0) cyan = Color(0x00FFFF, 1.0) thinlinewhite = LineStyle(1, white) thinlineblack = LineStyle(1, black) thinlinered = LineStyle(1, red) thinlineblue = LineStyle(1, blue) thinlinegreen = LineStyle(1, green) thinlineyellow = LineStyle(1, yellow) thinlineorange = LineStyle(1, orange) thinlinecyan = LineStyle(1, cyan) def __init__(self): Screen.registerturtle(self) self.currentcolor = self.black self.currentthinline = self.thinlineblack width = Screen.width height = Screen.height screencenter = (width / 2, height / 2 ) #finds a tuple for the center of the screen #self.newturtle(self.currentcolor, self.currentthinline) startturtle = PolygonAsset([(5, 5), (20, 13), (5, 21), (10, 13), (5, 5)], self.currentthinline, self.currentcolor) self.turtle = Sprite(startturtle, screencenter) self.rotationgoal = None self.forwardgoal = None self.bkgoal = None self.vr = 0 self.turtle.fxcenter = 1 self.turtle.fycenter = 1 / 2 self.vx = 0 self.vy = 0 self.commandlist = [] self.currentcmd = None self.distance = 0 self.combinedhead = 0 self.fdx = width / 2 self.fdy = height / 2 def newturtle(self, color, line): if self.turtle: x = self.turtle.x y = self.turtle.y r = self.turtle.rotation self.turtle.destroy() else: x = self.turtle.x y = self.turtle.y r = self.turtle.rotation startturtle = PolygonAsset([(5, 5), (20, 13), (5, 21), (10, 13), (5, 5)], self.currentthinline, self.currentcolor) self.turtle = Sprite(startturtle, screencenter) self.turtle.x = x self.turtle.fxcenter = 1 self.turtle.fycenter = 1 / 2 def step(self): if self.currentcmd: cmd, val = self.currentcmd if cmd == "right": self.vr = -0.06 if self.rotationgoal == None: self.currentcmd = None if cmd == "left": self.vr = 0.06 if self.rotationgoal == None: self.currentcmd = None if cmd == "forward": self.vx = -1 * cos(self.turtle.rotation) self.vy = sin(self.turtle.rotation) if self.forwardgoal == None: self.currentcmd = None if cmd == "backward": self.vx = -cos(self.turtle.rotation) self.vy = sin(self.turtle.rotation) if self.bkgoal == None: self.currentcmd = None elif self.commandlist: self.currentcmd = self.commandlist.pop(0) cmd, val = self.currentcmd if cmd == "right": self.rotationgoal = self.turtle.rotation - val * pi / 180 if cmd == "left": self.rotationgoal = self.turtle.rotation + val * pi / 180 if cmd == "forward": self.forwardgoal = val if cmd == "backward": self.bkgoal = val if cmd == "color": if val == "red": self.currentcolor = self.red self.currentthinline = self.thinlinered if val == "black": self.currentcolor = self.black self.currentthinline = self.thinlineblack if val == "blue": self.currentcolor = self.blue self.currentthinline = self.thinlineblue if val == "green": self.currentcolor = self.green self.currentthinline = self.thinlinegreen if val == "yellow": self.currentcolor = self.yellow self.currentthinline = self.thinlineyellow if val == "orange": self.currentcolor = self.orange self.currentthinline = self.thinlineorange if val == "cyan": self.currentcolor = self.cyan self.currentthinline = self.thinlinecyan self.currentcmd = None if not self.rotationgoal is None: #TURNS if self.rotationgoal - self.turtle.rotation < 0: #right turn if self.turtle.rotation + self.vr <= self.rotationgoal: self.vr = 0 self.turtle.rotation = self.rotationgoal self.rotationgoal = None self.currentcmd = None else: self.turtle.rotation += self.vr elif self.rotationgoal - self.turtle.rotation > 0: #left turn if self.turtle.rotation + self.vr >= self.rotationgoal: self.vr = 0 self.turtle.rotation = self.rotationgoal self.rotationgoal = None self.currentcmd = None else: self.turtle.rotation += self.vr if self.turtle.rotation == self.rotationgoal: self.vr = 0 self.turtle.rotation = self.rotationgoal self.rotationgoal = None self.currentcmd = None if not self.forwardgoal is None: #forward if self.forwardgoal - self.distance > 0: if self.distance + (self.vx**2 + self.vy**2)**1 / 2 >= self.forwardgoal: self.vx = 0 self.vy = 0 self.turtle.x = (self.forwardgoal - self.distance) * cos( self.turtle.rotation) + self.turtle.x self.turtle.y = (self.forwardgoal - self.distance) * sin( self.turtle.rotation) + self.turtle.y self.distance = 0 self.fdx = self.turtle.x self.fdy = self.turtle.y self.forwardgoal = None self.currentcmd = None else: self.turtle.x -= self.vx self.turtle.y -= self.vy line = LineSegment( (self.turtle.x, self.turtle.y), (self.turtle.x - self.vx, self.turtle.y - self.vy), style=self.currentthinline, positioning="physical") self.distance = ((self.turtle.x - self.fdx)**2 + (self.turtle.y - self.fdy)**2)**(1 / 2) else: self.vx = 0 self.vy = 0 self.forwardgoal = None self.currentcmd = None self.fdx = self.turtle.x self.fdy = self.turtle.y self.distance = 0 if not self.bkgoal is None: #backward if self.bkgoal - self.distance > 0: if self.distance + (self.vx**2 + self.vy**2)**1 / 2 >= self.bkgoal: self.vx = 0 self.vy = 0 self.turtle.x = (self.bkgoal - self.distance) * cos( self.turtle.rotation) + self.turtle.x self.turtle.y = (self.bkgoal - self.distance) * sin( self.turtle.rotation) + self.turtle.y self.distance = 0 self.fdx = self.turtle.x self.fdy = self.turtle.y self.bkgoal = None self.currentcmd = None else: self.turtle.x += self.vx self.turtle.y += self.vy line = LineSegment( (self.turtle.x, self.turtle.y), (self.turtle.x - self.vx, self.turtle.y - self.vy), positioning="physical") self.distance = ((self.turtle.x - self.fdx)**2 + (self.turtle.y - self.fdy)**2)**(1 / 2) else: self.vx = 0 self.vy = 0 self.bkgoal = None self.currentcmd = None self.fdx = self.turtle.x self.fdy = self.turtle.y self.distance = 0 def right(self, x): ''' Turn turtle right by angle units. Aliases: right | rt Argument: angle -- a number (integer or float) Turn turtle right by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on mode. (See this.) Example (for a Turtle instance named turtle): >>> turtle.heading() 22.0 >>> turtle.right(45) >>> turtle.heading() 337.0''' self.commandlist.append(("right", x)) self.combinedhead -= x if self.combinedhead < 0: self.combinedhead = -(-self.combinedhead % 360) + 360 if self.combinedhead > 0: self.combinedhead = self.combinedhead % 360 def rt(self, x): ''' Turn turtle right by angle units. Aliases: right | rt Argument: angle -- a number (integer or float) Turn turtle right by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on mode. (See this.) Example (for a Turtle instance named turtle): >>> turtle.heading() 22.0 >>> turtle.right(45) >>> turtle.heading() 337.0''' return self.right(x) def left(self, x): '''Turn turtle left by angle units. Aliases: left | lt Argument: angle -- a number (integer or float) Turn turtle left by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on mode. (See this.) Example (for a Turtle instance named turtle): >>> turtle.heading() 22.0 >>> turtle.left(45) >>> turtle.heading() 67.0''' self.commandlist.append(("left", x)) self.combinedhead += x if self.combinedhead < 0: self.combinedhead = -(-self.combinedhead % 360) + 360 if self.combinedhead > 0: self.combinedhead = self.combinedhead % 360 def lt(self, x): '''Turn turtle left by angle units. Aliases: left | lt Argument: angle -- a number (integer or float) Turn turtle left by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) Angle orientation depends on mode. (See this.) Example (for a Turtle instance named turtle): >>> turtle.heading() 22.0 >>> turtle.left(45) >>> turtle.heading() 67.0''' return self.left(x) def heading(self): return self.combinedhead def forward(self, x): '''Move the turtle forward by the specified distance. Aliases: forward | fd Argument: distance -- a number (integer or float) Move the turtle forward by the specified distance, in the direction the turtle is headed. Example: >>> position() (0.00, 0.00) >>> forward(25) >>> position() (25.00,0.00) >>> forward(-75) >>> position() (-50.00,0.00)''' self.commandlist.append(("forward", x)) def fd(self, x): '''Move the turtle forward by the specified distance. Aliases: forward | fd Argument: distance -- a number (integer or float) Move the turtle forward by the specified distance, in the direction the turtle is headed. Example: >>> position() (0.00, 0.00) >>> forward(25) >>> position() (25.00,0.00) >>> forward(-75) >>> position() (-50.00,0.00)''' return self.forward(x) def backward(self, x): '''Move the turtle backward by distance. Aliases: back | backward | bk Argument: distance -- a number Move the turtle backward by distance ,opposite to the direction the turtle is headed. Do not change the turtle's heading. Example: >>> position() (0.00, 0.00) >>> backward(30) >>> position() (-30.00, 0.00)''' self.commandlist.append(("backward", x)) def bk(self, x): '''Move the turtle backward by distance. Aliases: back | backward | bk Argument: distance -- a number Move the turtle backward by distance ,opposite to the direction the turtle is headed. Do not change the turtle's heading. Example: >>> position() (0.00, 0.00) >>> backward(30) >>> position() (-30.00, 0.00)''' return self.backward(x) def back(self, x): '''Move the turtle backward by distance. Aliases: back | backward | bk Argument: distance -- a number Move the turtle backward by distance ,opposite to the direction the turtle is headed. Do not change the turtle's heading. Example: >>> position() (0.00, 0.00) >>> backward(30) >>> position() (-30.00, 0.00)''' return self.backward(x) def color(self, x): self.commandlist.append(("color", x))
def destroy(self): MathApp._removeVisual(self) MathApp._removeMovable(self) MathApp._removeStrokable(self) _MathDynamic.destroy(self) Sprite.destroy(self)
class MC(Sprite): def __init__(self, position, ls): super().__init__(cf, position) self.moves = speed1 SpaceGame.listenKeyEvent("keydown", "d", self.dKey) SpaceGame.listenKeyEvent("keydown", "a", self.aKey) SpaceGame.listenKeyEvent("keydown", "s", self.sKey) SpaceGame.listenKeyEvent("keydown", "w", self.wKey) SpaceGame.listenKeyEvent("keydown", "q", self.qKey) SpaceGame.listenKeyEvent("keydown", "e", self.eKey) SpaceGame.listenKeyEvent("keydown", "j", self.jKey) SpaceGame.listenKeyEvent("keydown", "k", self.kKey) SpaceGame.listenKeyEvent("keydown", "l", self.lKey) SpaceGame.listenKeyEvent("keydown", "r", self.rKey) SpaceGame.listenKeyEvent("keydown", "z", self.zKey) self.fxcenter = self.fycenter = 0.5 self.lives = ls self.start = 0 self.end = 0 self.dead = 0 self.go = 0 self.Sprites = [] self.shielded = -1 self.cooldownS = 0 self.ammo = bulletCount def zKey(self, event): self.KILL() if self.moves > 1: movelist[self.moves - 1].destroy() self.moves -= 1 movelist[self.moves - 1].destroy() self.moves -= 1 self.x -= 50 * cos((pi / 2) - self.rotation) self.y -= 50 * sin((pi / 2) - self.rotation) def lKey(self, event): self.KILL() if self.moves > 1 and self.cooldownS < 1: shield((self.x, self.y)) self.shielded = 2 movelist[self.moves - 1].destroy() self.moves -= 1 movelist[self.moves - 1].destroy() self.moves -= 1 self.cooldownS = 2 def rKey(self, event): if self.moves > 3: movelist[self.moves - 1].destroy() self.moves -= 1 movelist[self.moves - 1].destroy() self.moves -= 1 movelist[self.moves - 1].destroy() self.moves -= 1 RELOAD() self.ammo = 4 def jKey(self, event): self.KILL() if self.moves > 0: self.Sprites.append( axe((self.x - 60 * cos((pi / 2) - self.rotation), self.y - 60 * sin((pi / 2) - self.rotation)), self.rotation)) movelist[self.moves - 1].destroy() self.moves -= 1 def kKey(self, event): self.KILL() if self.moves > 2 and self.ammo > 0: if len(myapp.getSpritesbyClass(plasmaBolt)) > 1: for x in myapp.getSpritesbyClass(plasmaBolt): x.destroy() plasmaBolt((self.x - 19 * cos((pi / 2) - self.rotation), self.y - 19 * sin((pi / 2) - self.rotation)), self.rotation) movelist[self.moves - 1].destroy() self.moves -= 1 movelist[self.moves - 1].destroy() self.moves -= 1 movelist[self.moves - 1].destroy() self.moves -= 1 ammolist[self.ammo - 1].destroy() self.ammo -= 1 def dKey(self, event): self.KILL() if self.moves > 0 and self.x + speed * cos( self.rotation) < SCREEN_WIDTH1 and self.y - speed * sin( self.rotation) > 0: self.x += speed * cos(self.rotation) self.y -= speed * sin(self.rotation) movelist[self.moves - 1].destroy() self.moves -= 1 def aKey(self, event): self.KILL() if self.moves > 0 and self.x - speed * cos( self.rotation) > 0 and self.y + speed * sin( self.rotation) < SCREEN_HEIGHT: self.x -= speed * cos(self.rotation) self.y += speed * sin(self.rotation) movelist[self.moves - 1].destroy() self.moves -= 1 def sKey(self, event): self.KILL() if self.moves > 0 and self.x + speed * cos( (pi / 2) - self.rotation) < SCREEN_WIDTH1 and self.y + speed * sin( (pi / 2) - self.rotation) < SCREEN_HEIGHT: self.x += speed * cos((pi / 2) - self.rotation) self.y += speed * sin((pi / 2) - self.rotation) movelist[self.moves - 1].destroy() self.moves -= 1 def wKey(self, event): self.KILL() if self.moves > 0 and self.x - speed * cos( (pi / 2) - self.rotation) > 0 and self.y - speed * sin( (pi / 2) - self.rotation) > 0: self.x -= speed * cos((pi / 2) - self.rotation) self.y -= speed * sin((pi / 2) - self.rotation) movelist[self.moves - 1].destroy() self.moves -= 1 def qKey(self, event): self.KILL() if self.rotation == 31 * (pi / 32): self.rotation = 0 else: self.rotation += pi / 32 def eKey(self, event): self.KILL() if self.rotation == -31 * (pi / 32): self.rotation = 0 else: self.rotation -= pi / 32 def KILL(self): if len(self.Sprites) > 0: for x in self.Sprites: x.destroy() self.Sprites = [] def step(self): self.KILL() self.moves = speed1 if self.cooldownS > 0: self.cooldownS -= 1 def hit(self): global t print('I got hit', self.shielded, t) if self.shielded > 0: self.shielded -= 1 else: print("here") self.cooldownS = 0 self.start = time.time() global heartlist self.lives -= 1 print(heartlist[self.lives]) heartlist[self.lives].destroy() heartlist.remove(heartlist[self.lives]) if t != 0: print('here 2') self.dead = Sprite(dead_asset, (1, 1)) print(self.dead) t = 0 self.end = self.start + .5 def hit2(self): elapsed = time.time() if elapsed > self.end: print(self.dead) self.dead.destroy() global t t = 1 if self.lives == 0: gameoverT = TextAsset("Final Score: {0}".format(score)) Sprite(gameoverT, (myapp.width - 150, 40)) if SCREEN_WIDTH1 / 1071 < SCREEN_HEIGHT / 571: self.go = Sprite(gameover, (1, 1)) self.go.scale = SCREEN_WIDTH1 / 1071 else: self.go = Sprite(gameover, (1, 1)) self.go.scale = SCREEN_HEIGHT / 571
class SpaceGame(App): def __init__(self): super().__init__() # Background print(self.width) print(self.height) black = Color(0, 1) noline = LineStyle(0, black) TA= TextAsset("Press Enter to Begin", style="bold 40pt Arial", width=250, fill=black) self.Enter=Sprite(TA,(400,200)) self.levelindex=-.5 self.listenMouseEvent("mousemove", self.Mouse) self.listenMouseEvent("click", self.Click) self.listenKeyEvent("keydown", "enter", self.newlevel) self.listenKeyEvent("keydown", "z", self.uplevel) self.listenKeyEvent("keydown", "x", self.downlevel) self.listenKeyEvent("keydown","1",self.player2) self.levelfinish=[] self.terrainlist=None self.p = None self.c=None self.q=None self.progress=False self.player2=False def Click(self, event): if self.c: self.c.destroy() print("hah you're attempt is futile.") self.c=Collide(self.pos,5,5,red) def Mouse(self, event): self.pos = (event.x, event.y) def uplevel(self, event): self.levelindex+=0.5 def downlevel(self, event): self.levelindex-=0.5 def player2(self,event): if self.player2==True: self.player2=False print("Removing Player 2.\n Press Enter to restart the level without the other Player.") else: self.player2=True print("Adding another player.\nPress Enter to restart the level with the other Player.\n Player 2 is controlled with WASD and 'c'.") def newlevel(self,event): for s in self.getSpritesbyClass(Player): s.destroy() for s in self.getSpritesbyClass(Spike): s.destroy() for a in self.getSpritesbyClass(Variblock): a.destroy() for c in self.getSpritesbyClass(Collide): c.destroy() for s in self.getSpritesbyClass(sprong): s.destroy() for s in self.getSpritesbyClass(textbox): s.destroy() for s in self.getSpritesbyClass(goal): s.destroy() for s in self.getSpritesbyClass(Snake): s.destroy() for s in self.getSpritesbyClass(Snakebox): s.destroy() for s in self.getSpritesbyClass(Platform): s.destroy() for s in self.getSpritesbyClass(goal2): s.destroy() if self.Enter: self.Enter.destroy() self.Enter=None if self.levelindex==-1: self.progress=True Variblock(103,52,0,0) textbox("<Credits>","bold 40pt Arial",600,320,220) if self.levelindex==-2: Variblock(103,20,0,0) Variblock(103,10,0,400) self.p=Player((100,360),0) Variblock(5,15,0,200) Platform(50,350,False,0) self.c=[goal2(20,20,70,300,1),goal2(20,20,220,300,2),goal2(20,20,370,300,3),goal2(20,20,520,300,4),goal2(20,20,670,300,5),goal2(20,20,820,300,5)] Variblock(5,15,150,200) Platform(200,350,False,0) Variblock(5,15,300,200) Platform(350,350,False,0) Variblock(5,15,450,200) Platform(500,350,False,0) Variblock(5,15,600,200) Platform(650,350,False,0) Variblock(5,15,750,200) textbox("Level Select","bold 40pt Arial",600,350,70) textbox("Level 1","bold 20pt Arial",600,60,160) textbox("Level 2","bold 20pt Arial",600,210,160) textbox("Level 3","bold 20pt Arial",600,360,160) textbox("Level 4","bold 20pt Arial",600,510,160) textbox("Level 5","bold 20pt Arial",600,650,160) if self.levelindex==-.5: self.progress=True self.p=Player((500,100),0) if self.player2==True: self.q=Player((500,100),1) Variblock(103,8,0,0) Variblock(103,8,0,435) Variblock(8,35,0,80) Variblock(8,35,940,80) Variblock(50,20,260,160) textbox("Castlevania too: Electric Boogaloo","bold 40pt Arial",600,300,200) textbox("Start","bold 30pt Arial",200,100,450) textbox("Credits","bold 30pt Arial",200,800,450) textbox("Level Select", "bold 30pt Arial",400,400,450) goal(100,20,100,420) self.c=[goal2(100,20,450,350,-2)] Spike(10,2,820,420) if self.levelindex==0: self.progress=True self.p=Player((60,350),0) Variblock(105,30,0,0) Variblock(105,12,0,420) Spike(1,12,0,300) goal(20,120,1000,300) textbox("Press 'Enter' when touching the blue goal to complete the level. The red 'Spikes' will send you back.","bold 40pt Arial",1000,10,10) if self.levelindex==0.5: self.progress=True self.p = Player((60,50),0) Variblock(3,105,0,0) Variblock(105,4,0,480) Variblock(3,90,990,0) textbox("Press 'Spacebar' to jump and 'm' to attack","bold 40pt Arial",1000,100,10) Variblock(4,8,200,420) Variblock(4,8,620,420) Snakebox(530,480) Spike(1,3,230,450) goal(20,20,800,430) if self.player2==True: self.q=Player((60,50),1) if self.levelindex==1: self.progress=True self.p = Player((60,50),0) Variblock(5,105,0,0) Variblock(105,4,0,480) Variblock(39,90,650,0) ###NonborderTerrain Variblock(21,13,50,150) Variblock(9,3,400,150) Variblock(3,12,400,150) Spike(14,1,260,250) Spike(1,9,430,180) textbox("You can slide on walls by holding a direction into the wall.","bold 20pt Arial",1000,10,10) textbox("And you can jump off the wall by pressing the spacebar","bold 20pt Arial",380,650,250) Spike(21,1,440,400) goal(20,20,500,470) if self.player2==True: self.q=Player((60,50),1) if self.levelindex==1.5 or self.levelindex==2.5 or self.levelindex==3.5: self.progress=True self.p=Player((60,350),0) Variblock(105,30,0,0) Variblock(105,12,0,420) Spike(1,12,0,300) goal(20,120,1000,300) if self.levelindex==4.5: self.progress=True self.p=Player((60,350),0) Variblock(105,30,0,0) Variblock(105,12,0,420) Spike(1,12,0,300) goal(20,120,1000,300) textbox("Here's a tough section. Good Luck!","bold 40pt Arial",1000,500,10) if self.levelindex==2: self.progress=True self.p = Player((60,50),0) Variblock(3,105,0,0) Variblock(105,4,0,480) Variblock(3,90,990,0) Variblock(10,30,400,250) Spike(10,1,400,250) sprong(340,470) textbox("This is a spring. It will send you high into the air.","bold 30pt Arial",1000,50,100) goal(20,20,510,450) if self.player2==True: self.q=Player((60,50),1) if self.levelindex==3: self.progress=True self.p = Player((100,400),0) Variblock(3,105,0,0) Variblock(105,4,0,480) Variblock(3,90,990,0) Variblock(4,10,200,400) Variblock(4,14,620,370) Platform(570,420,False,0) Snakebox(530,480) Spike(1,3,230,450) goal(20,20,800,440) if self.player2==True: self.q=Player((100,400),1) if self.levelindex==4: Variblock(3,105,0,0) Variblock(105,4,0,500) Variblock(3,90,990,0) ###Nonborder self.p=Player((100,450),0) #Level1 Variblock(50,2,0,400) Snakebox(470,500) Spike(1,8,30,420) Variblock(10,2,800,400) Spike(2,2,840,370) Variblock(10,2,600,400) Spike(2,2,640,370) Platform(300,300,True,1) Platform(200,300,True,-1) Snakebox(470,400) Spike(1,18,30,220) Variblock(2,10,50,260) #Level2 Variblock(50,2,500,300) Platform(500,200,True,2.5) #Level3 Variblock(50,2,0,200) Snakebox(470,200) Spike(1,18,30,20) Variblock(2,10,50,50) Platform(50,150,False,0) Variblock(5,2,250,70) Variblock(5,2,500,70) #level4 Variblock(45,2,550,100) Spike(20,1,550,90) goal(10,100,980,0) ###Snakes!! if self.player2==True: self.q=Player((100,450),1) if self.levelindex==5: self.progress=True self.p = Player((60,50),0) Variblock(3,105,0,0) Variblock(105,4,0,500) Variblock(3,90,990,0) ###NonborderTerrain Variblock(20,3,30,150) Spike(20,1,30,145) Variblock(3,10,260,210) Spike(3,1,260,205) #Block 1 Variblock(80,3,50,410) Spike(80,1,50,400) #Block 2 Variblock(10,3,420,280) Spike(10,1,420,310) Spike(1,3,420,280) #Block 3 Variblock(3,10,600,180) Spike(1,10,630,175) Spike(3,1,600,175) #Block 4 Variblock(13,3,400,180) Variblock(3,10,400,90) Spike(13,1,400,200) Spike(1,12,400,90) #Block 5 Variblock(10,3,700,300) goal(20,20,500,470) if self.player2==True: self.q=Player((60,50),1) if self.levelindex==5.5: self.p=Player((500,50),0) Variblock(3,105,0,0) Variblock(3,90,990,0) Variblock(105,4,0,500) Spike(92,1,30,440) goal(100,20,450,480) #Walljumps n Stuff Variblock(2,15,550,0) #Diagonal Jump Spike(10,1,450,150) Variblock(2,5,430,0) Spike(5,1,400,110) Spike(5,1,400,50) Spike(2,1,380,120) Spike(2,1,380,60) Spike(2,1,360,130) Spike(2,1,360,70) sprong(30,430) Variblock(6,2,150,100) Variblock(2,2,450,400) Variblock(2,2,550,400) Variblock(2,2,620,380) Variblock(2,5,700,270) Spike(2,1,700,260) Variblock(2,5,600,230) Spike(2,1,600,220) sprong(750,300) Spike(1,40,800,50) Variblock(5,2,800,50) Spike(7,1,920,250) Variblock(1,5,810,200) if self.player2==True: self.q=Player((500,50),1) if self.levelindex==6: self.p=Player((50,260),0) Variblock(103,20,0,300) Variblock(103,25,0,0) Snakebox(800,300) Snakebox(200,300) Snakebox(500,300) goal(20,50,1000,250) if self.player2==True: self.q=Player((50,260),1) player2=False def step(self): if self.p: self.levelfinish=self.p.collidingWithSprites(goal) self.playerhurt=self.p.collidingWithSprites(Spike) self.playerhurt1=self.p.collidingWithSprites(Snake) self.playerhurt.extend(self.playerhurt1) if self.q: self.playerhurt3=self.q.collidingWithSprites(Spike) self.playerhurt4=self.q.collidingWithSprites(Snake) self.playerhurt.extend(self.playerhurt3) self.playerhurt.extend(self.playerhurt4) self.levelfinish2=self.q.collidingWithSprites(goal) self.levelfinish.extend(self.levelfinish2) self.select=self.p.collidingWithSprites(goal2) #if len(self.select): #self.levelindex=self.c.i#-((self.c.x)-self.c.x%100)/100 for l in self.select: self.levelindex=l.i if len(self.levelfinish): if self.progress==True: print(self.levelindex) self.levelindex=self.levelindex+.5 print(self.levelindex) self.progress=False if len(self.playerhurt): for s in self.getSpritesbyClass(Player): s.destroy() for q in self.getSpritesbyClass(Spike): q.destroy() for a in self.getSpritesbyClass(Variblock): a.destroy() for c in self.getSpritesbyClass(Collide): c.destroy() for s in self.getSpritesbyClass(sprong): s.destroy() for s in self.getSpritesbyClass(textbox): s.destroy() for s in self.getSpritesbyClass(Snake): s.destroy() for s in self.getSpritesbyClass(Snakebox): s.destroy() for s in self.getSpritesbyClass(Platform): s.destroy() for s in self.getSpritesbyClass(goal): s.destroy() for s in self.getSpritesbyClass(goal2): s.destroy() if self.levelindex>.5: self.levelindex-=.5 for ship in self.getSpritesbyClass(Player): ship.step() for p in self.getSpritesbyClass(Snake): p.step() for p in self.getSpritesbyClass(Snakebox): p.step() for p in self.getSpritesbyClass(Platform): p.step()