Why Python is so popular? Python is relatively simple compared with other languages, and even ordinary people with zero foundation can master it quickly. In other aspects, for example, crawlers in the gray world, VIP videos, novels, songs, nothing crawler can not solve; Data mining and analysis, Taobao is an example, want to open a Taobao shop, need to obtain the relevant commodity information, then data analysis can solve and so on; Game development, automated testing, artificial intelligence, hacking language and more.
Continue to introduce today will not stop, as long as the Python understanding, use, should know his powerful place, into the theme, games, today directly on dry products, childhood you played 21 games source share.
1. Backgammon
Display:
Game source code:
"Game start screen"
class gameStartUI(QWidget) :
def __init__(self, parent=None, **kwargs) :
super(gameStartUI, self).__init__(parent)
self.setFixedSize(760.650)
self.setWindowTitle('Backgammon - Nine songs')
self.setWindowIcon(QIcon(cfg.ICON_FILEPATH))
# Background image
palette = QPalette()
palette.setBrush(self.backgroundRole(), QBrush(QPixmap(cfg.BACKGROUND_IMAGEPATHS.get('bg_start'))))
self.setPalette(palette)
# button
# -- Man versus machine
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)
# -- Play online
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)
"Man versus machine"
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()
"Go Online."
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_())
Copy the code
2. Defend the forest
Display:
Game source code:
import cfg
import pygame
from modules import *
Principal function
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("Tower Defense game -- Nine Songs"** start_interface = StartInterface(CFG) is_play = start_interface.update(screen)if not is_play:
return** Calls the game interface **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()
Copy the code
3. The Super Maze
Display:
Game source code:
import cfg
import sys
import pygame
from modules import *
Principal function
def main(cfg) :** 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 - Nine songs')
font = pygame.font.SysFont('Consolas'.15) Start Interface(screen, CFG,'game_start') Record number of levels num_levels =0Record the minimum number of steps best_scores ='None'Level loop switchwhile True:
num_levels += 1Clock = 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) -- num_steps =0-- Main loop within the levelwhile 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) ---- display 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 a winif (hero_now.coordinate[0] == cfg.MAZESIZE[1] - 1) and (hero_now.coordinate[1] == cfg.MAZESIZE[0] - 1) :breakPygame.display.update () -- Updates the best resultif best_scores == 'None':
best_scores = num_steps
else:
ifBest_scores > num_steps: best_scores = num_steps -- Level switch Interface(screen, CFG, mode='game_switch')
'''run'''
if __name__ == '__main__':
main(cfg)
Copy the code
There are so many that need access to resources,You can click to get it.
4. Ski games
5. Gold game
6, tower defense games
7. Tank Wars
Repeatedly look
9, demining
10. Super Mario Bros
The Bomber
Too many, lazy will not be shown, these games accompany our happiness at different stages of growth, need resources to pay attention toI click to get resources. I’m a sharing socialite, thanks for watching.