赞
踩
Python为什么能这么火热,Python相对于其他语言来说比较简单,即使是零基础的普通人也能很快的掌握,在其他方面比如,处于灰色界的爬虫,要VIP的视频,小说,歌,没有爬虫解决不了的;数据挖掘及分析,淘宝就是例子,想开个淘宝店,需要获取相关商品信息,这时数据分析就能解决等等;游戏开发、自动化测试、人工智能,黑客入门语言等等等。
接着介绍下去今天就停不了啦,只要对于Python有过了解的,用过的,应该都知道他的厉害之处, 进入主题,游戏,今天直接上干货,童年你玩过的21款游戏源码分享。
展示:
'''游戏开始界面''' 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_())
展示:
import cfg import pygame from modules import * '''主函数''' def main(): pygame.init() pygame.mixer.init() pygame.mixer.music.load(cfg.AUDIOPATHS['bgm']) pygame.mixer.music.play(-1, 0.0) pygame.mixer.music.set_volume(0.25) screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption("塔防游戏 —— 九歌") **调用游戏开始界面** start_interface = StartInterface(cfg) is_play = start_interface.update(screen) if not is_play: return **调用游戏界面** while True: choice_interface = ChoiceInterface(cfg) map_choice, difficulty_choice = choice_interface.update(screen) game_interface = GamingInterface(cfg) game_interface.start(screen, map_path=cfg.MAPPATHS[str(map_choice)], difficulty_path=cfg.DIFFICULTYPATHS[str(difficulty_choice)]) end_interface = EndInterface(cfg) end_interface.update(screen) '''run''' if __name__ == '__main__': main()
展示:
import cfg import sys import pygame from modules import * '''主函数''' def main(cfg): **初始化** pygame.init() pygame.mixer.init() pygame.font.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('Maze —— 九歌') font = pygame.font.SysFont('Consolas', 15) 开始界面 Interface(screen, cfg, 'game_start') 记录关卡数 num_levels = 0 记录最少用了多少步通关 best_scores = 'None' 关卡循环切换 while True: num_levels += 1 clock = pygame.time.Clock() screen = pygame.display.set_mode(cfg.SCREENSIZE) --随机生成关卡地图 maze_now = RandomMaze(cfg.MAZESIZE, cfg.BLOCKSIZE, cfg.BORDERSIZE) --生成hero hero_now = Hero(cfg.HEROPICPATH, [0, 0], cfg.BLOCKSIZE, cfg.BORDERSIZE) --统计步数 num_steps = 0 --关卡内主循环 while True: dt = clock.tick(cfg.FPS) screen.fill((255, 255, 255)) is_move = False # ----↑↓←→控制hero 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: is_move = hero_now.move('up', maze_now) elif event.key == pygame.K_DOWN: is_move = hero_now.move('down', maze_now) elif event.key == pygame.K_LEFT: is_move = hero_now.move('left', maze_now) elif event.key == pygame.K_RIGHT: is_move = hero_now.move('right', maze_now) num_steps += int(is_move) hero_now.draw(screen) maze_now.draw(screen) ----显示一些信息 showText(screen, font, 'LEVELDONE: %d' % num_levels, (255, 0, 0), (10, 10)) showText(screen, font, 'BESTSCORE: %s' % best_scores, (255, 0, 0), (210, 10)) showText(screen, font, 'USEDSTEPS: %s' % num_steps, (255, 0, 0), (410, 10)) showText(screen, font, 'S: your starting point D: your destination', (255, 0, 0), (10, 600)) ----判断游戏是否胜利 if (hero_now.coordinate[0] == cfg.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == cfg.MAZESIZE[0] - 1): break pygame.display.update() --更新最优成绩 if best_scores == 'None': best_scores = num_steps else: if best_scores > num_steps: best_scores = num_steps --关卡切换 Interface(screen, cfg, mode='game_switch') '''run''' if __name__ == '__main__': main(cfg)
太多了,偷懒就不一一展示,这些游戏陪伴了我们每个不同成长阶段的快乐,需要资源的可以关注我点击领取资源。我是一名喜欢分享的程序媛,感谢观看。
如果你对Python感兴趣,想要学习python,这里给大家分享一份Python全套学习资料,都是我自己学习时整理的,希望可以帮到你,一起加油!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。