Python实现极限车神游戏的示例代码
目录
- 导语
- 正文
- 一、环境准备
- 二、代码实现
- 三、效果实现
导语
今天小编要为大家介绍一款小编自己用代码码出来的赛车风格的打字小游戏
取名暂定为《极限车神》打字小游戏。
这款Pygame小游戏在玩法上可以说十分创新,不仅能游戏还能学到很多不同类型的编程代码关键字的语言,比如我就做了这几款类型:python、Java等。
下面我们就来看一下这款赛车风格打字微信小游戏《极限车神》具体怎么玩吧!
正文
让我们先来看看这款Pygame的经典点评吧,让大家快速了解一下《极限车神》打字小游戏
**关键词:**休闲、赛车、竞技、竞速、操作、打字、闯关、限编程客栈时。
**氪金指数:**0氪金。
推荐星数:4星(满星5星)
难度星数:5星(满星5星)
这是一款有点考验玩家打字速度操作水平的赛车游戏,玩起来还有点意思,考验大家手速的时候到啦。平常打字看键盘的小可爱你准备好了嘛?
一、环境准备
1)运行环境
本文用到的环境如下——
Python3、Pycharm社区版,第三方模块:Tkinter、Pygame。部分自带的库只要安装完Python就可以直接使用了
一般安装:pip install +模块名 镜像源安装:pip install -i https://pypi.douban.com/simple/+模块名 (之前有说过安装报错的几种方式跟解决方法,不会安装的可以去看下,还有很多国内镜像源也有文章的)
还有准备一些数据源素材等这些大家可以用自己准备的哦!
图片文本素材等如下——
小游戏当然要有音乐更配啦~
二、代码实现
1)导入库
import tkinter as tk import threading,random,time,datetime import inspect import ctypes from tkinter import ttk from pygame import mixer
2)主程序
FLAG = True # 定义一个信号量,用于判断最前面的小车是否到达终点,游戏结束变为False CARFLAG = True # 定义小车真正在窗口上移动时的信号量,当小车在窗口上静止不动时变为False ROAD = '' # 赛道(Python,Java,mysql赛道) # 玩家类 class Player: def __init__(self,username): self.username = '[玩家]'+username # 创建小车 def car_create(self,c,carposy_list): self.car = Car('./sucai/car1.png', carposy_list, 'img1', self.username) self.car_img = tk.PhotoImage(file=self.car.filepath) c.create_image(self.car.posx, self.car.posy, image=self.car_img, tag=self.car.cartag) c.create_text(self.car.posx-30, self.car.posy, text=self.car.username, font=("Purisa", 16), fill='white', tag=self.car.username) # 电脑类 class Computer: def __init__(self,username='[电脑]'): self.username = username # 创建小车 def car_create(self,c,filepath, posy, cartag, username): self.car = Car(filepath, posy, cartag, username) self.car_img = tk.PhotoImage(file=self.car.filepath) c.create_image(self.car.posx, self.car.posy, image=self.car_img, tag=self.car.cartag) c.create_text(self.car.posx-30, self.car.posy, text=self.car.username, font=js("Purisa", 16), fill='white',tag=self.car.username) # 赛道类 class Road: def __init__(self,roadtype): self.text = [] with open(f'./sucai/{roadtype}关键字.txt', mode='r', encoding='utf-8') as f: 编程客栈 for line in f: self.text.append(line.strip().split('\t')[0]) self.road_number = int(len(self.text)*1.2) if ROAD == 'Python' else int(len(self.text)*1.5) # 赛道的数量 self.road_length = (self.road_number - 3) * 1000 # 需要跑完赛道的长度 self.road_s = 0.0 # 起点线 def begin(self,c): c.create_rectangle(100, 50, 150, 352, fill='#FFF6EE', tag='begin') c.create_text(135, 200, text='R\ne\na\nd\ny\ni\nn\ng\n \nG\nO', font=("Purisa", 20), fill='black', tag='begin') # 终点线 def destination(self,c): self.c_end = c.create_text(self.road_length - 120, 200, text='终\n点', font=("Purisa", 52), fill='white',tag='end') # 路相对于车移动 def roadmove(self,c,tag_road): global FLAG,player self.tag_road = tag_road if FLAG: self.road_s += player.car.speed if self.road_s > 1000: self.road_s = 0.0 if self.tag_road: c.delete(self.tag_road.pop(0)) for i in range(1, self.road_number + 1): c.move(i, -player.car.speed, 0) c.move(self.c_end, -player.car.speed, 0) # 小树类 class Tree: speed = 0 def __init__(self, posx,posy,filepath,treetag='tree'): self.posx = posx self.posy = posy self.filepath = filepath self.treetag = treetag # 创建树 def tree_create(self,c): self.tree_img = tk.PhotoImage(file=self.filepath) c.create_image(self.posx, self.posy, image=self.tree_img, tag=self.treetag) # 树相对于车移动 def treemove(self, c): global FLAG,player if FLAG: if self.posx >10: self.speed = player.car.speed self.posx -= self.speed c.delete(self.treetag) c.create_image(self.posx, self.posy, image=self.tree_img, tag=self.treetag) else: self.posx = random.randint(550,950) self.speed = player.car.speed self.posx -= self.speed c.create_image(self.posx, self.posy, image=self.tree_img, tag=self.treetag) # 小车类 class Car: speed = 2.0 # 初始化小车的移动速度 posx = 70 # 初始化小车的初始位置,x轴方向 car_move_distance = [] # 记录小车,以初始化速度speed = 2.0开始移动的路程 def __init__(self, filepath, posy, cartag, username): self.filepath = filepath self.posy = posy self.cartag = cartag self.username = username # 小车移动方法 def car_move(self,c,car_img): global FLAG print(self.username,'准备就绪...') self.car_img = car_img def run(): nKneMJL if FLAG: if CARFLAG: self.posx += self.speed self.car_move_distance = [] self.car_move_distance.append((self.cartag, self.posx)) c.delete(self.cartag) c.create_image(self.posx, self.posy, image=self.car_img, tag=self.cartag) c.delete(self.username) c.create_text(self.posx, self.posy, text=self.username, font=("Purisa", 13), fill='white',tag=self.username) else: c.delete(self.cartag) c.create_image(self.posx, self.posy, image=self.car_img, tag=self.cartag) c.delete(self.username) c.create_text(self.posx, self.posy, text=self.username, font=("Purisa", 13), fill='white',tag=self.username) c.after(10, run) else: print(f'{self.username} 小车停止') run() # 小车移动速度随时变化 def car_speed_change(self): global FLAG,player time.sleep(2.3) while FLAG: if player.car.speed < 14: self.speed = random.uniform(player.car.speed - 0.3, player.car.speed + 0.7) elif 14 < player.car.speed < 18: self.speed = random.uniform(player.car.speed - 0.8, player.car.speed + 0.5) else: self.speed = random.uniform(19 - 1.1, 19 + 0.3) time.sleep(1) # 打字类 class display_text: def __init__(self, gw): self.gw = gw self.l1_oop = [] # 存储提示标签对象 self.l2_oop = [] # 存储玩家打字时的标签对象 self.cpm_list = [] #用正确的总输入字符次数除以总花费的时间(以分钟为单位) self.typspeed = 0.0 # 记录打字的速度 self.anwser = '' # 拼接答案 self.text = self.gw.road.text # 单词内容 self.text2 = self.text.copy() self.text.extend(self.text2) # 单词内容翻倍 x 2 self.text_number = len(self.text) / 2 # 单词个数 self.loading_text() # 加载打字内容 def loading_text(self): col = 1 # 列数 number = 20 # 每页展示20个字 for i, text in enumerate(self.text[:number]): l1 = tk.Label(gw.text_frame, text=text, bg='white', font=('微软雅黑', 15)) l2 = tk.Label(gw.text_frame, text='', bg='white', font=('微软雅黑', 15)) if i < int(nujavascriptmber / 2): l1.grid(row=1, column=col, ) l2.grid(row=2, column=col, ) elif int(number / 2) <= i < number: l1.grid(row=3, column=col - int(number / 2), ) l2.grid(row=4, column=col - int(number / 2), ) col += 1 l1.focus_set() l1.bind("<Key>", self.l_bind) self.l1_oop.append(l1) self.l2_oop.append(l2) # 标签绑定键盘事件 def l_bind(self, event): # print(f"事件触发键盘输入:{event.char},对应的ASCII码:{event.keycode}") global FLAG if not FLAG: return if not self.text: return if event.keycode == 8: self.anwser = '' self.l2_oop[0].configure(text='') return if not 65 <= event.keycode <= 90: return self.anwser += event.char self.l2_oop[0].configure(text=self.anwser, bg='#FFFAE3') result = self.text[0] if self.anwser.strip() == result: self.cpm_list.append(result) self.l1_oop[0].configure(fg='red') self.text.remove(result) self.l1_oop.pop(0) self.l2_oop.pop(0) self.anwser = '' if not self.l1_oop and len(self.text) != 0: gw.text_frame.destroy() gw.display_text_frame() self.loading_text() return if len(self.anwser) >= len(result): self.anwser = '' # 检测打字速度, 每分钟输入了多少字符 def typing_speed(self): global FLAG time_ = 0 while FLAG: time_ += 1 try: self.cpm = round(len(''.join(self.cpm_list)) / time_ * 60) self.wpm = round(len(self.cpm_list) / (time_ / 60))# 用来表示打字速度,即每分钟打多少个字(或单词) gw.cpm_label.configure(text=f'cpm:{self.cpm}') gw.wpm_label.configure(text=f'wpm:{self.wpm}') except: self.cpm = 0 self.wpm = 0 self.typspeed = self.cpm / 12 time.sleep(1) # 游戏窗口类 class GameWindow: bgcolor = '#68696E' # 画布颜色 root = tk.Tk() root.title('打字小游戏开发') screenwidth = root.winfo_screenwidth() screenheight = root.winfo_screenheight() def __init__(self): pass # 窗口页面的大小 def window_page_size(self,Windowsize): rootwidth, rootheight = windowsize rootposx = int((self.screenwidth - rootwidth) / 2) rootposy = int((self.screenheight - rootheight) / 2) - 100 self.root.geometry('{}x{}+{}+{}'.format(rootwidth, rootheight, rootposx, rootposy)) time.sleep(0.1) # 选择赛道 def select_road(self): def func(event): global FLAG,CARFLAG,ROAD FLAG = True CARFLAG = True road = event.widget["text"] print(road) ROAD = road[:-2] self.select_road_frame.destroy() self.window_page_size((1000, 560)) self.create_canvas() configuration_project(ROAD) self.recording_time() self.display_typing_speed() self.replay_button() self.return_button() self.window_page_size((500, 300)) self.select_road_frame = tk.Frame(self.root) self.select_road_frame.place(x=0, y=0, width=500, height=300) for road in ['Python赛道','Java赛道','MySql赛道']: b = tk.Button(self.select_road_frame, text=road,font=('华文行楷', 26),relief=tk.RAISED,cursor='hand2',width=12,height=1) b.pack(pady=20) b.bind("<Button-1>", func) # 创建一张画布 def create_canvas(self): self.canvas = tk.Canvas(self.root, width=1000, height=400, bg=self.bgcolor) self.canvas.place(x=0, y=0) # 创建一条路——赛道 def create_road(self,roadtype): # self.road = Road('Java') # 实列化一个路的对象(Java类型的路) self.road = Road(roadtype) # 实列化一个路的对象(Python类型的路) self.road_img = tk.PhotoImage(file='./sucai/road.png') self.c_road = [self.canvas.create_image(i, 200, image=self.road_img, tag=f'c_road{i}') for i in range(500, self.road.road_number * 1000, 1000)] self.tag_road = [f'c_road{i}' for i in range(500, (self.road.road_number - 2) * 1000, 1000)] # 创建两棵树 def create_tree(self): self.tree1 = Tree(posx=160, posy=350,filepath='./sucai/tree1.png',treetag='tree1') self.tree1.tree_create(self.canvas) self.tree2 = Tree(posx=230, posy=340,filepath='./sucai/tree2.png', treetag='tree2') self.tree2.tree_create(self.canvas) # 创建跑程进度条 def displayprogressbar(self,caroop_list): maximum = self.road.road_length self.progressbar_list = [] for i in range(4): progressbar = ttk.Progressbar(self.canvas, length=200, maximum=maximum) progressbar.place(x=10, y= 20 if i == 0 else i*40 + 20) tk.Label(self.canvas,text=caroop_list[i].username,fg='#191970').place(x=215,y=20 if i == 0 else i*40 + 20) self.progressbar_list.append(progressbar) # 重新开始按钮 def replay_button(self): def function(): global FLAG,CARFLAG,end_label_list,dt FLAG = False CARFLAG = False for t in threading.enumerate()[1:]: stop_thread(t) self.canvas.destroy() self.text_frame.destroy() if end_label_list: for i in end_label_list: i.destroy() del dt FLAG = True CARFLAG = True self.create_canvas() configuration_project(ROAD) self.recording_time() self.return_button() self.replaybutton_img = tk.PhotoImage(file='./sucai/replay.png') tk.Label(self.root,text='重新开始').place(x=920,y=480) replaybutton = tk.Button(self.root, image=self.replaybutton_img, command=function) replaybutton.place(x=910,y=400) # 返回按钮 def return_button(self): def function(): global FLAG,CARFLAG,ROAD,end_label_list,dt FLAG = False CARFLAG = False ROAD = '' for t in threading.enumerate()[2:]: stop_thread(t) self.canvas.destroy() self.text_frame.destroy() self.cpm_label.destroy() self.wpm_label.destroy() if end_label_list: for i in end_label_list: i.destroy() del dt self.select_road() self.returnbutton_img = tk.PhotoImage(file='./sucai/return.png') returnbutton = tk.Button(self.root,text='返回',image=self.returnbutton_img,command=function) returnbutton.place(x=900,y=50) # 记录游戏时间 def recording_time(self): time_label = tk.Label(self.canvas, text='时长:00:00:00', font=("华文行楷", 15), background='#DAEFE6') time_label.place(x=870, y=20) start_time = datetime.datetime.now() def run(): global FLAG if FLAG: time_label.after(1000, run) update_time = datetime.datetime.now() - start_time self.time_ = f'时长:{update_time}'.split('.')[0] time_label.configure(text=self.time_) # 不显示时长的毫秒值 run() # 展示文本 def display_text_frame(self): self.text_frame = tk.Frame(self.root, bg='white') self.text_frame.place(x=100, y=400, width=800, height=150) # 显示打字速度 def display_typing_speed(self): self.cpm_label = tk.Label(self.root,text='cpm:0', font=("微软雅黑", 13),fg='#A52A2A') self.cpm_label.place(x=8,y=410) self.wpm_label = tk.Label(self.root, text='wpm:0', font=("微软雅黑", 13),fg='#A52A2A') self.wpm_label.place(x=8, y=440) # 播放背景音乐函数 def playmusic(self,): global FLAG mixer.init() mixer.music.load('./sucai/bgmusic.mp3') mixer.music.play() while FLAG: time.sleep(2) mixer.music.stop() # 显示窗口 def displaywindow(self): self.root.resizable(False, False) self.root.mainloop() def _async_raise(tid, exctype): tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") # 停止正在运行中的线程 def stop_thread(thread): _async_raise(thread.ident, SystemExit) # 监测小车移动时的数据变化 def monitoring_data(gw,caroop_list,dt): global FLAG,CARFLAG,end_label_list time.sleep(2.5) # 休眠2.5秒后才开始 gw.displayprogressbar(caroop_list) #显示完成跑道的进度条 gw.canvas.delete('begin') # 删除起点线 gw.road.destination(gw.canvas) # 创建终点线 player_car_s = player.car.car_move_distance[0][1] timesleep3_posx_s_list = {} for i in caroop_list: timesleep3_posx_s_list[i.username] = i.car.car_move_distance[0][1] print(i.username,'已跑起来') while FLAG: CARFLAG = False for idx,p in enumerate(gw.progressbar_list): p['value'] = timesleep3_posx_s_list[caroop_list[idx].username] if max(timesleep3_posx_s_list.values()) > gw.road.road_length: FLAG = False ranking = sorted(timesleep3_posx_s_list.items(), key=lambda x: x[1]) # 排名 for idx,username in enumerate(ranking[::-1]): l = tk.Label(gw开发者_JAVA教程.root, text=username[0] + f' 第{idx + 1}名', font=('微软雅黑', 28), bg='white') l.pack(pady=30) end_label_list.append(l) print('游戏结束') return # player.car.speed = 30 player.car.speed = dt.typspeed #使小车速度 = 打字速度 player_car_s += player.car.speed #让小车此时已移动的路程加上小车速度 timesleep3_posx_s_list[player.username] = player_car_s for i, caroop in enumerate(caroop_list[1:]): caroop.car.posx += caroop.car.speed - player.car.speed timesleep3_posx_s_list[caroop.username] = timesleep3_posx_s_list[caroop.username] + caroop.car.speed gw.road.roadmove(gw.canvas, gw.tag_road) # 赛道相对小车的速度,开始移动 gw.tree1.treemove(gw.canvas) # 树1相对小车的速度,开始移动 gw.tree2.treemove(gw.canvas) # 树2相对小车的速度,开始移动 time.sleep(0.0095) # 配置运行流程 def configuration_project(roadtype): print('游戏开始') global player,gw,dt,end_label_list tplaymusic = threading.Thread(target=gw.playmusic) # 开启一个播放背景音乐的线程 tplaymusic.setDaemon(True) tplaymusic.start() gw.create_road(roadtype) # 创建赛道 gw.create_tree() # 创建树 gw.road.begin(gw.canvas) #创建一条起点线 gw.display_text_frame() # 创建展示文本的frame end_label_list = [] # 游戏结束时,存放显示名次的标签列表 caroop_list = [] # 添加所有玩家(小车)对象 carposy_list = [90, 147, 250, 309] # 定义四个小车初始化的y轴坐标 player = Player('yyds') # 创建一个真人玩家对象 player.car_create(gw.canvas, carposy_list[0]) # 生成一个属于真人玩家的小车 caroop_list.append(player) for i in range(3): #创建三个电脑玩家 computer = Computer(f'[电脑]{i+1}') computer.car_create(gw.canvas,f'./sucai/car{i+2}.png', carposy_list[i+1], f'img{i+2}', computer.username)# 生成属于电脑玩家的小车 caroop_list.append(computer) for caroop in caroop_list: tcm = threading.Thread(target=caroop.car.car_move,args=(gw.canvas,caroop.car_img)) # 为每一个玩家开启一个线程,用来启动各自的小车 tcm.setDaemon(True) tcm.start() tcsc = threading.Thread(target=caroop.car.car_speed_change) #为每一辆小车定时随机改变小车的移动速度 tcsc.setDaemon(True) tcsc.start() dt = display_text(gw) # 实例化一个打字所用的文本内容对象 tdt = threading.Thread(target=dt.typing_speed) # 开启线程来记录打字的速度 tdt.setDaemon(True) tdt.start() tmd = threading.Thread(target=monitoring_data, args=(gw, caroop_list, dt)) # 开启线程来记录每一辆小车移动时的数据变化 tmd.setDaemon(True) tmd.start() def start(): global gw gw = GameWindow() # 实例化一个游戏窗口对象 gw.select_road() # 选择赛道 gw.displaywindow() if __name__ == '__main__': start()
三、效果实现
这款《极限车神打字小游戏》顾名思义就是一个打字的游戏,只有打字速度快,你的赛车才能
跑起来哦。游戏分为三个赛道,都是关于不同编程的关键字英文,边玩游戏还能提升自己哦,
两全其美哦!
1)游戏界面
2)Python赛道游戏界面
3)赛道起飞
到此这篇关于Python实现极限车神游戏的示例代码的文章就介绍到这了,更多相关Python极限车神游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论