def drawPiece(piece, canvas): if piece.color == white: piece.image = graphics.Image(piece.center, whitePath) else: piece.image = graphics.Image(piece.center, blackPath) piece.image.draw(canvas) sleep(.05)
def showCard(self, x, y, card_type): if card_type == "queen": graphics.Image(graphics.Point(x, y), "queen.gif").draw(self.win) elif card_type == "dragon": graphics.Image(graphics.Point(x, y), "dragon.gif").draw(self.win) else: graphics.Image(graphics.Point(x, y), "cow.gif").draw(self.win)
def main(): window = graphics.GraphWin("scrap", 500, 500) bg = graphics.Image(graphics.Point(400, 100), 'ogre-attack1.gif') bg.draw(window) L2xPos_bg = bg.getAnchor().getX() - bg.getWidth() / 2 L2yPos_bg = bg.getAnchor().getY() - bg.getHeight() / 2 R2xPos_bg = bg.getAnchor().getX() + bg.getWidth() / 2 R2yPos_bg = bg.getAnchor().getY() + bg.getHeight() / 2 l2 = bg.graphics.Point(L2xPos_bg, L2yPos_bg) r2 = bg.graphics.Point(R2xPos_bg, R2yPos_bg) hero = graphics.Image(graphics.Point(200, 300), 'horseman-ne-attack6.gif') hero.draw(window) L1xPos_hero = hero.getAnchor().getX() - hero.getWidth() / 2 L1yPos_hero = hero.getAnchor().getY() - hero.getHeight() / 2 R1xPos_hero = hero.getAnchor().getX() + hero.getWidth() / 2 R1yPos_hero = hero.getAnchor().getY() + hero.getHeight() / 2 l1 = hero.graphicsPoint(L1xPos_hero, L1yPos_hero) r1 = hero.graphicsPoint(R1xPos_hero, R1yPos_hero) if l1.getX() > r2.getX() or l2.getX() > r1.getX(): return False # If one rectangle is above other if (l1.getY() > r2.getY() or l2.getY() > r1.getY()): return False return True # we could use else but not really needed window.getMouse()
def hand_images(self): ''' This method will display images that represent human hands and computer hands ''' c_lImage = graphics.Image(graphics.Point(130, 275), "c_l.gif") c_lImage.draw(self.win) c_rImage = graphics.Image(graphics.Point(478, 275), "c_r.gif") c_rImage.draw(self.win) h_lImage = graphics.Image(graphics.Point(130, 475), "h_l.gif") h_lImage.draw(self.win) h_rImage = graphics.Image(graphics.Point(478, 475), "h_r.gif") h_rImage.draw(self.win)
def endScreen(self, masterDict): """Create a window displays the numbers of sick and healthy people for each age group along with the images. Close it when the user clicks. masterDict is the dictionary of community members after the entire simulation has been run.""" self.endScreen = graphics.GraphWin("The Common Code", 800, 800) self.endScreen.setCoords(0, 0, 800, 800) numSickKid = 0 numHealthyKid = 0 numSickAdult = 0 numHealthyAdult = 0 numSickElderly = 0 numHealthyElderly = 0 for key in masterDict: if key <= 18: numSickKid += masterDict[key][1] numHealthyKid += masterDict[key][0] elif key <= 60: numSickAdult += masterDict[key][1] numHealthyAdult += masterDict[key][0] else: numSickElderly += masterDict[key][1] numHealthyElderly += masterDict[key][0] bigString = graphics.Text(graphics.Point(400, 700), "SIMULATION COMPLETE") bigString.setSize(36) bigString.setStyle("bold") bigString.setFill("purple4") kidImage = graphics.Image(graphics.Point(400,450),"img/kids.gif") adultImage = graphics.Image(graphics.Point(200,150),"img/adult.gif") elderlyImage = graphics.Image(graphics.Point(600,150),"img/elderly.gif") kidString = graphics.Text(graphics.Point(400,600),"CHILDREN \n Sick: " + str(numSickKid) + "\n Healthy: " + str(numHealthyKid)) kidString.setSize(20) kidString.setFill("purple4") adultString = graphics.Text(graphics.Point(200,300),"ADULTS \n Sick: " + str(numSickAdult) + "\n Healthy: " + str(numHealthyAdult)) adultString.setSize(20) adultString.setFill("purple4") elderlyString = graphics.Text(graphics.Point(600,300),"ELDERLY \n Sick: " + str(numSickElderly) + "\n Healthy: " + str(numHealthyElderly)) elderlyString.setSize(20) elderlyString.setFill("purple4") bigString.draw(self.endScreen) kidImage.draw(self.endScreen) kidString.draw(self.endScreen) adultImage.draw(self.endScreen) adultString.draw(self.endScreen) elderlyImage.draw(self.endScreen) elderlyString.draw(self.endScreen) self.endScreen.getMouse() # the window closes when the user clicks self.endScreen.close()
def __init__(self, win): self.win = win self.pic = grf.Image(grf.Point(575, 200), 'images/macrophage2.gif') self.speed = 10 self.jump = 350 self.jump_refresh = 3 self.last_jump = 0
def introduction(self): ''' This method will display introduction and rules to human player ''' #this click_ thing tells the player to click to continue click_ = graphics.Text(graphics.Point(600, 550), "Click to Continue") click_.setStyle("bold italic") click_.setTextColor("Plum") click_.draw(self.win) #this part will display images for the rule Images = [ "intro_pic.gif", "r1.gif", "r2.gif", "r3.gif", "r4.gif", "r5.gif" ] for im in Images: rules = graphics.Image(graphics.Point(337, 300), im) rules.draw(self.win) click_p = self.win.getMouse() rules.undraw() rules_t = graphics.Text(graphics.Point(337, 300), "The player loses if both hands are ‘dead’") rules_t.setTextColor("Dark Olive Green") rules_t.setSize(20) rules_t.draw(self.win) click_p = self.win.getMouse() rules_t.undraw() click_.undraw()
def showField(): #Generates window and basic objects """Generates window and basic objects, returns window and object list.""" if settings[3]: #Hardcore setting lw = gr.GraphWin("Pysweeper | HARDCORE Game", settings[0] * 40, (settings[1] * 40) + 40, False) else: lw = gr.GraphWin("Pysweeper | Game", settings[0] * 40, (settings[1] * 40) + 40, False) lw.setBackground(gr.color_rgb(33, 33, 33)) li = [] # 0: Top bar background # 1: Remaining flags # 2: Timer # 3: Reset icon li.append(gr.Rectangle(gr.Point(0, 0), gr.Point(lw.width, 40))) li[0].setWidth(0) li[0].setFill(gr.color_rgb(66, 66, 66)) li.append(gr.Text(gr.Point(30, 20), "000")) li[1].setSize(20) li[1].setFill(gr.color_rgb(250, 250, 250)) li.append(gr.Text(gr.Point(lw.width - 30, 20), "000")) li[2].setSize(20) li[2].setFill(gr.color_rgb(250, 250, 250)) li.append(gr.Image(gr.Point(lw.width / 2, 20), "img/Reset.png")) li[1].setText((3 - len(str(rflags))) * "0" + str(rflags)) for i in li: i.draw(lw) lw.flush() return lw, li
def findWinner(self): # Figures out who the winner is, or if it's a tie, based on number of pieces on the board. if self.board.getScore(self.players[0], self.board.boardArray) > self.board.getScore( self.players[1], self.board.boardArray): winner, count = self.players[0].getColor(), self.board.getScore( self.players[0], self.board.boardArray) elif self.board.getScore(self.players[1], self.board.boardArray) > self.board.getScore( self.players[0], self.board.boardArray): winner, count = self.players[1].getColor(), self.board.getScore( self.players[1], self.board.boardArray) else: winner, count = -1, -1 if winner == 0: winnerColor = 'white' elif winner == 1: winnerColor = 'black' # Creates the strings. The shadow is the same text off-set slightly and black. if winner == -1 and count == -1: winString = "It’s a draw!" else: winString = 'The ' + winnerColor + ' player wins with ' + str( count) + ' pieces!' self.winMessageBackground = graphics.Image(graphics.Point(320, 321), winMessagePath) self.winShadow = graphics.Text(graphics.Point(321, 321), winString) self.winMessage = graphics.Text(graphics.Point(320, 320), winString) self.winShadow.setSize(36) self.winMessage.setSize(36) self.winMessage.setTextColor(textColor) self.winMessageBackground.draw(self.board.canvas) self.winShadow.draw(self.board.canvas) self.winMessage.draw(self.board.canvas)
def initializeMap(mapFile, xPixels, yPixels): win = graphics.GraphWin('Sacramento Area', xPixels, yPixels) win.setCoords(0, 0, xPixels, yPixels) center = graphics.Point(xPixels / 2, yPixels / 2) image = graphics.Image(center, mapFile) image.draw(win) return win
def showCrash(self, lander): '''Crash message... displays an image of a crashed lunar lander and displays a graphical text to the user saying that the lander has crashed. Gives the user a score based on his / her performance.''' self.landerPolygon.undraw() self.landerPolygon = graphics.Polygon( graphics.Point(self.win.width / 2 - 13, 0), graphics.Point(self.win.width / 2 - 6, 10), graphics.Point(self.win.width / 2 - 3, 10), graphics.Point(self.win.width / 2 - 3, 0)) self.landerPolygon = graphics.Image(graphics.Point(100, 150), "explosion.gif") self.landerPolygon.draw(self.win) #displays an image of an explosion to show the crash of the lander crash = graphics.Text(graphics.Point(150, 460), "Crash! Oh noes!") crash.setStyle("bold") crash.setSize(30) crash.setFace("helvetica") crash.setTextColor("white") crash.draw(self.win) fuel = lander.getFuel() velocity = lander.getVelocity() scoreCalc = int(fuel + (100 / (-1 * velocity))) #self-derived formula for calculating a player's score. The higher the velocity, #the lower the score will be, (vel *-1 as vel will be negative upon reaching the surface). #Likewise, the more fuel left, the higher the score will be. scoreString = "Your overall score is: " + str(scoreCalc) score = graphics.Text(graphics.Point(150, 430), scoreString) score.setFill("red") score.draw(self.win) self.win.getMouse() if self.win.getMouse(): self.win.close()
def main(argv): f = open(argv[1][:-3] + 'vhd', 'w') sys.stdout = f if len(argv) < 2: print('Usage: python %s <filename>' % (argv[0])) exit() return printStart(argv) src = graphics.Image(graphics.Point(0, 0), argv[1]) display.displayImage(src, argv[1][:-4]) num_rows = src.getHeight() num_cols = src.getWidth() row = 0 col = 0 while row < num_rows: while col < num_cols: (r, g, b) = src.getPixel(col, row) pixelString = "\"" + dec2bin4(r / 16) + dec2bin4( g / 16) + dec2bin4(b / 16) + "\" when X = " + str( col) + " AND Y = " + str(row) + " else" print(pixelString) col += 1 col = 0 row += 1 print('"000000000000"; -- should never get here') print("end rtl;") f.close() return
def __init__(self, win, num): self.win = win self.num = num self.loc = grf.Point(50+(self.num*100), self.win.height-30) self.pic = grf.Image(self.loc, 'images/cells.gif') self.health = 100 self.bar = self.make_bar()
def maskSprite(win, x, y, image): mask = gr.Image(gr.Point(x * 10, win.getHeight() - y * 10), getImage(image)) mask.draw(win) return mask
def drawWin(gameWin): '''Draws stars all over the screen to indicate a win by the user Parameter: gameWin - window the graphic is drawn into ''' for i in range(0, 1200, 100): for j in range(0, 800, 100): winStar = graphics.Image(graphics.Point(i, j), "goldstar.gif") winStar.draw(gameWin) finalStar = graphics.Image(graphics.Point(500, 325), "goldstar.gif") finalStar.draw(gameWin) winText = graphics.Text(graphics.Point(500, 325), "YOU WIN!") winText.setFace("arial") winText.setStyle('bold') winText.setSize(36) winText.draw(gameWin)
def playController(self): play = True # Creates the graphics that will be used between each game for input. continueButton = Button(self.board.canvas, graphics.Point(318, 654), graphics.Point(385, 678)) quitButton = Button(self.board.canvas, graphics.Point(407, 654), graphics.Point(474, 678)) continueButton.deactivate() quitButton.deactivate() playAgain = graphics.Image(graphics.Point(320, 665), playAgainPath) while play: # Update score display. self.infoDisplayUpdate() # This while loop will run till the end of the game. while not self.gameOver(): self.turn() self.findWinner() # Asks to continue from here on, and if yes will go back to the while play loop. play = None playAgain.draw(self.board.canvas) while play == None: continueButton.activate() quitButton.activate() click = waitForClick(self.board.canvas) if quitButton.clicked(click): play = False elif continueButton.clicked(click): play = True continueButton.deactivate() quitButton.deactivate() playAgain.undraw() self.restartGame() else: play = None
def displayIntro( hero ): #passing the hero object so the hero name can be formatted in the introduction """Creates the spooky introduction with an image to the Monster Movie Adventure Game. Using sleep so that all text doesn't pop up at once""" monsterIntroImage = 'Monster Intro.gif' #adding an image adds an extra creative touch that was not listed in the requirments win = g.GraphWin("Movie Monster Game", 400, 400) gameIntroImage = g.Image(g.Point(200, 200), monsterIntroImage) gameIntroImage.draw(win) #print("Welcome to the Classic Movie Monsters Adventure Game {}!".format(hero.name)) print( "{}, you are a movie projectionist and a magician has put a spell on all of the classic movie monster films" .format(hero.heroName)) time.sleep(1) print("They have come to life and you must hunt them all down...") time.sleep(1) print("Before they hunt you!!!!") time.sleep(1) print( "Collect 3 movie reels and make it to the projection room in 10 steps to reverse the spell!! Good luck!!" ) time.sleep(1) print(" ") print( "*************************************************************************************************************" ) win.close()
def sweep(self, t, lw): #Shows bomb icon """Reveals if tile is safe or not, returns graphics object.""" self.sweeped = True self.gr[0].setFill(gr.color_rgb(66, 66, 66)) self.l = gr.Image(gr.Point((40 * self.x) + 20, (40 * self.y) + 60), "img/Bomb.png") return self.l
def sideBySideList(): ''' Function to test out placing an image (left) next to its filtered version (right) ''' # Create an image width = 300 height = 400 img = gr.Image(gr.Point(0, 0), width, height) # Make image list, with x, y placement info, how to process it (str), # and the `Image` object # We want a yellow image on the left, a pink one on the rights imgListofLists = [[0, 0, 'pink', img], [width, 0, 'yellow', img]] # Make 1x2 canvas canvas = gr.Image(gr.Point(0, 0), 2 * width, height) '''Process is: - Loop through the list, deal with one sublist at a time. - Clone the image object (original image by default). - Apply the solid color depending on the color name in the sublist. - Place it on the canvas. - Display. ''' for imgList in imgListofLists: # imgList is a SUBLIST currentImage = imgList[-1].clone() if imgList[2] == 'red': solid.solid_fill(currentImage, r=255) elif imgList[2] == 'blue': solid.solid_fill(currentImage, b=255) elif imgList[2] == 'yellow': solid.solid_fill(currentImage, r=255, g=255) elif imgList[2] == 'pink': solid.solid_fill(currentImage, r=255, b=255) solid.placeImageinCanvas(canvas, currentImage, imgList[0], imgList[1]) # Update image object in list of lists imgList[-1] = currentImage # Display canvas screen = display.displayImage(canvas, 'Our canvas') # Wait for mouse click to close window and end program screen.getMouse()
def drawImage(filename): """Draws an image given by filename (should be in the same folder as your python script). The top left of the image is the current graphics pen point. @param filename Filename of image to display. Must be a gif. """ im = graphics.Image(_current_point, filename) im.config["anchor"] = "nw" _objects.append(im)
def drawTie(gameWin): '''Draws ties all over the screen to indicate it was a tie game also draws text Parameter: gameWin - window the graphic is drawn into ''' for i in range(0, 1200, 100): for j in range(0, 800, 100): tie = graphics.Image(graphics.Point(i, j), "tie.gif") tie.draw(gameWin) finalTie = graphics.Image(graphics.Point(500, 325), "finalTie.gif") finalTie.draw(gameWin) tieText = graphics.Text(graphics.Point(500, 250), "It's a tie!") tieText.setFace("arial") tieText.setStyle('bold') tieText.setSize(36) tieText.draw(gameWin)
def intro(win): ''' draws the start screen comprising of a background image ''' # start screen welcome = gr.Image(gr.Point(285, 300), "background.gif") welcome.draw(win) start.append(welcome)
def main(): winX = 440 winY = 660 winCenter = graphics.Point(winX / 2, winY / 2) winName = "| bullet.heck |" menu = engine.Menu() hud = engine.Hud() player = engine.Player() window = graphics.GraphWin(winName, winX, winY, autoflush=False) wallpaper = graphics.Image(winCenter, "assets/space.gif") window.setBackground("black") wallpaper.draw(window) while (window): menu.main(window) player.score = 0 player.lives = 4 patternNumber = 0 nextSpawn = randint(125, 160) player = engine.Player() patternList = list() player.draw(window) hud.draw(window) hud.update_bar(window, player) hud.update_score(window, player.score) while (player.lives > 0): # Game loop starts here. press = window.checkKey() if (player.score % nextSpawn == 0): nextSpawn = randint(125, 160) patternList.append(engine.build_attack()) player.hit = False player.move(press) for i in range (len(patternList)): patternList[i].fire(window) patternList[i].move() if (patternList[i].detect_hit(player)): player.hit = True player.score = player.score + 1 player.update_hitframes() player.update_lives() player.update_shield(window) hud.update_bar(window, player) hud.update_score(window, player.score) hud.pause(press, window) if (player.lives == 0): for i in range (len(patternList)): patternList[i].undraw() player.undraw() hud.undraw(window) menu.game_over(window, player.score) sleep(.03) graphics.update(30) # Game loop ends here. return
def gamePlay(gameWin, location, gameBoard, difficulty, strategy): '''Draws the gameBoard for Connect Four in the location chosen by the player Parameter: gameWin - window the graphic is drawn into location - image file of the location chosen by the player gameBoard - list of lists containing pieces difficulty - the click of the user used to pick strategy strategy - the computer's way of choosing where to put a piece ''' gamePlace = graphics.Image(graphics.Point(500, 325), location) gamePlace.draw(gameWin) grid = graphics.Image(graphics.Point(500, 300), "CFBoard.gif") grid.draw(gameWin) getFour = False turnCount = 0 while getFour == False: if turnCount % 2 == 0: playerColor = "maize" setColumn = getHumanColumn(gameWin) else: playerColor = "blue" if strategy.strategyReturn() == "easy": setColumn = strategy.easy() if strategy.strategyReturn() == "medium": setColumn = strategy.medium(gameBoard) if strategy.strategyReturn() == "hard": setColumn = strategy.hard(gameBoard) if strategy.strategyReturn() == "advanced": setColumn = strategy.advanced(gameBoard) filledSpot = gameBoard.getTileCoordinate(setColumn, playerColor) gameBoard.playTile(filledSpot, playerColor) tileFall(gameWin, filledSpot, playerColor) end = gameBoard.checkForWin() if end == "win": drawWin(gameWin) getFour = True elif end == "loss": drawLoss(gameWin) getFour = True elif end == "tie": drawTie(gameWin) getFour = True else: turnCount += 1 gameWin.getMouse()
def make_pic(self): if self.kind == 'glucose': loc = grf.Point() elif self.kind == 'protein': loc = grf.Point() elif self.kind == 'lipid': loc = grf.Point() pic = grf.Image(loc, 'images/{}.gif'.format(self.kind)) return pic
def __init__(self, win, scale): self.win = win self.loc = grf.Point(random.randint(25, self.win.width - 225), random.randint(25, 50)) self.pic = grf.Image(self.loc, 'images/virus.gif') self.power = random.randint(1, 5) * (int(scale**.5)+1) self.health = random.randint(5, 10) * (int(scale**.5)+1) self.move_prob = 2 self.speed = random.randint(1, 4) * (int(scale**.5)+1)
def displayCard(self, x, y): imageURL = self.showImageURL() # print(imageURL) raw_data = request.urlopen(imageURL).read() image = Image.open(io.BytesIO(raw_data)) image = image.resize((97, 142), Image.ANTIALIAS) # print(type(image)) image = graphics.Image(graphics.Point(x, y), image) return image
def BadGuys(window): #create list for bad guys instead of variable for each bad guy bad_guy_list = [] for i in range(10): bad_guy = graphics.Image(graphics.Point(32 + i * 75, 150), "burner-fly-2.gif") bad_guy_list.append(bad_guy) #append adds to list bad_guy.draw(window) return bad_guy_list
def getImage( self ): #adding an image adds an extra creative touch that was not listed in the requirments. Makes the fight more interesting. win = g.GraphWin("Monster!!", 450, 450) monsterImage = self.image filmReel = g.Image(g.Point(200, 200), monsterImage) filmReel.draw(win) time.sleep(1.5) win.close()
def introScreen(self): """Create a window.""" self.introScreen = graphics.GraphWin("The Common Code", 800, 800) self.introScreen.setCoords(0, 0, 800, 800) self.background = graphics.Image(graphics.Point(400, 500), "img/map.gif") self.background.draw(self.introScreen) self.baseString = graphics.Text(graphics.Point(400, 100),"Please select the probability of getting sick for the school, \nthe office, and the shop, and the probability of recovery for \nthe hospital. Probability is based on age.") self.baseString.setStyle("bold") self.baseString.setSize(26) self.baseString.draw(self.introScreen)