Python游戲推箱子的實(shí)現(xiàn)

?前言:

要說小時(shí)候稱霸所有翻蓋手機(jī)的小游戲,除了貪吃蛇,那就是推箱子了。
???控制小人將所有箱子放到指定位置,就是這樣簡簡單單的操作,陪伴我度過了無數(shù)個(gè)沒有動(dòng)畫片的時(shí)光。

?這個(gè)畫面是不是特別熟悉?
小編也是從玩「推箱子」那個(gè)年代過來的人。那時(shí),我拿個(gè)學(xué)習(xí)機(jī),在老師眼皮子底下,通過了一關(guān)又一關(guān)?,F(xiàn)在想起來,依然覺得很快樂。
今天一天都沒給大家更新游戲了,看大家饑渴難耐的樣子,也是時(shí)候要開始準(zhǔn)備了。
那么今天為大家準(zhǔn)備了童年經(jīng)典游戲——推箱子,有看中就趕緊上車入手吧~
1.游戲規(guī)則
推箱子游戲是一款可玩性極高的策略解謎手游,游戲中玩家將扮演一名可愛Q萌的角色,
我們需通過將場(chǎng)景內(nèi)的箱子,推送到合適的位置上進(jìn)行擺放,才可以輕松獲得游戲勝利。
整個(gè)過程雖然極其簡單,但極需玩家動(dòng)腦思考,充分的利用有效地空間,合理得將箱子推送到指定位置,從而獲得游戲勝利。
不僅如此,游戲整體畫風(fēng)十分簡潔清爽,采用了簡單和程式化的圖形設(shè)計(jì),給予了玩家前所未有的體驗(yàn)感哦。
2.材料準(zhǔn)備
玩家、箱子、背景等圖片素材:

