def test_changeDirection(self): # test change once pacman = Pacman(0,0,NORTH) pacman.changeDirection(LEFT) self.assertEqual(pacman.direction, WEST) pacman.changeDirection(RIGHT) pacman.changeDirection(RIGHT) pacman.changeDirection(RIGHT) pacman.changeDirection(RIGHT) pacman.changeDirection(RIGHT) # test go around a circle self.assertEqual(pacman.direction, NORTH)
def test_move(self): # normal movement pacman = Pacman(0,0,NORTH) pacman.move() self.assertEqual(pacman.x, 0) self.assertEqual(pacman.y, 1) # move around pacman.move() pacman.move() pacman.changeDirection(RIGHT) pacman.move() pacman.move() self.assertEqual(pacman.x, 2) self.assertEqual(pacman.y, 3)
def test_detectBoundary(self): # test 4 edge cases grid = Grid(5,5) pacman = Pacman(0,0,WEST) self.assertTrue(isMoveOutOfBoundary(grid,pacman)) pacman = Pacman(0,5,NORTH) self.assertTrue(isMoveOutOfBoundary(grid,pacman)) pacman = Pacman(5,3,EAST) self.assertTrue(isMoveOutOfBoundary(grid,pacman)) pacman = Pacman(3,0,SOUTH) self.assertTrue(isMoveOutOfBoundary(grid,pacman)) # test normal case pacman = Pacman(3,2,SOUTH) self.assertFalse(isMoveOutOfBoundary(grid,pacman))
def __init__(self, numGhosts, width, height): self.pacman = Pacman() #initialize pacbot self.ghosts = [Ghost() for i in range(self.NUM_GHOSTS)] #initialize ghosts self.maze = Maze(width, height) #initialize maze self.points = 0 self.bigDotMode = False self.bigDotTimeRemaining = 0 for ghost in self.ghosts: ghost.setPosition( 18, 18 ) # set starting positions, may have to split this up to assign different starting locations to each
def __init__(self): # Initialization pygame.init() self.DISPSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) self.FPSCLOCK = pygame.time.Clock() # Load the level levelFile = open('level1.txt') levelData = levelFile.read() self.level = Level(levelData) # Create pacman and the ghosts self.pac = Pacman() blinky = Blinky() pinky = Pinky() inky = Inky() clyde = Clyde() self.entities = { self.pac.name: self.pac, blinky.name: blinky, clyde.name: clyde, inky.name: inky, pinky.name: pinky } # Scatter timer pygame.time.set_timer(USEREVENT + 1, 5000) self.quit = False
def prep_ships(self): self.ships = Group() for ship_number in range(self.pacSettings.livesLeft): ship = Pacman(self.pacSettings, self.screen) ship.rect.x = 10 + ship_number * (ship.rect.width * 3) ship.rect.y = self.screen_rect.bottom - 50 self.ships.add(ship)
class CommandInput(object): """Interprets commands and sends them to Pacman class""" def __init__(self): self.pac = Pacman() #Method for interpreting inputCommand def _execute(self, inputCommand): inputCommandSplit = inputCommand.split(" ") #Move if inputCommand == "MOVE": self.pac.Move() #turn left elif inputCommand == "LEFT": self.pac.Left() #turn right elif inputCommand == "RIGHT": self.pac.Right() #report position elif inputCommand == "REPORT": return self.pac.Report() #Place pacman elif len(inputCommandSplit) == 2 and inputCommandSplit[0] == "PLACE": #get coordinates posSplit = inputCommandSplit[1].split(",") #Check coordinates are correct if len(posSplit) == 3 and self.__isNumber( posSplit[1]) and self.__isNumber( posSplit[1]) and self.__isFacing(posSplit[2]): self.pac.Place(int(posSplit[0]), int(posSplit[1]), posSplit[2]) #Return empty string by default return "" # Checks if user inputCommand for facing is valid def __isFacing(self, input): if input == self.pac.NORTH or input == self.pac.SOUTH or input == self.pac.EAST or input == self.pac.WEST: return True return False # Checks if user inputCommand for facing is valid def __isNumber(self, input): try: int(input) return True except ValueError: return False
def __init__(self): self.pacman = Pacman() blinky = Ghost("blinky", self.pacman, None) self.ghosts = [blinky, Ghost("pinky", self.pacman, None), Ghost("clyde", self.pacman, None), Ghost("inky", self.pacman, blinky)] self.dots = [] self.allDotsView = pygame.Surface((GameConfig.windowW, GameConfig.windowH)) self.allDotsView.set_colorkey((0, 0, 0)) self.dotsView = [] self.initDots() self.gameOver = False self.life = 2 self.level = 0 self.score = 0 self.date = time() self.stateDate = time() self.mode = 2 self.win = False self.loose = False
def run_game(): pygame.init() pygame.font.init() settings = Settings() screen = pygame.display.set_mode((settings.HEIGHT, settings.WIDTH)) pygame.display.set_caption("Pacman") blocks = Group() dots = Group() powers = Group() nodes = Group() pacman = Pacman(settings, screen) portal = Portal(screen) score = Score(settings, screen) menupac = Pacman(settings, screen) blinky = Ghost(screen, 'Blinky', (150, 200), settings) inky = Ghost(screen, 'Inky', (150, 250), settings) pinky = Ghost(screen, 'Pinky', (150, 300), settings) clyde = Ghost(screen, 'Clyde', (150, 350), settings) title = settings.title while not settings.GAME_ACTIVE: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: settings.GAME_ACTIVE = True textsurface = settings.myfont.render("PACMAN", False, (255, 255, 0)) menupac.rect.center = (150, 150) menupac.MOVINGRIGHT = True menupac.blitme() screen.blit(textsurface, (180, 145)) textsurface = settings.myfont.render("Press any Key to Continue", False, (255, 255, 255)) screen.blit(textsurface, (150, 600)) blinky.blitme() inky.blitme() pinky.blitme() clyde.blitme() textsurface = settings.myfont.render("BLINKY", False, (255, 0, 0)) screen.blit(textsurface, (180, 200)) textsurface = settings.myfont.render("INKY", False, (7, 225, 225)) screen.blit(textsurface, (180, 250)) textsurface = settings.myfont.render("PINKY", False, (225, 207, 231)) screen.blit(textsurface, (180, 300)) textsurface = settings.myfont.render("CLYDE", False, (255, 159, 7)) screen.blit(textsurface, (180, 350)) screen.blit(title, (0, 0)) textsurface = settings.myfont.render("HIGH SCORES", False, (255, 255, 255)) screen.blit(textsurface, (350, 100)) pygame.display.flip() gf.create_maze(settings, blocks, screen, dots, pacman, powers, nodes) while True: pacman.update() portal.update(pacman) gf.check_events(pacman, dots, blocks, powers, score, settings, screen, nodes, portal) gf.update_screen(blocks, dots, pacman, screen, settings, powers, score, portal)
# Create window wSurface = pygame.display.set_mode(WINDOWSIZE, 0, 32) pygame.display.set_caption("Pacman") import random, copy from Character import Character from Pacman import Pacman from Ghost import Ghost from Walls import Walls from Pellets import Pellets from Sound import Sound # Create game objects background = pygame.image.load("bg.png").convert() pacman = Pacman() ghosts = [Ghost()] walls = Walls.createList(Walls()) pellets_small = Pellets.createListSmall(Pellets()) pellets_large = Pellets.createListLarge(Pellets()) clock = pygame.time.Clock() pygame.mixer.music.load("bg_music.mp3") pygame.mixer.music.set_volume(0.5) # Opening screen and music Sound.channel.play(Sound.opening) wSurface.fill((0, 0, 0)) wSurface.blit(background, (100, 0)) wSurface.blit(pacman.getScoreSurface(), (10, 10)) wSurface.blit(pacman.getLivesSurface(), (WINDOWSIZE[0] - 200, 10)) for p in pellets_small:
class Game: GHOST_KILL_POINTS_INITAL = 200 #constant ghost_kill_points = 200 #changes when multiple ghosts are killed in a row BIG_DOT_COUNTER = 20 DOT_POINTS = 10 BIG_DOT_POINTS = 50 NUM_GHOSTS = 1 def __init__(self, numGhosts, width, height): self.pacman = Pacman() #initialize pacbot self.ghosts = [Ghost() for i in range(self.NUM_GHOSTS)] #initialize ghosts self.maze = Maze(width, height) #initialize maze self.points = 0 self.bigDotMode = False self.bigDotTimeRemaining = 0 for ghost in self.ghosts: ghost.setPosition( 18, 18 ) # set starting positions, may have to split this up to assign different starting locations to each def nextStep(self): for ghost in self.ghosts: ghost.move( self.maze ) #each ghost independently picks what direction it wants to move in self.pacman.move(self.maze, self.ghosts) #^ self.maze.checkDots(pacman) #check if a new dot has been passed over self.bigDotTimeRemaining -= 1 #count down big dot duration if bigDotTimeRemaining <= 0: self.bigDotMode = False ghost_kill_points = GHOST_KILL_POINTS_INITAL #reset score multiplier #print(self.maze) return self.checkLoss( ) #return whether a loss has occurred so the simulation can stop def checkLoss(self): for ghost in self.ghosts: if Game.isAdjacent( ghost.getPosition(), self.pacman.getPosition()) and not self.bigDotMode: return True elif ghost.getPosition() == self.pacman.getPosition( ) and self.bigDotMode: self.points += ghost_kill_points ghost.kill() ghost_kill_points *= 2 #points awarded for each subsequent ghost killed are doubled until the time runs out return False def bigDotCollected(self): self.bigDotMode = True self.bigDotTimeRemaining = BIG_DOT_COUNTER #reset countdown clock for big dot self.points += BIG_DOT_POINTS def checkDots(self): if self.maze.hasDot(pacman.getPosition()): self.points += DOT_POINTS self.maze.removeDot(pacman.getPosition()) def isAdjacent(point1, point2): return (point1[0] - point2[0])**2 + (point1[1] - point2[0])**2 <= 1
import pygame import Enums from Pacman import Pacman from Map import Map from Move import Move import time pygame.init() pygame.font.init() frames = 30 (screenWidth, screenHeight) = ((560, 720)) pacman = Pacman() map = Map("data\\map.txt") map.getMap() screen = pygame.display.set_mode((screenWidth, screenHeight)) pygame.display.set_caption('Pacman') clock = pygame.time.Clock() running = True pacmanMove = Move.initWithDirection(Enums.Direction.Left) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: pacmanMove = Move.initWithDirection(Enums.Direction.Up) elif pressed[pygame.K_DOWN]: pacmanMove = Move.initWithDirection(Enums.Direction.Down) elif pressed[pygame.K_LEFT]: pacmanMove = Move.initWithDirection(Enums.Direction.Left) elif pressed[pygame.K_RIGHT]: pacmanMove = Move.initWithDirection(Enums.Direction.Right)
def test_TurnLeftEast(self): pac = Pacman() self.turnLeftTest("3,3,NORTH", pac, pac.EAST)
def test_TurnLeftNorth(self): pac = Pacman() self.turnLeftTest("3,3,WEST", pac, pac.NORTH)
# Initialize colours BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Initialize window window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) window.fill(BLACK) # Pixels per loop MOVESPEED = 16 # Initialize Pacman pacman = Pacman(224, 384, MOVESPEED) # 16 * 14, 16 * 24 pacman_group = pygame.sprite.GroupSingle(pacman) # Initialize Ghost ghost = Ghost(224, 256, MOVESPEED) ghost_group = pygame.sprite.GroupSingle(ghost) # Initialize movement variable movement = 'R' # Draw Pacman onto the window pacman_group.draw(window) # Update display pygame.display.update()
def test_TurnRightEast(self): pac = Pacman() self.turnRightTest("3,3,SOUTH", pac, pac.EAST)
def test_Place(self): expected = "3,3,NORTH" pac = Pacman() pac.Place(3, 3, pac.NORTH) self.assertEqual(pac.Report(), expected)
def test_TurnLeftWest(self): pac = Pacman() self.turnLeftTest("3,3,SOUTH", pac, pac.WEST)
def main(): b = Beallitasok() jAblak = b.kezdo() pont = 0 clock = pygame.time.Clock() TOP10 = False keepGoing_game = False animacio(jAblak, b) while True: jAblak.fill(b.BLACK) if TOP10: fajl_ki(pacman.pont, jAblak, b) nev_ki(jAblak, b) keepGoing_game = fomenu(jAblak, b) while keepGoing_game: szellemek = [Blinky(b), Inky(b), Pinky(b), Clyde(b)] pacman = Pacman(b) pacman.pont = pont palya = Palya().palya_be(b) csomopontok = Palya().csomopont_be(b) kiskaja = Kaja().kiskaja_be() nagykaja = Kaja().nagykaja_be() kaja = Kaja() keepGoing_round = True while keepGoing_round: clock.tick(b.FPS) keepGoing_game = keepGoing_round = TOP10 = pacman.iranyit() pacman.mozog(palya) pacman.billentyu = False pacman.teleport(b) pacman.melyik() jAblak.fill(b.BLACK) kiskaja = kaja.kkaja_eves(kiskaja, pacman) nagykaja = kaja.nkaja_eves(nagykaja, pacman, szellemek) kaja.kaja_ki(kiskaja, nagykaja, jAblak) for sz in szellemek: sz.mozog(palya, csomopontok, pacman, b) sz.teleport(b) jAblak.blit(sz.felulet, sz.rect) if sz.eheto: sz.eheto_e() if sz.nev == "Blinky": sz.cruise_elroy(kiskaja) Palya().palya_ki(palya, jAblak, b) jAblak.blit(pacman.pont_kiir(b), (b.EGYSEG, 0)) pacman.elet_kiir(b, jAblak) bal_also("ESC - Kilépés a főmenübe", b.YELLOW, jAblak, b) jAblak.blit(pacman.felulet, pacman.rect) pygame.display.update() for sz in szellemek: if pacman.rect.colliderect(sz.rect): if sz.eheto: szellemek.pop(szellemek.index(sz)) pacman.pont += 50 if sz.nev == "Blinky": szellemek.append(Blinky(b)) elif sz.nev == "Pinky": szellemek.append(Pinky(b)) elif sz.nev == "Inky": szellemek.append(Inky(b)) elif sz.nev == "Clyde": szellemek.append(Clyde(b)) else: pacman.elet -= 1 pacman.uj(b) szellemek = [ Inky(b), Blinky(b), Pinky(b), Clyde(b) ] Inky.db = 1 if pacman.elet == 0: keepGoing_game = keepGoing_round = False TOP10 = True break pont = pacman.pont if len(kiskaja) == 0 and len(nagykaja) == 0: keepGoing_round = False
def test_TurnRightWest(self): pac = Pacman() self.turnRightTest("3,3,NORTH", pac, pac.WEST)
def test_TurnRightSouth(self): pac = Pacman() self.turnRightTest("3,3,WEST", pac, pac.SOUTH)
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() screen = pygame.display.set_mode((Settings.screen_width, Settings.screen_height)) pygame.display.set_caption("Catch Me If You CAN") # 绘制屏幕 wall = loadFigure(Settings.FIG_WALL) path = loadFigure(Settings.FIG_PATH) # 绘制底色 重绘的时候会覆盖所有的画面 screen.fill(Settings.BG_COLOR) # 绘制迷宫地图 for i in range(Settings.UNIT_HEIGHT): for j in range(Settings.UNIT_WIDTH): if (Map.get((i, j)) == 1): screen.blit(wall, (j * Settings.UNIT_LENGTH, i * Settings.UNIT_LENGTH)) elif (Map.get((i, j)) == 0): screen.blit(path, (j * Settings.UNIT_LENGTH, i * Settings.UNIT_LENGTH)) # 绘制gHost ghost_1 = Ghost(10, 11, 'left') screen.blit(ghost_1.getFigure("./figures/red_right.png"), (11 * Settings.UNIT_LENGTH, 10 * Settings.UNIT_LENGTH)) ghost_2 = Ghost(12, 13, 'left') screen.blit(ghost_1.getFigure("./figures/blue_right.png"), (13 * Settings.UNIT_LENGTH, 12 * Settings.UNIT_LENGTH)) # 绘制pac-man pacman_1 = Pacman(11, 1, 'right') screen.blit(pacman_1.getFigure("./figures/Pacman_left.png"), (1 * Settings.UNIT_LENGTH, 11 * Settings.UNIT_LENGTH)) # 让最近绘制的屏幕可见 pygame.display.flip() loadMusic("./music/Devil Trigger.mp3") count_dots = 300 # the main loop while True: # time interval among steps time.sleep(Settings.UNIT_INTERVAL) # 监视键盘和鼠标事件 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() else: break # current state state = {'ghost_locations': [ghost_1.getLoc(), ghost_2.getLoc()], 'pacman_location': pacman_1.getLoc()} # one move for pac-man&ghost # pac-man loc_last = pacman_1.getLoc() figure, loc_next = pacman_1.takeAction(state, Settings.ICONMAP_PACMAN) drawMoving(screen, figure, loc_last, loc_next, True) # eat a dot if Map.get(loc_next) == Settings.MAP_DOTS: Map.eatDots(loc_next) count_dots -= 1 # gHost loc_last = ghost_1.getLoc() figure, loc_next = ghost_1.takeAction(state, Settings.ICONMAP_GHOST_1) drawMoving(screen, figure, loc_last, loc_next) loc_last = ghost_2.getLoc() figure, loc_next = ghost_2.takeAction(state, Settings.ICONMAP_GHOST_2) drawMoving(screen, figure, loc_last, loc_next) # 让最近绘制的屏幕可见 pygame.display.flip() # 判定胜利条件 if ghost_1.getLoc() == pacman_1.getLoc() or ghost_2.getLoc() == pacman_1.getLoc(): break if count_dots <= 0: break # 监视键盘和鼠标事件 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() else: break
def test_MoveWest(self): pac = Pacman() self.moveTest("2,3,WEST", pac, pac.WEST)
# initialize window windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) windowSurface.fill(BLACK) # initialize movement variables moveLeft = False moveRight = False moveDown = False moveUp = False # pixels per loop MOVESPEED = 6 # create pacman and set coordinates to the center of the surface pacman = Pacman(WINDOWWIDTH / 2, WINDOWHEIGHT / 2, MOVESPEED) pacman_group = pygame.sprite.GroupSingle(pacman) # create pellets pellet_group = pygame.sprite.Group() pellet_group.add(Pellet(50, 50), Pellet(100, 100)) # blit pacman and pellets to the surface pacman_group.draw(windowSurface) pellet_group.draw(windowSurface) # update the game pygame.display.update() while True: for event in pygame.event.get():
def test_TurnRightNorth(self): pac = Pacman() self.turnRightTest("3,3,EAST", pac, pac.NORTH)
cropped_image = background.subsurface(selected_area) # If the cropped image's color is BLACK if pygame.transform.average_color(cropped_image)[:3] == BLACK: grid_member = Box(x, y, GREEN) grid_member.check_possible_moves(x, y) grid_group.add(grid_member) else: box_group.add(Box(x, y, RED)) x += 16 y += 16 x = 0 # Initialize Pacman pacman = Pacman(224, 384, MOVESPEED, box_group) # 16 * 14, 16 * 24 pacman_group = pygame.sprite.GroupSingle(pacman) # Initialize movement variable movement = 'R' last_movement = 'R' # Draw Pacman onto the window pacman_group.draw(window) # Update display pygame.display.update() def update_window(): """Updates the window by redrawing the background and sprites"""
class Game: def __init__(self): self.pacman = Pacman() blinky = Ghost("blinky", self.pacman, None) self.ghosts = [blinky, Ghost("pinky", self.pacman, None), Ghost("clyde", self.pacman, None), Ghost("inky", self.pacman, blinky)] self.dots = [] self.allDotsView = pygame.Surface((GameConfig.windowW, GameConfig.windowH)) self.allDotsView.set_colorkey((0, 0, 0)) self.dotsView = [] self.initDots() self.gameOver = False self.life = 2 self.level = 0 self.score = 0 self.date = time() self.stateDate = time() self.mode = 2 self.win = False self.loose = False def nextState(self): self.pacman.move() self.checkEatDot() self.moveGhost() if self.mode == 1 and (time() - self.stateDate) > 60.0: print("scatterMode") self.mode = 2 for ghost in self.ghosts: if ghost.state != 3: ghost.scatterMode() self.stateDate = time() elif self.mode == 2 and (time() - self.stateDate) > 10.0: print("chasseMode") self.mode = 1 for ghost in self.ghosts: if ghost.state != 3: ghost.chaseMode() self.stateDate = time() else: for ghost in self.ghosts: if ghost.state == 3 and (time() - self.date) > 15: ghost.state = 1 def initDots(self): y = 35 while y < 630: x = 35 i = 0 if y != 35: i = round((y - 35) / 21) self.dots.append([]) self.dotsView.append(pygame.Surface((GameConfig.mapSizeW, 21))) self.dotsView[i].set_colorkey((0, 0, 0)) while x < 580: if GameConfig.imgDots.get_at((x, y)) == (255, 255, 255, 255): if (x == 35 and y == 77) or (x == 560 and y == 77) or (x == 35 and y == 497) or ( x == 560 and y == 497): self.dots[i].append(Dot(x, y, 1)) else: self.dots[i].append(Dot(x, y, 0)) x += 21 self.drawDots(i) y += 21 self.drawAllDots() def drawDots(self, i): self.dotsView[i].fill((0, 0, 0)) for dot in self.dots[i]: if dot.value == 0: img = GameConfig.imgDot self.dotsView[i].blit(img, (dot.x, 0)) else: img = GameConfig.imgBigDot self.dotsView[i].blit(img, (dot.x - 8, - 8)) def drawAllDots(self): i = 0 self.allDotsView.fill((0, 0, 0)) for dotView in self.dotsView: self.allDotsView.blit(dotView, (GameConfig.mapStartW, GameConfig.mapStartH + 35 + i * 21)) i += 1 def drawStart(self, window): self.win = False self.draw(window) font = pygame.font.Font('resources/arcadeClassic.ttf', 40) img = font.render("READY!", True, GameConfig.yellow) displayRect = img.get_rect() displayRect.center = (405, 440) window.blit(img, displayRect) def drawGameOver(self, window): window.fill(GameConfig.background) window.blit(GameConfig.imgMap, (GameConfig.mapStartW, GameConfig.mapStartH)) window.blit(self.allDotsView, (0, 0)) font = pygame.font.Font('resources/arcadeClassic.ttf', 20) img = font.render(str(self.score), True, GameConfig.white) displayRect = img.get_rect() displayRect.center = (GameConfig.mapStartW + 50, GameConfig.mapStartH - 25) window.blit(img, displayRect) self.draw(window) font = pygame.font.Font('resources/arcadeClassic.ttf', 35) img = font.render("GAME OVER", True, GameConfig.red) displayRect = img.get_rect() displayRect.center = (400, 440) window.blit(img, displayRect) def draw(self, window): window.fill(GameConfig.background) window.blit(GameConfig.imgMap, (GameConfig.mapStartW, GameConfig.mapStartH)) window.blit(self.allDotsView, (0, 0)) # Affichage Pac Man every: int = 270000 # Temps entre chaque fois que pacMan ferme la bouche en micro secondes duree = 200000 # durée pendant laquelle pacMan ferme sa bouche en micro secondes millis = datetime.now().microsecond currentPacManImage = GameConfig.imgPacMan if self.pacman.possibleMove(self.pacman.direction) and millis % every >= every - 1 * duree / 5: currentPacManImage = GameConfig.imgPacMan_state_2 elif self.pacman.possibleMove( self.pacman.direction) and millis % every >= every - 2 * duree / 5: currentPacManImage = GameConfig.imgPacMan_state_3 elif self.pacman.possibleMove( self.pacman.direction) and millis % every >= every - 3 * duree / 5: # bouche fermée currentPacManImage = GameConfig.imgPacManClose elif self.pacman.possibleMove(self.pacman.direction) and millis % every >= every - 4 * duree / 5: currentPacManImage = GameConfig.imgPacMan_state_3 elif self.pacman.possibleMove(self.pacman.direction) and millis % every >= every - 5 * duree / 5: currentPacManImage = GameConfig.imgPacMan_state_2 window.blit(pygame.transform.rotate(currentPacManImage, int(self.pacman.direction * 90)), (GameConfig.mapStartW + self.pacman.x - round(GameConfig.pacManW / 2), GameConfig.mapStartH + self.pacman.y - round(GameConfig.pacManH / 2))) # Affichage fantomes every: int = 200000 # Temps entre chaque fois que pacMan ferme la bouche en micro secondes duree = 100000 # durée pendant laquelle pacMan ferme sa bouche en micro secondes millis = datetime.now().microsecond for ghost in self.ghosts: currentGhostImg = getGhostImg(ghost.name, ghost.direction, False, ghost.state) if millis % every >= every - duree: currentGhostImg = getGhostImg(ghost.name, ghost.direction, True, ghost.state) window.blit(currentGhostImg, (GameConfig.mapStartW + ghost.x - round(GameConfig.ghostW / 2), GameConfig.mapStartH + ghost.y - round(GameConfig.ghostH / 2))) # Affichage vie for i in range(0, self.life): window.blit(pygame.transform.rotate(GameConfig.imgPacMan, 90), (GameConfig.mapStartW + 50 + i * (GameConfig.pacManW + 10), GameConfig.mapStartH + GameConfig.mapSizeH + 20)) font = pygame.font.Font('resources/arcadeClassic.ttf', 20) img = font.render(str(self.score), True, GameConfig.white) displayRect = img.get_rect() displayRect.center = (GameConfig.mapStartW + 50, GameConfig.mapStartH - 25) window.blit(img, displayRect) def checkEatDot(self): for dots in self.dots: for dot in dots: if self.pacman.x - 15 < dot.x < self.pacman.x + 15 and self.pacman.y - 15 < dot.y < self.pacman.y + 15: dots.remove(dot) self.drawDots(round((dot.y - 35) / 21)) self.drawAllDots() if dot.value == 1: self.initDate() for ghost in self.ghosts: ghost.scraredMode() else: self.score += 10 if self.checkNextLevel(): self.pacman.initialPosition() self.pacman.setDirection(0) for ghost in self.ghosts: ghost.setup() self.level += 1 self.initDots() self.win = True break def checkNextLevel(self): for dots in self.dots: if len(dots) != 0: return False return True def moveGhost(self): for ghost in self.ghosts: ghost.move() if ghost.x - round(GameConfig.ghostW / 2) < self.pacman.x < ghost.x + round(GameConfig.ghostW / 2) \ and ghost.y - round(GameConfig.ghostW / 2) < self.pacman.y < ghost.y + round(GameConfig.ghostW / 2): if ghost.state != 3 and ghost.state != 0: self.life -= 1 self.pacman.initialPosition() self.mode = 2 self.initDate() self.loose = True self.pacman.direction = 3 for ghost in self.ghosts: ghost.setup() if self.life < 0: self.gameOver = True else: sleep(1) else: ghost.state = 0 ghost.eat() def initDate(self): self.date = time()
def INIT_pacman(fenetre, canvas): pacman = Pacman(canvas) for k in ["<Down>", "<Left>", "<Right>", "<Up>"]: fenetre.bind(k, pacman.PERDEP_ChgNewDir) return pacman
def test_TurnLeftSouth(self): pac = Pacman() self.turnLeftTest("3,3,EAST", pac, pac.SOUTH)
def createPacmanFromCommand(command): inputs = transferCommand(command) # when command invalid, default Parman will be created to prevent further error return Pacman(int(inputs[0]), int(inputs[1]), inputs[2]) if inputs else Pacman(0, 0, NORTH)
import random, copy from Character import Character from Pacman import Pacman from Ghost import Ghost from Walls import Walls from Pellets import Pellets from Sound import Sound # Create game objects background = pygame.image.load ("bg.png").convert () pacman = Pacman () ghosts = [Ghost ()] walls = Walls.createList (Walls ()) pellets_small = Pellets.createListSmall (Pellets ()) pellets_large = Pellets.createListLarge (Pellets ()) clock = pygame.time.Clock () pygame.mixer.music.load ("bg_music.mp3") pygame.mixer.music.set_volume (0.5) # Opening screen and music Sound.channel.play (Sound.opening) wSurface.fill ((0, 0, 0)) wSurface.blit (background, (100, 0)) wSurface.blit (pacman.getScoreSurface (), (10, 10)) wSurface.blit (pacman.getLivesSurface (), (WINDOWSIZE [0] - 200, 10))