您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Python代碼,能玩30多款童年游戲有哪些,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
大游戲小游戲有千千萬萬,這些小游戲應該只有90后才玩過和懂吧
兒童節即將到來,雖然禿頭程序員沒有頭發,但是童心還是一直都在的,今天就分享一些私藏的童年游戲,十幾行代碼就能進入使用Python開發的小游戲快樂玩耍!
使用工具:
Python3.8、pycharm2020
也可以不安裝pycharm2020也可以直接運行~但是安裝了些會好一點
源碼:
'''游戲開始界面''' class gameStartUI(QWidget): def __init__(self, parent=None, **kwargs): super(gameStartUI, self).__init__(parent) self.setFixedSize(760, 650) self.setWindowTitle('五子棋 —— 九歌') self.setWindowIcon(QIcon(cfg.ICON_FILEPATH)) # 背景圖片 palette = QPalette() palette.setBrush(self.backgroundRole(), QBrush(QPixmap(cfg.BACKGROUND_IMAGEPATHS.get('bg_start')))) self.setPalette(palette) # 按鈕 # --人機對戰 self.ai_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('ai'), self) self.ai_button.move(250, 200) self.ai_button.show() self.ai_button.click_signal.connect(self.playWithAI) # --聯機對戰 self.online_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('online'), self) self.online_button.move(250, 350) self.online_button.show() self.online_button.click_signal.connect(self.playOnline) '''人機對戰''' def playWithAI(self): self.close() self.gaming_ui = playWithAIUI(cfg) self.gaming_ui.exit_signal.connect(lambda: sys.exit()) self.gaming_ui.back_signal.connect(self.show) self.gaming_ui.show() '''聯機對戰''' def playOnline(self): self.close() self.gaming_ui = playOnlineUI(cfg, self) self.gaming_ui.show() '''run''' if __name__ == '__main__': app = QApplication(sys.argv) handle = gameStartUI() font = QFont() font.setPointSize(12) handle.setFont(font) handle.show() sys.exit(app.exec_())
else: break Interface(screen, cfg, mode='game_end') me.display.update() clock.tick(cfg.FPS) if is_win_flag:
源碼:
'''游戲主程序''' def main(cfg): # 初始化 pygame.init() pygame.mixer.init() pygame.mixer.music.load(cfg.BGMPATH) pygame.mixer.music.play(-1, 0.0) screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption('Bomber Man —— 九歌') # 開始界面 Interface(screen, cfg, mode='game_start') # 游戲主循環 font = pygame.font.SysFont('Consolas', 15) for gamemap_path in cfg.GAMEMAPPATHS: # -地圖 map_parser = mapParser(gamemap_path, bg_paths=cfg.BACKGROUNDPATHS, wall_paths=cfg.WALLPATHS, blocksize=cfg.BLOCKSIZE) # -水果 fruit_sprite_group = pygame.sprite.Group() used_spaces = [] for i in range(5): coordinate = map_parser.randomGetSpace(used_spaces) used_spaces.append(coordinate) fruit_sprite_group.add(Fruit(random.choice(cfg.FRUITPATHS), coordinate=coordinate, blocksize=cfg.BLOCKSIZE)) # -我方Hero coordinate = map_parser.randomGetSpace(used_spaces) used_spaces.append(coordinate) ourhero = Hero(imagepaths=cfg.HEROZELDAPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='ZELDA') # -電腦Hero aihero_sprite_group = pygame.sprite.Group() coordinate = map_parser.randomGetSpace(used_spaces) aihero_sprite_group.add(Hero(imagepaths=cfg.HEROBATMANPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='BATMAN')) used_spaces.append(coordinate) coordinate = map_parser.randomGetSpace(used_spaces) aihero_sprite_group.add(Hero(imagepaths=cfg.HERODKPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='DK')) used_spaces.append(coordinate) # -炸彈bomb bomb_sprite_group = pygame.sprite.Group() # -用于判斷游戲勝利或者失敗的flag is_win_flag = False # -主循環 screen = pygame.display.set_mode(map_parser.screen_size) clock = pygame.time.Clock() while True: dt = clock.tick(cfg.FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(-1) # --↑↓←→鍵控制上下左右, 空格鍵丟炸彈 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: ourhero.move('up') elif event.key == pygame.K_DOWN: ourhero.move('down') elif event.key == pygame.K_LEFT: ourhero.move('left') elif event.key == pygame.K_RIGHT: ourhero.move('right') elif event.key == pygame.K_SPACE: if ourhero.bomb_cooling_count <= 0: bomb_sprite_group.add(ourhero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH)) screen.fill(cfg.WHITE) # --電腦Hero隨機行動 for hero in aihero_sprite_group: action, flag = hero.randomAction(dt) if flag and action == 'dropbomb': bomb_sprite_group.add(hero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH)) # --吃到水果加生命值(只要是Hero, 都能加) ourhero.eatFruit(fruit_sprite_group) for hero in aihero_sprite_group: hero.eatFruit(fruit_sprite_group) # --游戲元素都綁定到屏幕上 map_parser.draw(screen) for bomb in bomb_sprite_group: if not bomb.is_being: bomb_sprite_group.remove(bomb) explode_area = bomb.draw(screen, dt, map_parser) if explode_area: # --爆炸火焰范圍內的Hero生命值將持續下降 if ourhero.coordinate in explode_area: ourhero.health_value -= bomb.harm_value for hero in aihero_sprite_group: if hero.coordinate in explode_area: hero.health_value -= bomb.harm_value fruit_sprite_group.draw(screen) for hero in aihero_sprite_group: hero.draw(screen, dt) ourhero.draw(screen, dt) # --左上角顯示生命值 pos_x = showText(screen, font, text=ourhero.hero_name+'(our):'+str(ourhero.health_value), color=cfg.YELLOW, position=[5, 5]) for hero in aihero_sprite_group: pos_x, pos_y = pos_x+15, 5 pos_x = showText(screen, font, text=hero.hero_name+'(ai):'+str(hero.health_value), color=cfg.YELLOW, position=[pos_x, pos_y]) # --我方玩家生命值小于等于0/電腦方玩家生命值均小于等于0則判斷游戲結束 if ourhero.health_value <= 0: is_win_flag = False break for hero in aihero_sprite_group: if hero.health_value <= 0: aihero_sprite_group.remove(hero) if len(aihero_sprite_group) == 0: is_win_flag = True break pygame.display.update() clock.tick(cfg.FPS) if is_win_flag: Interface(screen, cfg, mode='game_switch') else: break Interface(screen, cfg, mode='game_end') '''run''' if __name__ == '__main__': while True: main(cfg)
因為篇幅原因,下面的游戲都不貼源碼了,需要的同學關注小編,并私信“01”獲取下載
上學時恨不得一直待在電腦房玩這個游戲,哈哈哈
上述就是小編為大家分享的Python代碼,能玩30多款童年游戲有哪些了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。