本文主要是介绍20几个 Python 小游戏,上班摸鱼我能玩一天(内附源码、python工具),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天给大家带来上班摸鱼的20个游戏,超级有趣,很简单,这个需要先把python编辑器安装好,需要python编辑器(破解版)文章末尾获取!
1、平衡木
玩法:也是小时候的经典游戏,控制左右就行,到后面才有一点点难度。
源码分享:
import cfg
from modules import breakoutClone'''主函数'''
def main():game = breakoutClone(cfg)game.run()'''run'''
if __name__ == '__main__':main()
【还有配置文件】
2、外星人入侵
玩法:这让我想起了魂斗罗那第几关的boss,有点类似,不过魂斗罗那个难度肯定高点。
源码分享:
import os
import sys
import cfg
import random
import pygame
from modules import *'''开始游戏'''
def startGame(screen):clock = pygame.time.Clock()# 加载字体font = pygame.font.SysFont('arial', 18)if not os.path.isfile('score'):f = open('score', 'w')f.write('0')f.close()with open('score', 'r') as f:highest_score = int(f.read().strip())# 敌方enemies_group = pygame.sprite.Group()for i in range(55):if i < 11:enemy = enemySprite('small', i, cfg.WHITE, cfg.WHITE)elif i < 33:enemy = enemySprite('medium', i, cfg.WHITE, cfg.WHITE)else:enemy = enemySprite('large', i, cfg.WHITE, cfg.WHITE)enemy.rect.x = 85 + (i % 11) * 50enemy.rect.y = 120 + (i // 11) * 45enemies_group.add(enemy)boomed_enemies_group = pygame.sprite.Group()en_bullets_group = pygame.sprite.Group()ufo = ufoSprite(color=cfg.RED)# 我方myaircraft = aircraftSprite(color=cfg.GREEN, bullet_color=cfg.WHITE)my_bullets_group = pygame.sprite.Group()# 用于控制敌方位置更新# --移动一行enemy_move_count = 24enemy_move_interval = 24enemy_move_flag = False# --改变移动方向(改变方向的同时集体下降一次)enemy_change_direction_count = 0enemy_change_direction_interval = 60enemy_need_down = Falseenemy_move_right = Trueenemy_need_move_row = 6enemy_max_row = 5# 用于控制敌方发射子弹enemy_shot_interval = 100enemy_shot_count = 0enemy_shot_flag = False# 游戏进行中running = Trueis_win = False# 主循环while running:screen.fill(cfg.BLACK)for event in pygame.event.get():# --点右上角的X或者按Esc键退出游戏if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:pygame.quit()sys.exit()# --射击if event.type == pygame.MOUSEBUTTONDOWN:my_bullet = myaircraft.shot()if my_bullet:my_bullets_group.add(my_bullet)# --我方子弹与敌方/UFO碰撞检测for enemy in enemies_group:if pygame.sprite.spritecollide(enemy, my_bullets_group, True, None):boomed_enemies_group.add(enemy)enemies_group.remove(enemy)myaircraft.score += enemy.rewardif pygame.sprite.spritecollide(ufo, my_bullets_group, True, None):ufo.is_dead = Truemyaircraft.score += ufo.reward# --更新并画敌方# ----敌方子弹enemy_shot_count += 1if enemy_shot_count > enemy_shot_interval:enemy_shot_flag = Trueenemies_survive_list = [enemy.number for enemy in enemies_group]shot_number = random.choice(enemies_survive_list)enemy_shot_count = 0# ----敌方移动enemy_move_count += 1if enemy_move_count > enemy_move_interval:enemy_move_count = 0enemy_move_flag = Trueenemy_need_move_row -= 1if enemy_need_move_row == 0:enemy_need_move_row = enemy_max_rowenemy_change_direction_count += 1if enemy_change_direction_count > enemy_change_direction_interval:enemy_change_direction_count = 1enemy_move_right = not enemy_move_rightenemy_need_down = True# ----每次下降提高移动和射击速度enemy_move_interval = max(15, enemy_move_interval-3)enemy_shot_interval = max(50, enemy_move_interval-10)# ----遍历更新for enemy in enemies_group:if enemy_shot_flag:if enemy.number == shot_number:en_bullet = enemy.shot()en_bullets_group.add(en_bullet)if enemy_move_flag:if enemy.number in range((enemy_need_move_row-1)*11, enemy_need_move_row*11):if enemy_move_right:enemy.update('right', cfg.SCREENSIZE[1])else:enemy.update('left', cfg.SCREENSIZE[1])else:enemy.update(None, cfg.SCREENSIZE[1])if enemy_need_down:if enemy.update('down', cfg.SCREENSIZE[1]):running = Falseis_win = Falseenemy.change_count -= 1enemy.draw(screen)enemy_move_flag = Falseenemy_need_down = Falseenemy_shot_flag = False# ----敌方爆炸特效for boomed_enemy in boomed_enemies_group:if boomed_enemy.boom(screen):boomed_enemies_group.remove(boomed_enemy)del boomed_enemy# --敌方子弹与我方飞船碰撞检测if not myaircraft.one_dead:if pygame.sprite.spritecollide(myaircraft, en_bullets_group, True, None):myaircraft.one_dead = Trueif myaircraft.one_dead:if myaircraft.boom(screen):myaircraft.resetBoom()myaircraft.num_life -= 1if myaircraft.num_life < 1:running = Falseis_win = Falseelse:# ----更新飞船myaircraft.update(cfg.SCREENSIZE[0])# ----画飞船myaircraft.draw(screen)if (not ufo.has_boomed) and (ufo.is_dead):if ufo.boom(screen):ufo.has_boomed = Trueelse:# ----更新UFOufo.update(cfg.SCREENSIZE[0])# ----画UFOufo.draw(screen)# --画我方飞船子弹for bullet in my_bullets_group:if bullet.update():my_bullets_group.remove(bullet)del bulletelse:bullet.draw(screen)# --画敌方子弹for bullet in en_bullets_group:if bullet.update(cfg.SCREENSIZE[1]):en_bullets_group.remove(bullet)del bulletelse:bullet.draw(screen)if myaircraft.score > highest_score:highest_score = myaircraft.score# --得分每增加2000我方飞船增加一条生命if (myaircraft.score % 2000 == 0) and (myaircraft.score > 0) and (myaircraft.score != myaircraft.old_score):myaircraft.old_score = myaircraft.scoremyaircraft.num_life = min(myaircraft.num_life + 1, myaircraft.max_num_life)# --敌人都死光了的话就胜利了if len(enemies_group) < 1:is_win = Truerunning = False# --显示文字# ----当前得分showText(screen, 'SCORE: ', cfg.WHITE, font, 200, 8)showText(screen, str(myaircraft.score), cfg.WHITE, font, 200, 24)# ----敌人数量showText(screen, 'ENEMY: ', cfg.WHITE, font, 370, 8)showText(screen, str(len(enemies_group)), cfg.WHITE, font, 370, 24)# ----历史最高分showText(screen, 'HIGHEST: ', cfg.WHITE, font, 540, 8)showText(screen, str(highest_score), cfg.WHITE, font, 540, 24)# ----FPSshowText(screen, 'FPS: ' + str(int(clock.get_fps())), cfg.RED, font, 8, 8)# --显示剩余生命值showLife(screen, myaircraft.num_life, cfg.GREEN)pygame.display.update()clock.tick(cfg.FPS)with open('score', 'w') as f:f.write(str(highest_score))return is_win'''主函数'''
def main():# 初始化pygame.init()pygame.display.set_caption('外星人入侵 —— 九歌')screen = pygame.display.set_mode(cfg.SCREENSIZE)pygame.mixer.init()pygame.mixer.music.load(cfg.BGMPATH)pygame.mixer.music.set_volume(0.4)pygame.mixer.music.play(-1)while True:is_win = startGame(screen)endInterface(screen, cfg.BLACK, is_win)'''run'''
if __name__ == '__main__':main()
3、贪心鸟
玩法:有点类似那个炸弹人,控制好走位问题不大。
4、井字棋888‘’
玩法:我打赌大家在课堂上肯定玩过这个,想想当年和同桌玩这个废了好几本本子。
源码分享
from tkinter import *
import tkinter.messagebox as msgroot = Tk()
root.title('TIC-TAC-TOE---Project Gurukul')
# labels
Label(root, text="player1 : X", font="times 15").grid(row=0, column=1)
Label(root, text="player2 : O", font="times 15").grid(row=0, column=2)digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]# for player1 sign = X and for player2 sign= Y
mark = ''# counting the no. of click
count = 0panels = ["panel"] * 10def win(panels, sign):return ((panels[1] == panels[2] == panels[3] == sign)or (panels[1] == panels[4] == panels[7] == sign)or (panels[1] == panels[5] == panels[9] == sign)or (panels[2] == panels[5] == panels[8] == sign)or (panels[3] == panels[6] == panels[9] == sign)or (panels[3] == panels[5] == panels[7] == sign)or (panels[4] == panels[5] == panels[6] == sign)or (panels[7] == panels[8] == panels[9] == sign))def checker(digit):global count, mark, digits# Check which button clickedif digit == 1 and digit in digits:digits.remove(digit)##player1 will play if the value of count is even and for odd player2 will playif count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton1.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 2 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton2.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 3 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton3.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 4 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton4.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 5 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton5.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 6 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton6.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 7 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton7.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 8 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton8.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 9 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton9.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()###if count is greater then 8 then the match has been tiedif (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):msg.showinfo("Result", "Match Tied")root.destroy()####define buttons
button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))
button1.grid(row=1, column=1)
button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))
button2.grid(row=1, column=2)
button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))
button3.grid(row=1, column=3)
button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))
button4.grid(row=2, column=1)
button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5))
button5.grid(row=2, column=2)
button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6))
button6.grid(row=2, column=3)
button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7))
button7.grid(row=3, column=1)
button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8))
button8.grid(row=3, column=2)
button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9))
button9.grid(row=3, column=3)root.mainloop()
有点困难
【玩法详解+源码获取看底部】
5、炸弹人
玩法详解:小时候的又一经典游戏,小时候很多次都被自己炸死了。
6、保卫森林
玩法详解:类似保卫萝卜,塔防类的小游戏,布局一定要合理,考虑射程属性等等
7、五子棋
玩法详解:小时候很爱玩,先出是有必胜方法的,后面才知道会有禁手这个规则,就比较复杂了,大家可以学一下先出必胜的开局,有浦月、流星、丘月、游星、慧星等等。
8、吃豆豆
玩法详解:考验手速和操作和走位,我不喜欢玩这类跑来跑去的。
9、坦克大战
玩法详解:这是经典中的经典,我喜欢玩双人模式,后面有一些改版的模式,这是我觉得少数几个现在玩都不过时的游戏。
10、超级玛丽
玩法详解:经典中的经典,小时候玩觉得可难了,操作不必介绍了吧。
11、水果忍者
玩法详解:切水果风靡一时的游戏,不知道为啥总是切刀炸掉,挺解压的游戏。
极度困难
【攻略大全+源码获取看底部】
12、飞机大战
攻略大全:从这里开始的游戏,真正算的上有难度了,这个飞机大战跟童年玩的比起来还是差一点。
13、2048
攻略大全:也是曾经风靡一时的,越到后面越难,合成的时候一定要大数放在角落。
14、推箱子
攻略大全:以前的那个手机上都有的游戏,越推到后面的关卡越难,我好像是玩到二十多关就玩不下去了。
15、塔防
攻略大全:又是一种塔防类的游戏,有点意思,就是速度太快了,反应不过来。
16、植物大战僵尸
攻略大全:最经典的植物大战僵尸,操作不用介绍了,不过可以自己玩玩看。
17、扫雷
玩法详解:扫雷还是挺有意思的,技能玩又考验推理
终极挑战
【太难了。。源码领取看文章底部】
18、拼图
游戏体验:三个终极挑战,能完成一个就算你厉害,拼图是我最烦的,太难了。
19、走迷宫
游戏体验:我反正没走出去,大家能走出去吗
20、最强游戏
游戏体验:可太难控制了。。
(游戏源码、环境、python工具破解版视频安装教程、安装包)已经上传至 CSDN 官方,朋友们如果需要可以微信扫描下方CSDN官方认证二维码【免费获取】
这篇关于20几个 Python 小游戏,上班摸鱼我能玩一天(内附源码、python工具)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!