• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

二十种Python代码游戏源代码分享

python 搞代码 4年前 (2022-01-07) 45次浏览 已收录 0个评论

这篇文章主要介绍了二十种Python代码游戏源代码分享,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

学Python中,自我感觉学的还不错的亚子~想做点什么来练练手,然后我疯狂的找各种小游戏的教程源码什么的,于是我就疯狂的找呀找呀,就找到了一大堆,哈哈哈
毕竟我是从小就有一个游戏梦,现在就弥补一下自己小时候没有玩过瘾的游戏补上叭~

提示:爱学习哦,不要沉迷游戏,平时打发一下无聊时间最好啦

拿走源码的还请留言说一下好吗?不管是想学习的想转发的想干啥的,还请都点个赞说一下不,我也找的不容易呀

1、21点数字小游戏展示:

首先配置文件的源码:

 '''配置文件''' import os # 一些常量 RED = (255, 0, 0) BLACK = (0, 0, 0) AZURE = (240, 255, 255) WHITE = (255, 255, 255) MISTYROSE = (255, 228, 225) PALETURQUOISE = (175, 238, 238) PAPAYAWHIP = (255, 239, 213) CURRENTPATH = os.getcwd() FONTPATH = os.path.join(CURRENTPATH, 'resources/fonts/font.TTF') AUDIOWINPATH = os.path.join(CURRENTPATH, 'resources/audios/win.wav') AUDIOLOSEPATH = os.path.join(CURRENTPATH, 'resources/audios/lose.wav') AUDIOWARNPATH = os.path.join(CURRENTPATH, 'resources/audios/warn.wav') BGMPATH = os.path.join(CURRENTPATH, 'resources/audios/bgm.mp3') # 数字卡片 # --数字卡片字体颜色 NUMBERFONT_COLORS = [BLACK, RED] # --数字卡片背景颜色 NUMBERCARD_COLORS = [MISTYROSE, PALETURQUOISE] # --数字卡片字体路径与大小 NUMBERFONT = [FONTPATH, 50] # --数字卡片位置 NUMBERCARD_POSITIONS = [(25, 50, 150, 200), (225, 50, 150, 200), (425, 50, 150, 200), (625, 50, 150, 200)] # 运算符卡片 # --运算符种类 OPREATORS = ['+', '-', '×', '÷'] # --运算符卡片字体颜色 OPREATORFONT_COLORS = [BLACK, RED] # --运算符卡片背景颜色 OPERATORCARD_COLORS = [MISTYROSE, PALETURQUOISE] # --运算符卡片字体路径与大小 OPERATORFONT = [FONTPATH, 30] # --运算符卡片位置 OPERATORCARD_POSITIONS = [(230, 300, 50, 50), (330, 300, 50, 50), (430, 300, 50, 50), (530, 300, 50, 50)] # 按钮卡片 # --按钮类型 BUTTONS = ['RESET', 'ANSWERS', 'NEXT'] # --按钮卡片字体颜色 BUTTONFONT_COLORS = [BLACK, BLACK] # --按钮卡片背景颜色 BUTTONCARD_COLORS = [MISTYROSE, PALETURQUOISE] # --按钮卡片字体路径与大小 BUTTONFONT = [FONTPATH, 30] # --按钮卡片位置 BUTTONCARD_POSITIONS = [(25, 400, 700/3, 150), (50+700/3, 400, 700/3, 150), (75+1400/3, 400, 700/3, 150)] # 屏幕大小 SCREENSIZE = (800, 600) # 卡片类型 GROUPTYPES = ['NUMBER', 'OPREATOR', 'BUTTON']

