电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

python 實現(xiàn)簡單的吃豆人游戲

瀏覽:96日期:2022-06-23 08:47:36
效果展示:

python 實現(xiàn)簡單的吃豆人游戲

程序簡介

1.使用pygame模組2.在material目錄下有一些素材3.吃豆人的游戲主體4.吃豆人怪物的AI(未使用深度學習)

主要代碼

main.py

import pygame, sysfrom pygame.locals import *from unit import user, enemyimport random#constant initializeFPS = 60BLOCK_SIZE = 24WIDTH = 29HEIGHT = 15WINDOW_WIDTH = WIDTH * BLOCK_SIZEWINDOW_HEIGHT = HEIGHT * BLOCK_SIZEMAP_NAME = './material/map.maze'BGM_NAME = './material/bgm.ogg'BLOCK_IMAGE = './material/block.png'FOOD_IMAGE = './material/food.png'GAMEOVER_IMAGE = './material/gameover.png'SERVER_PORT = 30000ENEMY_COUNT = 4OX = 1OY = 1DELAY = 8#pygame initializepygame.init()display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))clock = pygame.time.Clock()block_image = pygame.image.load(BLOCK_IMAGE)food_image = pygame.image.load(FOOD_IMAGE)gameover_image = pygame.image.load(GAMEOVER_IMAGE)bgm = pygame.mixer.music.load(BGM_NAME)scene = 'game'unit_list = []game_map = []#map initializedef load_map(filename):global game_mapgame_map.clear()file = open(filename, ’r’)for line in file.readlines():game_map.append(list(line.strip()))passpass#set passportdef through(position):x = position[0]y = position[1]in_range = (x >= 0 and x < WIDTH) and (y >= 0 and y < HEIGHT)in_space = (not game_map[y][x] == ’1’)return (in_range and in_space)pass#gameover?def check_gameover(user_pos, enemy_pos):global scenegameover = (enemy_pos[0] == user_pos[0] and enemy_pos[1] == user_pos[1])if gameover:scene = 'gameover'passreturn gameoverpass#gameoverdef gameover():pygame.mixer.music.stop()keys = pygame.key.get_pressed()if keys[K_RETURN]:initialize()passdisplay.fill((0, 0, 0))x = (WINDOW_WIDTH-gameover_image.get_width())/2y = (WINDOW_HEIGHT-gameover_image.get_height())/2display.blit(gameover_image, (x, y))pygame.display.update()pass#unit initializedef initialize_unit():unit_list.clear()ox = random.randint(1, WIDTH - 2)oy = random.randint(1, HEIGHT - 2)while not through((ox, oy)):ox = random.randint(1, WIDTH - 2)oy = random.randint(1, HEIGHT - 2)unit_list.append(user(OX, OY))for i in range(0, ENEMY_COUNT):enemy_color = i % 4ox = random.randint(1, WIDTH - 2)oy = random.randint(1, HEIGHT - 2)while not through((ox, oy)):ox = random.randint(1, WIDTH - 2)oy = random.randint(1, HEIGHT - 2)unit_list.append(enemy(enemy_color, ox, oy))passpass#initializedef initialize():global sceneload_map(MAP_NAME)initialize_unit()scene = 'game'pygame.mixer.music.play(-1)#system updatedef system_update():clock.tick(FPS)for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()pass#update controlcontrol_clock = [0, DELAY]def control_update():#user controlif control_clock[0] > control_clock[1]:user = unit_list[0]keys = pygame.key.get_pressed()passport = Falsepos = user.positionif keys[K_UP]: pos = user.move(through(user.next(0)))elif keys[K_RIGHT]: pos = user.move(through(user.next(1)))elif keys[K_DOWN]:pos = user.move(through(user.next(2)))elif keys[K_LEFT]:pos = user.move(through(user.next(3)))passgame_map[pos[1]][pos[0]] = ’0’#enemy controlu_pos = unit_list[0].positionfor index in range(1, len(unit_list)):enemy = unit_list[index]if check_gameover(u_pos, enemy.position): breakenemy.track(u_pos)passport = through(enemy.next())enemy.move(passport)while not passport:enemy.clockwise()passport = through(enemy.next())enemy.move(passport)passcontrol_clock[0] = 0passelse:control_clock[0] += 1passpass#update screendef screen_update():display.fill((0, 0, 0))for i in range(0, HEIGHT):for j in range(0, WIDTH):x = j * BLOCK_SIZEy = i * BLOCK_SIZEif game_map[i][j] == ’1’:display.blit(block_image, (x, y))elif game_map[i][j] == ’4’:display.blit(food_image, (x, y))passpasspassfor unit in unit_list:unit.update()x = unit.position[0] * BLOCK_SIZEy = unit.position[1] * BLOCK_SIZEdisplay.blit(unit.image, (x, y), unit.image_rect())pygame.display.update()pass#firstinitialize()#main loopwhile True:system_update()if scene == 'game':control_update()screen_update()else:gameover()passpass

unit.py