?3.環(huán)境安裝
Python3.6、pycharm、pygame游戲模塊不能少。
pip install pygame
**導(dǎo)入游戲的素材,**增加游戲元素
def addElement(self, elem_type, col, row):
if elem_type == 'wall':
self.walls.append(elementSprite('wall.png', col, row, cfg))
elif elem_type == 'box':
self.boxes.append(elementSprite('box.png', col, row, cfg))
elif elem_type == 'target':
self.targets.append(elementSprite('target.png', col, row, cfg))
4.游戲開始、結(jié)束界面設(shè)置
def startInterface(screen, cfg):
screen.fill(cfg.BACKGROUNDCOLOR)
clock = pygame.time.Clock()
while True:
button_1 = Button(screen, (95, 150), '開始游戲', cfg)
button_2 = Button(screen, (95, 305), '退出游戲', cfg)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if button_1.collidepoint(pygame.mouse.get_pos()):
return
elif button_2.collidepoint(pygame.mouse.get_pos()):
pygame.quit()
sys.exit(0)
clock.tick(60)
pygame.display.update()
def endInterface(screen, cfg):
screen.fill(cfg.BACKGROUNDCOLOR)
clock = pygame.time.Clock()
font_path = os.path.join(cfg.FONTDIR, 'simkai.ttf')
text = '機(jī)智如你~恭喜通關(guān)!'
font = pygame.font.Font(font_path, 30)
text_render = font.render(text, 1, (255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(text_render, (120, 200))
clock.tick(60)
pygame.display.update()
如下:
?

**?****設(shè)置游戲的界面?,**導(dǎo)入關(guān)卡地圖。
class gameInterface():
def __init__(self, screen):
self.screen = screen
self.levels_path = cfg.LEVELDIR
self.initGame()
def loadLevel(self, game_level):
with open(os.path.join(self.levels_path, game_level), 'r') as f:
lines = f.readlines()
# 游戲地圖
self.game_map = gameMap(max([len(line) for line in lines]) - 1, len(lines))
# 游戲surface
height = cfg.BLOCKSIZE * self.game_map.num_rows
width = cfg.BLOCKSIZE * self.game_map.num_cols
self.game_surface = pygame.Surface((width, height))
self.game_surface.fill(cfg.BACKGROUNDCOLOR)
self.game_surface_blank = self.game_surface.copy()
for row, elems in enumerate(lines):
for col, elem in enumerate(elems):
if elem == 'p':
self.player = pusherSprite(col, row, cfg)
elif elem == '*':
self.game_map.addElement('wall', col, row)
elif elem == '#':
self.game_map.addElement('box', col, row)
elif elem == 'o':
self.game_map.addElement('target', col, row)
因?yàn)橛螒蚪缑婷娣e>游戲窗口界面, 所以需要根據(jù)人物位置滾動(dòng)。
def scroll(self):
x, y = self.player.rect.center
width = self.game_surface.get_rect().w
height = self.game_surface.get_rect().h
if (x + cfg.SCREENSIZE[0] // 2) > cfg.SCREENSIZE[0]:
if -1 * self.scroll_x + cfg.SCREENSIZE[0] < width:
self.scroll_x -= 2
elif (x + cfg.SCREENSIZE[0] // 2) > 0:
if self.scroll_x < 0:
self.scroll_x += 2
if (y + cfg.SCREENSIZE[1] // 2) > cfg.SCREENSIZE[1]:
if -1 * self.scroll_y + cfg.SCREENSIZE[1] < height:
self.scroll_y -= 2
elif (y + 250) > 0:
if self.scroll_y < 0:
self.scroll_y += 2
設(shè)置玩家的精靈類,可上下左右移動(dòng)等。
class pusherSprite(pygame.sprite.Sprite):
def __init__(self, col, row, cfg):
pygame.sprite.Sprite.__init__(self)
self.image_path = os.path.join(cfg.IMAGESDIR, 'player.png')
self.image = pygame.image.load(self.image_path).convert()
color = self.image.get_at((0, 0))
self.image.set_colorkey(color, pygame.RLEACCEL)
self.rect = self.image.get_rect()
self.col = col
self.row = row
'''移動(dòng)'''
def move(self, direction, is_test=False):
# 測(cè)試模式代表模擬移動(dòng)
if is_test:
if direction == 'up':
return self.col, self.row - 1
elif direction == 'down':
return self.col, self.row + 1
elif direction == 'left':
return self.col - 1, self.row
elif direction == 'right':
return self.col + 1, self.row
else:
if direction == 'up':
self.row -= 1
elif direction == 'down':
self.row += 1
elif direction == 'left':
self.col -= 1
elif direction == 'right':
self.col += 1
'''將人物畫到游戲界面上'''
def draw(self, screen):
self.rect.x = self.rect.width * self.col
self.rect.y = self.rect.height * self.row
screen.blit(self.image, self.rect)
'''游戲元素精靈類'''
class elementSprite(pygame.sprite.Sprite):
def __init__(self, sprite_name, col, row, cfg):
pygame.sprite.Sprite.__init__(self)
# 導(dǎo)入box.png/target.png/wall.png
self.image_path = os.path.join(cfg.IMAGESDIR, sprite_name)
self.image = pygame.image.load(self.image_path).convert()
color = self.image.get_at((0, 0))
self.image.set_colorkey(color, pygame.RLEACCEL)
self.rect = self.image.get_rect()
# 元素精靈類型
self.sprite_type = sprite_name.split('.')[0]
# 元素精靈的位置
self.col = col
self.row = row
'''將游戲元素畫到游戲界面上'''
def draw(self, screen):
self.rect.x = self.rect.width * self.col
self.rect.y = self.rect.height * self.row
screen.blit(self.image, self.rect)
'''移動(dòng)游戲元素'''
def move(self, direction, is_test=False):
if self.sprite_type == 'box':
# 測(cè)試模式代表模擬移動(dòng)
if is_test:
if direction == 'up':
return self.col, self.row - 1
elif direction == 'down':
return self.col, self.row + 1
elif direction == 'left':
return self.col - 1, self.row
elif direction == 'right':
return self.col + 1, self.row
else:
if direction == 'up':
self.row -= 1
elif direction == 'down':
self.row += 1
elif direction == 'left':
self.col -= 1
elif direction == 'right':
self.col += 1
游戲關(guān)卡循環(huán),當(dāng)某個(gè)關(guān)卡過不去的時(shí)候,想重新來按住R鍵即可返回本關(guān)卡。
def runGame(screen, game_level):
clock = pygame.time.Clock()
game_interface = gameInterface(screen)
game_interface.loadLevel(game_level)
font_path = os.path.join(cfg.FONTDIR, 'simkai.ttf')
text = '按R鍵重新開始本關(guān)'
font = pygame.font.Font(font_path, 15)
text_render = font.render(text, 1, (255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
next_pos = game_interface.player.move('left', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('left')
else:
box = game_interface.game_map.getBox(*next_pos)
if box:
next_pos = box.move('left', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('left')
box.move('left')
break
if event.key == pygame.K_RIGHT:
next_pos = game_interface.player.move('right', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('right')
else:
box = game_interface.game_map.getBox(*next_pos)
if box:
next_pos = box.move('right', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('right')
box.move('right')
break
if event.key == pygame.K_DOWN:
next_pos = game_interface.player.move('down', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('down')
else:
box = game_interface.game_map.getBox(*next_pos)
if box:
next_pos = box.move('down', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('down')
box.move('down')
break
if event.key == pygame.K_UP:
next_pos = game_interface.player.move('up', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('up')
else:
box = game_interface.game_map.getBox(*next_pos)
if box:
next_pos = box.move('up', is_test=True)
if game_interface.game_map.isValidPos(*next_pos):
game_interface.player.move('up')
box.move('up')
break
if event.key == pygame.K_r:
game_interface.initGame()
game_interface.loadLevel(game_level)
game_interface.draw(game_interface.player, game_interface.game_map)
if game_interface.game_map.levelCompleted():
return
screen.blit(text_render, (5, 5))
pygame.display.flip()
clock.tick(100)
如下:
?

?判斷****該關(guān)卡中所有的箱子是否都在指定位置, 在的話就是通關(guān)了。
def levelCompleted(self):
for box in self.boxes:
is_match = False
for target in self.targets:
if box.col == target.col and box.row == target.row:
is_match = True
break
if not is_match:
return False
return True
效果圖第二關(guān)卡如下:

到此這篇關(guān)于Python游戲推箱子的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python游戲推箱子內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)京東訂單推送到測(cè)試環(huán)境,提供便利操作示例
這篇文章主要介紹了python實(shí)現(xiàn)京東訂單推送到測(cè)試環(huán)境,提供便利操作,涉及Python基于requests模塊的網(wǎng)絡(luò)請(qǐng)求操作相關(guān)使用技巧,需要的朋友可以參考下2019-08-08
用Python進(jìn)行簡單圖像識(shí)別(驗(yàn)證碼)
這篇文章主要為大家詳細(xì)介紹了用Python進(jìn)行簡單圖像識(shí)別驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
python中關(guān)于decimal使用出現(xiàn)的一些問題
這篇文章主要介紹了python中關(guān)于decimal使用出現(xiàn)的一些問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Python中yield關(guān)鍵字及與return的區(qū)別詳解
這篇文章主要介紹了Python中yield關(guān)鍵字及與return的區(qū)別詳解,帶有 yield 的函數(shù)在 Python 中被稱之為 generator生成器,比如列表所有數(shù)據(jù)都在內(nèi)存中,如果有海量數(shù)據(jù)的話將會(huì)非常耗內(nèi)存,想要得到龐大的數(shù)據(jù),又想讓它占用空間少,那就用生成器,需要的朋友可以參考下2023-08-08
python實(shí)現(xiàn)人臉檢測(cè)的簡單實(shí)例
這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)人臉檢測(cè)的相關(guān)資料,OpenCV?可以使用機(jī)器學(xué)習(xí)算法搜索圖像中的人臉,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Python求兩個(gè)字符串最長公共子序列代碼實(shí)例
這篇文章主要介紹了Python求兩個(gè)字符串最長公共子序列代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
python django集成cas驗(yàn)證系統(tǒng)
cas是什么東西就不多說了,簡而言之就是單點(diǎn)登陸系統(tǒng),一處登陸,全網(wǎng)有權(quán)限的系統(tǒng)均可以訪問2014-07-07