游戏源码:

 import os import sys import pygame from cfg import * from modules import * from fractions import Fraction '''检查控件是否被点击''' def checkClicked(group, mouse_pos, group_type='NUMBER'): selected = [] # 数字卡片/运算符卡片 if group_type == GROUPTYPES[0] or group_type == GROUPTYPES[1]: max_selected = 2 if group_type == GROUPTYPES[0] else 1 num_selected = 0 for each in group: num_selected += int(each.is_selected) for each in group: if each.rect.collidepoint(mouse_pos): if each.is_selected: each.is_selected = not each.is_selected num_selected -= 1 each.select_order = None else: if num_selected <max_selected: each.is_selected num_selected + each.select_order=str(num_selected) if each.is_selected: selected.append(each.attribute) # 按钮卡片 elif group_type== grouptypes[2]: for each in group: each.rect.collidepoint(mouse_pos): true 抛出异常 else: raise valueerror('checkclicked.group_type unsupport %s, expect %s or %s...' % (group_type, *grouptypes)) return selected '''获取数字精灵组''' def getnumberspritesgroup(numbers): number_sprites_group idx, number enumerate(numbers): args=(*NUMBERCARD_POSITIONS[idx], str(number), numberfont, numberfont_colors, numbercard_colors, str(number)) number_sprites_group.add(card(*args)) '''获取运算符精灵组''' getoperatorspritesgroup(operators): operator_sprites_group operator enumerate(operators): (*operatorcard_positions[idx], str(operator), operatorfont, opreatorfont_colors, operatorcard_colors, str(operator)) operator_sprites_group.add(card(*args)) '''获取按钮精灵组''' getbuttonspritesgroup(buttons): button_sprites_group button enumerate(buttons): (*buttoncard_positions[idx], str(button), buttonfont, buttonfont_colors, buttoncard_colors, str(button)) button_sprites_group.add(button(*args)) '''计算''' calculate(number1, number2, operator): operator_map={'+': '+', '-': '-', '×': '*', '÷': ' '} try: result '.' not else str(fraction(number1+operator_map[operator]+number2)) except: none '''在屏幕上显示信息''' showinfo(text, screen): rect=pygame.Rect(200, 180, 400, 200) pygame.draw.rect(screen, papayawhip, rect) font=pygame.font.Font(FONTPATH, 40) text_render=font.render(text, true, black) font_size=font.size(text) screen.blit(text_render, (rect.x+(rect.width-font_size[0]) 2, rect.y+(rect.height-font_size[1]) 2)) '''主函数''' main(): 初始化, 导入必要的游戏素材 pygame.init() pygame.mixer.init() screen=pygame.display.set_mode(SCREENSIZE) pygame.display.set_caption('24 point ―― 九歌') win_sound=pygame.mixer.Sound(AUDIOWINPATH) lose_sound=pygame.mixer.Sound(AUDIOLOSEPATH) warn_sound=pygame.mixer.Sound(AUDIOWARNPATH) pygame.mixer.music.load(bgmpath) pygame.mixer.music.play(-1, 0.0) 24点游戏生成器 game24_gen=game24Generator() game24_gen.generate() 精灵组 --数字 getnumberspritesgroup(game24_gen.numbers_now) --运算符 getoperatorspritesgroup(opreators) --按钮 getbuttonspritesgroup(buttons) 游戏主循环 clock=pygame.time.Clock() selected_numbers=[] selected_operators=[] selected_buttons is_win while true: event pygame.event.get(): event.type== pygame.quit: pygame.quit() sys.exit(-1) pygame.mousebuttonup: mouse_pos=pygame.mouse.get_pos() checkclicked(number_sprites_group, mouse_pos, 'number') checkclicked(operator_sprites_group, 'opreator') checkclicked(button_sprites_group, 'button') screen.fill(azure) 更新数字 len(selected_numbers)== 2 and len(selected_operators)== 1: noselected_numbers=[] number_sprites_group: '1': selected_number1=each.attribute '2': selected_number2=each.attribute valueerror('unknow select_order 1 2...' each.select_order) noselected_numbers.append(each.attribute) false operator_sprites_group: calculate(selected_number1, selected_number2, *selected_operators) is none: game24_gen.numbers_now=noselected_numbers [result] game24_gen.check() is_win: win_sound.play() len(game24_gen.numbers_now)== lose_sound.play() warn_sound.play() [] 精灵都画到screen上 each.draw(screen, pygame.mouse.get_pos()) button_sprites_group: selected_buttons[0] ['reset', 'next']: each.attribute== selected_buttons[0]: each.do(game24_gen, getnumberspritesgroup, number_sprites_group, button_sprites_group) 游戏胜利 showinfo('congratulations', screen) 游戏失败 showinfo('game over', pygame.display.flip() clock.tick(30) '''run''' __name__== '__main__': main()<pre></div><h3>2、保卫森林大作战啦啦</h3><p>展示:</p><p style="text-align: center"></p><p><strong>首先配置文件的源码:</strong></p><div class="gaodaimacode"><pre class="prettyprint linenums"> '''配置文件''' import os '''屏幕大小''' SCREENSIZE = (800, 600) '''图片路径''' IMAGEPATHS = { 'choice': { 'load_game': os.path.join(os.getcwd(), 'resources/images/choice/load_game.png-600'), 'map1': os.path.join(os.getcwd(), 'resources/images/choice/map1.png-600'), 'map1_black': os.path.join(os.getcwd(), 'resources/images/choice/map1_black.png-600'), 'map1_red': os.path.join(os.getcwd(), 'resources/images/choice/map1_red.png-600'), 'map2': os.path.join(os.getcwd(), 'resources/images/choice/map2.png-600'), 'map2_black': os.path.join(os.getcwd(), 'resources/images/choice/map2_black.png-600'), 'map2_red': os.path.join(os.getcwd(), 'resources/images/choice/map2_red.png-600'), 'map3': os.path.join(os.getcwd(), 'resources/images/choice/map3.png-600'), 'map3_black': os.path.join(os.getcwd(), 'resources/images/choice/map3_black.png-600'), 'map3_red': os.path.join(os.getcwd(), 'resources/images/choice/map3_red.png-600'), }, 'end': { 'gameover': os.path.join(os.getcwd(), 'resources/images/end/gameover.png-600'), 'continue_red': os.path.join(os.getcwd(), 'resources/images/end/continue_red.png-600'), 'continue_black': os.path.join(os.getcwd(), 'resources/images/end/continue_black.png-600'), }, 'game': { 'arrow1': os.path.join(os.getcwd(), 'resources/images/game/arrow1.png-600'), 'arrow2': os.path.join(os.getcwd(), 'resources/images/game/arrow2.png-600'), 'arrow3': os.path.join(os.getcwd(), 'resources/images/game/arrow3.png-600'), 'basic_tower': os.path.join(os.getcwd(), 'resources/images/game/basic_tower.png-600'), 'boulder': os.path.join(os.getcwd(), 'resources/images/game/boulder.png-600'), 'bush': os.path.join(os.getcwd(), 'resources/images/game/bush.png-600'), 'cave': os.path.join(os.getcwd(), 'resources/images/game/cave.png-600'), 'dirt': os.path.join(os.getcwd(), 'resources/images/game/dirt.png-600'), 'enemy_blue': os.path.join(os.getcwd(), 'resources/images/game/enemy_blue.png-600'), 'enemy_pink': os.path.join(os.getcwd(), 'resources/images/game/enemy_pink.png-600'), 'enemy_red': os.path.join(os.getcwd(), 'resources/images/game/enemy_red.png-600'), 'enemy_yellow': os.path.join(os.getcwd(), 'resources/images/game/enemy_yellow.png-600'), 'godark': os.path.join(os.getcwd(), 'resources/images/game/godark.png-600'), 'golight': os.path.join(os.getcwd(), 'resources/images/game/golight.png-600'), 'grass': os.path.join(os.getcwd(), 'resources/images/game/grass.png-600'), 'healthfont': os.path.join(os.getcwd(), 'resources/images/game/healthfont.png-600'), 'heavy_tower': os.path.join(os.getcwd(), 'resources/images/game/heavy_tower.png-600'), 'med_tower': os.path.join(os.getcwd(), 'resources/images/game/med_tower.png-600'), 'nexus': os.path.join(os.getcwd(), 'resources/images/game/nexus.png-600'), 'othergrass': os.path.join(os.getcwd(), 'resources/images/game/othergrass.png-600'), 'path': os.path.join(os.getcwd(), 'resources/images/game/path.png-600'), 'rock': os.path.join(os.getcwd(), 'resources/images/game/rock.png-600'), 'tiles': os.path.join(os.getcwd(), 'resources/images/game/tiles.png-600'), 'unitfont': os.path.join(os.getcwd(), 'resources/images/game/unitfont.png-600'), 'water': os.path.join(os.getcwd(), 'resources/images/game/water.png-600'), 'x': os.path.join(os.getcwd(), 'resources/images/game/x.png-600'), }, 'pause': { 'gamepaused': os.path.joi<mark style="color:transparent">来源gaodaimacom搞#代%码网</mark>n(os.getcwd(), 'resources/images/pause/gamepaused.png-600'), 'resume_black': os.path.join(os.getcwd(), 'resources/images/pause/resume_black.png-600'), 'resume_red': os.path.join(os.getcwd(), 'resources/images/pause/resume_red.png-600'), }, 'start': { 'play_black': os.path.join(os.getcwd(), 'resources/images/start/play_black.png-600'), 'play_red': os.path.join(os.getcwd(), 'resources/images/start/play_red.png-600'), 'quit_black': os.path.join(os.getcwd(), 'resources/images/start/quit_black.png-600'), 'quit_red': os.path.join(os.getcwd(), 'resources/images/start/quit_red.png-600'), 'start_interface': os.path.join(os.getcwd(), 'resources/images/start/start_interface.png-600'), }, } '''地图路径''' MAPPATHS = { '1': os.path.join(os.getcwd(), 'resources/maps/1.map'), '2': os.path.join(os.getcwd(), 'resources/maps/2.map'), '3': os.path.join(os.getcwd(), 'resources/maps/3.map'), } '''字体路径''' FONTPATHS = { 'Calibri': os.path.join(os.getcwd(), 'resources/fonts/Calibri.ttf'), 'm04': os.path.join(os.getcwd(), 'resources/fonts/m04.ttf'), 'Microsoft Sans Serif': os.path.join(os.getcwd(), 'resources/fonts/Microsoft Sans Serif.ttf'), } '''不同难度的settings''' DIFFICULTYPATHS = { 'easy': os.path.join(os.getcwd(), 'resources/difficulties/easy.json'), 'hard': os.path.join(os.getcwd(), 'resources/difficulties/hard.json'), 'medium': os.path.join(os.getcwd(), 'resources/difficulties/medium.json'), }

游戏源码:

 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()

3、超级大的迷宫

展示:

首先配置文件的源码:

 '''配置文件''' import os '''屏幕大小''' SCREENSIZE = (800, 625) '''游戏素材''' BGMPATH = os.path.join(os.getcwd(), 'resources/audios/bgm.mp3') HEROPICPATH = os.path.join(os.getcwd(), 'resources/images/hero.png-600') '''FPS''' FPS = 20 '''块大小''' BLOCKSIZE = 15 MAZESIZE = (35, 50) # num_rows * num_cols BORDERSIZE = (25, 50) # 25 * 2 + 50 * 15 = 800, 50 * 2 + 35 * 15 = 625

游戏源码:

 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游戏源代码内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是二十种Python代码游戏源代码分享的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:二十种Python代码游戏源代码分享

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址