import pygameimport mathimport randomUSER_IMAGE = './material/user.png'ENEMY_IMAGE = [('./material/enemy%d.png' % i) for i in range(1, 5)]class unit():def __init__(self, filename):super(unit, self).__init__()self.image = pygame.image.load(filename)self.clock = [0, 5]self.direction = 0self.position = [1, 1, 1, 1]self.index = 0self.source_rect = 0passdef update(self):self.animation_update()passdef animation_update(self):self.clock[0] += 1if self.clock[0] > self.clock[1]:if self.index < 4:self.index += 4else:self.index -= 4self.source_rect = self.image_rect()self.clock[0] = 0passpassdef move(self, passport):if passport:pos = self.position[:]self.position[0] = self.position[2]self.position[1] = self.position[3]else:self.position[2] = self.position[0]self.position[3] = self.position[1]pos = self.positionpassreturn pospassdef next(self):self.ahead()return (self.position[2], self.position[3])passdef turn(self, direction):self.direction = direction % 4self.index = self.directionpassdef ahead(self):if self.direction == 0:self.position[3] -= 1elif self.direction == 1:self.position[2] += 1elif self.direction == 2:self.position[3] += 1elif self.direction == 3:self.position[2] -= 1passdef image_rect(self):w = self.image.get_width()h = self.image.get_height()ox = math.floor(w / 4 * (self.index % 4)) oy = math.floor(h / 2 * math.floor(self.index / 4))return pygame.Rect((ox, oy), (24, 24))class user(unit):def __init__(self, x, y):super(user, self).__init__(USER_IMAGE)self.position = [x, y, x, y]passdef next(self, direction):self.turn(direction)self.ahead()return (self.position[2], self.position[3])passclass enemy(unit):def __init__(self, id, x, y):filename = ENEMY_IMAGE[id]super(enemy, self).__init__(filename)self.position = [x, y, x, y]passdef track(self, user_pos):rand_dir = [1,2,3,4]self.turn(random.choice(rand_dir))passdef clockwise(self):self.turn(self.direction + 1)passclass enemy_user(unit):def __init__(self, x, y):filename = ENEMY_IMAGE[0]super(enemy_user, self).__init__(filename)self.position = [x, y, x, y]passdef move(self, x, y):self.position[0] = xself.position[1] = ypass總結:

程序還有許多地方可以完善,如怪物的AI,時間的判定等等,有興趣的大佬可以加以修改完善。

完整項目下載:https://github.com/tinytsunami/Python-Game

以上就是python 實現(xiàn)簡單的吃豆人游戲的詳細內容,更多關于python 實現(xiàn)吃豆人游戲的資料請關注好吧啦網(wǎng)其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 火锅底料批发-串串香技术培训[川禾川调官网] | 手表腕表维修保养鉴定售后服务中心网点 - 名表维修保养 | 鑫铭东办公家具一站式定制采购-深圳办公家具厂家直销 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 衬塑设备,衬四氟设备,衬氟设备-淄博鲲鹏防腐设备有限公司 | 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | 济南拼接屏_山东液晶拼接屏_济南LED显示屏—维康国际官网 | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | 寮步纸箱厂_东莞纸箱厂 _东莞纸箱加工厂-东莞市寮步恒辉纸制品厂 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | 南京精锋制刀有限公司-纵剪机刀片_滚剪机刀片_合金刀片厂家 | 数控走心机-走心机价格-双主轴走心机-宝宇百科 | 花纹铝板,合金铝卷板,阴极铝板-济南恒诚铝业有限公司 | 事迹材料_个人事迹名人励志故事 学生作文网_中小学生作文大全与写作指导 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 办公室家具_板式办公家具定制厂家-FMARTS福玛仕办公家具 | 井式炉-台车式回火炉-丹阳市电炉厂有限公司 | 棕刚玉-白刚玉厂家价格_巩义市东翔净水材料厂 | 小型气象站_便携式自动气象站_校园气象站-竞道气象设备网 | 蓄电池回收,ups电池后备电源回收,铅酸蓄电池回收,机房电源回收-广州益夫铅酸电池回收公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 盐城网络公司_盐城网站优化_盐城网站建设_盐城市启晨网络科技有限公司 | 不锈钢闸阀_球阀_蝶阀_止回阀_调节阀_截止阀-可拉伐阀门(上海)有限公司 | 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 江苏皓越真空设备有限公司 | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | NMRV减速机|铝合金减速机|蜗轮蜗杆减速机|NMRV减速机厂家-东莞市台机减速机有限公司 | 防火门-专业生产甲级不锈钢钢质防火门厂家资质齐全-广东恒磊安防设备有限公司 | 首页-瓜尔胶系列-化工单体系列-油田压裂助剂-瓜尔胶厂家-山东广浦生物科技有限公司 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | 滚珠丝杆升降机_螺旋升降机_丝杠升降机-德迈传动 | 电镀整流器_微弧氧化电源_高频电解电源_微弧氧化设备厂家_深圳开瑞节能 | 搬运设备、起重设备、吊装设备—『龙海起重成套设备』 |