Brothers, crawling too many reptiles is not good for your health. You should also consider the combination of work and rest, and occasionally change your taste.
Come and experience developing a puzzle game with python today, come and eat enough!
Python develops puzzle games
get ready
For the software environment, we can still use python and pycharm.
For those who don't or won't install it, just scan the code on the left side of the article
If the module is not installed, install the cfg and pygame modules.
win+r Open the run box, enter cmd, and press Enter to pop up a command prompt window, enter the name of the pip install module, such as pip install pygame, and then press Enter to install successfully.
I have really written this step too many times, just because I am afraid that the old iron with zero foundation will not be able to write it every time, which is bad.
Code display
Then let's go straight to the show
module import
import cfg
import pygame
from modules.misc import *
from modules.mazes import *
from modules.Sprites import *
main function
initialization
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('迷宫益智小游戏')
font = pygame.font.SysFont('Consolas', 15)
start interface
Interface(screen, cfg, 'game_start')
Record the number of levels
num_levels = 0
Record the minimum number of steps taken to pass the level
best_scores = 'None'
Level cycle switching
while True:
num_levels += 1
clock = pygame.time.Clock()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
Randomly generated level map
maze_now = RandomMaze(cfg.MAZESIZE, cfg.BLOCKSIZE, cfg.BORDERSIZE)
generate hero
hero_now = Hero(cfg.HEROPICPATH, [0, 0], cfg.BLOCKSIZE, cfg.BORDERSIZE)
count steps
num_steps = 0
Main loop inside the level
while True:
dt = clock.tick(cfg.FPS)
screen.fill((255, 255, 255))
is_move = False
↑↓←→Control 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)
show some information
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))
Determine if the game is won
if (hero_now.coordinate[0] == cfg.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == cfg.MAZESIZE[0] - 1):
break
pygame.display.update()
Update best scores
if best_scores == 'None':
best_scores = num_steps
else:
if best_scores > num_steps:
best_scores = num_steps
level switch
Interface(screen, cfg, mode='game_switch')
run
if __name__ == '__main__':
main(cfg)
Show results
Just take a picture, I won't record the video
Then for this game, you need some material files, you can click the scan code below, or scan the code on the top left to get it.
Remember to like and favorite