3个文件,不到400行代码打造经典吃豆人小游戏

您所在的位置:网站首页 日本背部矫正哪个牌子 3个文件,不到400行代码打造经典吃豆人小游戏

3个文件,不到400行代码打造经典吃豆人小游戏

2024-02-19 14:16| 来源: 网络整理| 查看: 265

打造吃豆人小游戏,一共需要这几个文件: Game.py Levels.py Sprites.py 以及一些配置文件与源文件。

废话不多说,直接上代码:

Game.py """ Function: 吃豆豆小游戏 """ import os import sys import pygame import Levels """定义一些必要的参数""" BLACK = (0, 0, 0) WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) YELLOW = (255, 255, 0) PURPLE = (255, 0, 255) SKYBLUE = (0, 191, 255) BGMPATH = os.path.join(os.getcwd(), "resources/sounds/bg.mp3") ICONPATH = os.path.join(os.getcwd(), "resources/images/icon.png") FONTPATH = os.path.join(os.getcwd(), "resources/font/ALGER.TTF") HEROPATH = os.path.join(os.getcwd(), "resources/images/pacman.png") BlinkyPATH = os.path.join(os.getcwd(), "resources/images/Blinky.png") ClydePATH = os.path.join(os.getcwd(), "resources/images/Clyde.png") InkyPATH = os.path.join(os.getcwd(), "resources/images/Inky.png") PinkyPATH = os.path.join(os.getcwd(), "resources/images/Pinky.png") """开始某一关游戏""" def startLevelGame(level, screen, font): clock = pygame.time.Clock() SCORE = 0 wall_sprites = level.setupWalls(SKYBLUE) gate_sprites = level.setupGate(WHITE) hero_sprites, ghost_sprites = level.setupPlayers( HEROPATH, [BlinkyPATH, ClydePATH, InkyPATH, PinkyPATH] ) food_sprites = level.setupFood(YELLOW, WHITE) is_clearance = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(-1) pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: for hero in hero_sprites: hero.changeSpeed([-1, 0]) hero.is_move = True elif event.key == pygame.K_RIGHT: for hero in hero_sprites: hero.changeSpeed([1, 0]) hero.is_move = True elif event.key == pygame.K_UP: for hero in hero_sprites: hero.changeSpeed([0, -1]) hero.is_move = True elif event.key == pygame.K_DOWN: for hero in hero_sprites: hero.changeSpeed([0, 1]) hero.is_move = True if event.type == pygame.KEYUP: if ( (event.key == pygame.K_LEFT) or (event.key == pygame.K_RIGHT) or (event.key == pygame.K_UP) or (event.key == pygame.K_DOWN) ): hero.is_move = False screen.fill(BLACK) for hero in hero_sprites: hero.update(wall_sprites, gate_sprites) hero_sprites.draw(screen) for hero in hero_sprites: food_eaten = pygame.sprite.spritecollide(hero, food_sprites, True) SCORE += len(food_eaten) wall_sprites.draw(screen) gate_sprites.draw(screen) food_sprites.draw(screen) for ghost in ghost_sprites: # 幽灵随机运动(效果不好且有BUG) """ res = ghost.update(wall_sprites, None) while not res: ghost.changeSpeed(ghost.randomDirection()) res = ghost.update(wall_sprites, None) """ # 指定幽灵运动路径 if ghost.tracks_loc[1]


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3