def main(): win = GraphWin("Fall Drawing", 600, 600, autoflush=False) draw_background(win) sun = draw_sun(win) draw_forest(win) end = False while not end: key = win.checkKey() if key == "q": end = True win.close() update(win, sun) graphics.update(30)
threes = [3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58] fours = [4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59] fives = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] while True: if int(strftime('%S')) in ones: cir1.undraw() cir1.draw(win) cir2.undraw() cir3.undraw() cir4.undraw() cir5.undraw() if int(strftime('%S')) in twos: cir2.draw(win) if int(strftime('%S')) in threes: cir3.draw(win) if int(strftime('%S')) in fours: cir4.draw(win) if int(strftime('%S')) in fives: cir5.draw(win) sleep(1) if win.checkKey() == 'q': win.close()
draw_pik_score(pikScore, "0") debug_print() # --------------------------------------------------------- # Start game loop # --------------------------------------------------------- while alive: # if checkCapture(): # alive = False # else: # calcScore() coordinate = win.checkMouse() if coordinate is not None: alive = False move = win.checkKey() if move != "": move_pikman(move) if (gameDuration - gameTimer) < 0: alive = False else: # gameTimer = gameTimer - 1 gameTimer = time.time() - gameStart draw_pik_score(pikScore, f"{gameTimer:.1f}") # print("time: " + str(f'{gameTimer:.1f}')) animate_board() pikColor = draw_pikman(pikLocation, pikColor) if pikScore >= maxScore: draw_notice("Click anywhere to quit", "black") draw_notice("Winner!", "yellow")
def main(): gameOver = False eaten = False bodyPieces = [ SnakePiece([21, 20]), SnakePiece([22, 20]), SnakePiece([23, 20]), SnakePiece([24, 20]) ] fruit = Fruit() win = GraphWin("Snek!", 400, 400) head = SnakeHead([20, 20], Direction.Left) # region Initialise Sprites To Window head._gfx.draw(win) for p in bodyPieces: p._gfx.draw(win) fruit._gfx.draw(win) # endregion while (not gameOver): head._gfx.undraw() for p in bodyPieces: p._gfx.undraw() for i in range(len(bodyPieces) - 1, 0, -1): bodyPieces[i]._pos = bodyPieces[i - 1]._pos bodyPieces[0]._pos = head.getPos() if (eaten): spaceFree = True for p in bodyPieces: if (p._pos == pendingSnakePos): spaceFree = False if (spaceFree): bodyPieces.append(Snake(pendingSnakePos)) eaten = False head.moveSelf() for p in bodyPieces: p.getGFX() p._gfx.draw(win) head._gfx.draw(win) if (head._pos == fruit._pos): fruit._gfx.undraw() fruit.move() fruit._gfx.draw(win) eaten = True pendingSnakePos = head._pos key = win.checkKey() if (key == "Up"): head._facing = Direction.Up elif (key == "Right"): head._facing = Direction.Right elif (key == "Down"): head._facing = Direction.Down elif (key == "Left"): head._facing = Direction.Left elif (key == "Escape"): gameOver = True sleep(0.75) win.close() print("Game Over! Score was: " + str(len(bodyPieces))) sleep(5)
class Game: def __init__(self): self.window = GraphWin(TITLE, WindowLength, WindowHeight, autoflush = False) self.window.setBackground('black') self.Players = None self.score = 0 self.coins = [] self.best = 0 self.PauseObj = Text(Point(PauseX, PauseY), "Press p to pause!") self.PauseObj.setTextColor('red') def generate_coins(self): tomStart, jerryStart = self.Players.tomPos, self.Players.jerryPos self.coins, self.total = draw_coins(self.window, tomStart, jerryStart, self.Players.tunnels) def initiate_score(self): self.scoreObj, self.bestObj = spawn_score(self.window) def update_score_and_coins(self): obj,sc = self.coins[self.Players.tomPos[0]][self.Players.tomPos[1]] if obj: self.score += sc update_score(self.score, self.scoreObj) self.coins[self.Players.tomPos[0]][self.Players.tomPos[1]] = (None,0) destroy_coin(obj) def draw_map(self): draw_borders(self.window) self.obstacles = draw_obstacles(self.window) def clear(self): self.window.autoflush = False for item in self.window.items[:]: item.undraw() update() self.window.autoflush = True def check_win_loss(self): if self.Players.tomPos in self.Players.jerryPos: self.game_over() return True if self.score == self.total: self.game_win() return True def pause(self): self.PauseObj.setText("Game Paused!") while(self.window.checkKey() != "p"): continue self.PauseObj.setText("Press p to pause.") def gameLoop(self, weight): i = 0 while not self.check_win_loss(): key = self.window.checkKey() if key == "w": if self.Players.move_tom(-1,0): self.update_score_and_coins() elif key == "s": if self.Players.move_tom(1,0): self.update_score_and_coins() elif key == "a": if self.Players.move_tom(0,-1): self.update_score_and_coins() elif key == "d": if self.Players.move_tom(0,1): self.update_score_and_coins() elif key == "p": self.pause() elif key == "e": self.game_over() break if i == weight: i = 0 if i == 0: self.Players.move_jerry() i += 1 update(PLAYER_SPEED) def choose_difficulty(self): self.clear() key = None draw_bot(gameInstance.window, 'green', "Press 1 for difficulty level easy!\n\nPress 2 for difficulty level medium!\n\nPress 3 for difficulty level hard") while not key: key = self.window.checkKey() if key == "1": self.start_easy() elif key == "2": self.start_medium() elif key == "3": self.start_hard() else: self.start_medium() def start_easy(self): self.clear() self.scoreObj, self.bestObj = spawn_score(self.window) update_score(self.best, self.bestObj) self.PauseObj.draw(self.window) self.Players = Player(self.window, 1) self.draw_map() self.generate_coins() self.gameLoop(JERRY_WEIGHT_EASY) def start_medium(self): self.clear() self.scoreObj, self.bestObj = spawn_score(self.window) update_score(self.best, self.bestObj) self.PauseObj.draw(self.window) self.Players = Player(self.window, 2) self.draw_map() self.generate_coins() self.gameLoop(JERRY_WEIGHT_MEDIUM) def start_hard(self): self.clear() self.scoreObj, self.bestObj = spawn_score(self.window) update_score(self.best, self.bestObj) self.PauseObj.draw(self.window) self.Players = Player(self.window, 3) self.draw_map() self.generate_coins() self.gameLoop(JERRY_WEIGHT_HARD) def show_help(self): self.clear() draw_middle(self.window, 'blue', "'E' is the player who has to collect all coins.", -2) draw_middle(self.window, 'red', "'J' is the bot who will chase you to catch you.", -1) draw_middle(self.window, GOLD_COIN, "Golden coin is worth 2 points", 0) draw_middle(self.window, SILVER_COIN, "Silver coin is worth 1 points", 1) draw_middle(self.window, 'brown', "'\U0001F6AA' represents tunnel through which you can teleport to a corresponding tunnel", 2) draw_middle(self.window, 'green', "'#' represents obstacles which player or bot cannot pass", 3) draw_middle(self.window, 'green', "Press any key to continue!", 5) key = None while not key: key = self.window.checkKey() self.play() def play(self): key = None self.clear() draw_head(self.window, 'green', START) draw_bot(gameInstance.window, 'green', "Press h for help.\n\nPress any other key to start!") while not key: key = self.window.checkKey() if key == "h": self.show_help() else: self.choose_difficulty() def game_over(self): self.clear() key = None if self.score > self.best: self.best = self.score self.score = 0 draw_head(gameInstance.window, 'red', END) draw_bot(gameInstance.window, 'white', "Your Final Score: "+str(self.best)+"\n\n Press e to exit."+ "\n\n Press any other key to play again!") while not key: key = gameInstance.window.checkKey() if key != "e": self.play() def game_win(self): self.clear() key = None if self.score > self.best: self.best = self.score self.score = 0 draw_head(gameInstance.window, 'green', WIN) draw_bot(gameInstance.window, 'white', "Your Final Score: "+str(self.best)+"\n\n Press e to exit."+ "\n\n Press any other key to play again!") while not key: key = gameInstance.window.checkKey() if key != "e": self.play() def exit(self): self.window.close()
class Game: window = None snake = None food = None def __init__(self): self.window = GraphWin( title="dj-snake", width=TILE_SIZE * BOARD_LENGTH, height=TILE_SIZE * BOARD_LENGTH, ) self.window.setBackground(BACKGROUND_COLOR) for x in range(1, BOARD_LENGTH): for y in range(1, BOARD_LENGTH): self.window.plotPixel(x * TILE_SIZE, y * TILE_SIZE, SNAKE_COLOR) self.snake = Snake(self.window) self.spawnFood() def spawnFood(self): x, y = randint(0, BOARD_LENGTH - 1), randint(0, BOARD_LENGTH - 1) while any(block.coordinates == (x, y) for block in self.snake.blocks): x, y = randint(0, BOARD_LENGTH), randint(0, BOARD_LENGTH) self.food = Block(x, y, self.window, FOOD_COLOR) def checkIllegal(self): headCoordinates = self.snake.blocks[0].coordinates if any(headCoordinates == block.coordinates for block in self.snake.blocks[1:]): return True elif any(x < 0 or x >= BOARD_LENGTH for x in headCoordinates): return True return False def play(self): while True: time.sleep(UPDATE_INTERVAL) self.snake.changeDirection(self.window.checkKey()) if self.snake.blocks[0].coordinates == self.food.coordinates: self.snake.grow() self.food.destroy() self.spawnFood() if not self.checkIllegal(): self.snake.update() self.getEnvironmentParams() else: break def getEnvironmentParams(self): # return information about left, fleft, front, fright and right # 3 possible things detected: wall, snake, food # ie 5 parameters in total delta = dict() if self.snake.direction == 'Up': delta["front"] = (0, -1) delta["left"] = (-1, 0) delta["right"] = (1, 0) delta["fleft"] = (-1, -1) delta["fright"] = (1, -1) elif self.snake.direction == 'Down': delta["front"] = (0, 1) delta["left"] = (1, 0) delta["right"] = (-1, 0) delta["fleft"] = (1, 1) delta["fright"] = (-1, 1) elif self.snake.direction == 'Right': delta["front"] = (1, 0) delta["left"] = (0, -1) delta["right"] = (0, 1) delta["fleft"] = (1, -1) delta["fright"] = (1, 1) elif self.snake.direction == 'Left': delta["front"] = (-1, 0) delta["left"] = (0, 1) delta["right"] = (0, -1) delta["fleft"] = (-1, 1) delta["fright"] = (-1, -1) sight = { "front": "wall", "left": "wall", "right": "wall", "fleft": "wall", "fright": "wall" } for direction, step in delta.items(): scanner = self.snake.blocks[0].coordinates while scanner[0] >= 0 and scanner[0] <= BOARD_LENGTH and scanner[ 1] >= 0 and scanner[1] <= BOARD_LENGTH: scanner = (scanner[0] + step[0], scanner[1] + step[1]) if scanner == self.food.coordinates: sight[direction] = 'food' break elif scanner in [b.coordinates for b in self.snake.blocks]: sight[direction] = 'snake' break params = (1 if sight["left"] == "wall" else 0, 1 if sight["fleft"] == "wall" else 0, 1 if sight["front"] == "wall" else 0, 1 if sight["fright"] == "wall" else 0, 1 if sight["right"] == "wall" else 0, 1 if sight["left"] == "snake" else 0, 1 if sight["fleft"] == "snake" else 0, 1 if sight["front"] == "snake" else 0, 1 if sight["fright"] == "snake" else 0, 1 if sight["right"] == "snake" else 0, 1 if sight["left"] == "food" else 0, 1 if sight["fleft"] == "food" else 0, 1 if sight["front"] == "food" else 0, 1 if sight["fright"] == "food" else 0, 1 if sight["right"] == "food" else 0) return params