本篇文章给大家谈谈python小游戏源码,以及python小游戏源码开源网站对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Python游戏开发,Python实现贪吃蛇小游戏与吃豆豆 附带源码
Python版本: 3.6.4
相关模块:
pygame模块;
以及一些Python自带的模块。
安装Python并添加到环境变量,pip安装需要的相关模块即可。
贪吃蛇的 游戏 规则应该不需要我多做介绍了吧T_T。写个贪吃蛇 游戏 其实还是很简单的。首先,我们进行一下 游戏 初始化:
然后定义一个贪吃蛇类:
其中head_coord用来记录蛇头所在位置,而tail_coords是一个二维数组,用来记录所有蛇身的位置。一开始,贪吃蛇长为3,并且位置是随机生成的。用户通过 键来控制贪吃蛇的行动:
需要注意的是,贪吃蛇不能180 大拐弯,只能90 地拐弯。例如正在向左行动的贪吃蛇不能瞬间变成向右行动。具体而言,代码实现如下:
然后,我们需要随机生成一个食物,且需要保证该食物的位置不与贪吃蛇的位置相同:
在更新贪吃蛇的时候,如果它吃到了食物,则蛇身长加一,否则只是简单的按照给定的方向行动而不改变蛇身长度:
同时,当贪吃蛇吃到食物时,需要重新生成一个新的食物:
最后,当贪吃蛇碰到墙壁或者蛇头碰到蛇身时, 游戏 结束:
并显示一下 游戏 结束界面:
玩家通过 键控制 游戏 的主角吃豆人吃掉藏在迷宫内的所有豆子,并且不能被鬼魂抓到。
若能顺利吃完迷宫内的所有豆子并且不被鬼魂抓到,则 游戏 胜利,否则 游戏 失败。
逐步实现:
Step1:定义 游戏 精灵类
首先,让我们先来明确一下该 游戏 需要哪些 游戏 精灵类。
① 墙类
② 食物类(即豆豆)
③ 角色类
角色类包括吃豆人和鬼魂,鬼魂由电脑控制其运动轨迹,吃豆人由玩家控制其运动轨迹。
显然,其均需具备更新角色位置和改变角色运动方向的能力,其源代码如下:
Step2:设计 游戏 地图
利用Step1中定义的 游戏 精灵类,我们就可以开始设计 游戏 地图了。由于时间有限,我只写了一个关卡的 游戏 地图,有兴趣的小伙伴可以在此基础上进行扩展(在我的源代码基础上进行扩展是很方便滴~)。 游戏 地图的设计包括以下四方面内容:
① 创建墙
② 创建门(一开始关幽灵用的)
image.gif
③ 创建角色
④ 创建食物
因为食物不能和墙、门以及角色的位置重叠,所以为了方便设计 游戏 地图,要先创建完墙、门以及角色后再创建食物:
Step3:设计 游戏 主循环
接下来开始设计 游戏 主循环。首先是初始化:
然后定义主函数:
其中startLevelGame函数用于开始某一关 游戏 ,其源代码如下:
showText函数用于在 游戏 结束或关卡切换时在 游戏 界面中显示提示性文字,其源代码如下:
用Python写一个简单的小游戏
相信大家都玩过俄罗斯方块吧,应该是小时候的回忆吧,但是想不想了解一下这个程序是怎么写出来的呢,自己写出来的应该玩起来更有感觉吧!
感觉还是蛮好玩吧!
接下来,我就分享一下这个游戏的源码过程啊!
先用python创建一个py文件
定义这次程序所需要的类
然后写出它所需要的模块
画背景图
画网格线
# 画已经落下的方块
# 画单个方块
# 画得分等信息
这样就可以写出来一个十分简单的俄罗斯方块啦,是不是觉得还不错呢!
python小游戏2048,上班摸鱼必备(附源码)
话不多说,直接上菜
为了方便大家copy,我就不分段解释了
import turtle, random
# 定义一个类,用来画除了数字方块之外的图形
class BackGround(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
self.ht()
def draw_block(self):
self.shape('bg.gif') # 画出背景方块
for i in allpos:
self.goto(i)
self.stamp()
self.color('white', 'white') # 画出其他背景
self.goto(-215, 120)
self.begin_fill()
self.goto(215, 120)
self.goto(215, 110)
self.goto(-215, 110)
self.end_fill()
self.shape('title.gif')
self.goto(-125, 210)
self.stamp()
self.shape('score.gif')
self.goto(125, 245)
self.stamp()
self.shape('top_score.gif')
self.goto(125, 170)
self.stamp()
# 游戏失败及达成2048的提示文字
def judge(self):
global flag_win, flag_win_lose_text
self.color('blue')
judge = 0 # 判断是否还有位置可以移动
for i in block_dic.values():
for j in block_dic.values():
if i.num == 0 or i.num == j.num and i.distance(j) == 100:
judge += 1
if judge == 0: # 无位置可移动,游戏失败
self.write(' GAME OVER\n重新开始请按空格键', align='center', font=('黑体', 30, 'bold'))
flag_win_lose_text = False
if flag_win is True: # 此条件让2048达成的判断只能进行一次
for k in block_dic.values():
if k.num == 2048: # 游戏达成
flag_win = False
self.write(' 达成2048\n继续游戏请按回车键', align='center', font=('黑体', 30, 'bold'))
flag_win_lose_text = False
def win_lose_clear(self):
global flag_win_lose_text
self.clear()
flag_win_lose_text = True
def show_score(self): # 分值的显示
global score, top_score
if score top_score:
top_score = score
with open('.\\score.txt', 'w') as f:
f.write(f'{top_score}')
self.color('white')
self.goto(125, 210)
self.clear()
self.write(f'{score}', align='center', font=('Arial', 20, 'bold'))
self.goto(125, 135)
self.write(f'{top_score}', align='center', font=('Arial', 20, 'bold'))
# 数字方块类
class Block(turtle.Turtle):
def __init__(self):
super().__init__()
self.ht()
self.penup()
self.num = 0
def draw(self):
self.clear()
dic_draw = {2: '#eee6db', 4: '#efe0cd', 8: '#f5af7b',
16: '#fb9660', 32: '#f57d5a', 64: '#f95c3d',
128: '#eccc75', 256: '#eece61', 512: '#efc853',
1024: '#ebc53c', 2048: '#eec430', 4096: '#aeb879',
8192: '#aab767', 16384: '#a6b74f'}
if self.num 0: # 数字大于0,画出方块
self.color(f'{dic_draw[self.num]}') # 选择颜色
self.begin_fill()
self.goto(self.xcor()+48, self.ycor()+48)
self.goto(self.xcor()-96, self.ycor())
self.goto(self.xcor(), self.ycor()-96)
self.goto(self.xcor()+96, self.ycor())
self.goto(self.xcor(), self.ycor()+96)
self.end_fill()
self.goto(self.xcor()-48, self.ycor()-68)
if self.num 4: # 按照数字选择数字的颜色
self.color('white')
else:
self.color('#6d6058')
self.write(f'{self.num}', align='center', font=('Arial', 27, 'bold'))
self.goto(self.xcor(), self.ycor()+20)
class Game():
def init(self):
back = BackGround() # 实例画出游戏的背景
back.draw_block()
for i in allpos: # 画出16个海龟对应16个数字块
block = Block()
block.goto(i)
block_dic[i] = block
game.grow()
def restart(self): # 重开游戏的方法
global score, flag_win_lose_text
score = 0
for i in block_dic.values():
i.num = 0
i.clear()
win_lose_text.clear()
game.grow()
flag_win_lose_text = True # 此flag为游戏达成或失败出现提示语后的判断,要提示语被clear后才能继续move
def grow(self): # 随机出现一个2或4的数字块
block_list = []
for i in allpos:
if block_dic[i].num == 0:
block_list.append(block_dic[i]) # 挑出空白方块的海龟
turtle_choice = random.choice(block_list) # 随机选中其中一个海龟
turtle_choice.num = random.choice([2, 2, 2, 2, 4]) # 赋属性num=2/4
turtle_choice.draw()
win_lose_text.judge()
show_score_text.show_score()
ms.update()
def move_up(self):
allpos1 = allpos[::4] # 切片为四列
allpos2 = allpos[1::4]
allpos3 = allpos[2::4]
allpos4 = allpos[3::4]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_down(self):
allpos1 = allpos[-4::-4]
allpos2 = allpos[-3::-4]
allpos3 = allpos[-2::-4]
allpos4 = allpos[-1::-4]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_left(self):
allpos1 = allpos[:4]
allpos2 = allpos[4:8]
allpos3 = allpos[8:12]
allpos4 = allpos[12:16]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_right(self):
allpos1 = allpos[-1:-5:-1]
allpos2 = allpos[-5:-9:-1]
allpos3 = allpos[-9:-13:-1]
allpos4 = allpos[-13:-17:-1]
self.move_move(allpos1, allpos2, allpos3, allpos4)
def move_move(self, allpos1, allpos2, allpos3, allpos4):
if flag_win_lose_text is True:
count1 = self.move(allpos1) # 四列或四行依次移动
count2 = self.move(allpos2)
count3 = self.move(allpos3)
count4 = self.move(allpos4)
if count1 or count2 or count3 or count4: # 判断是否有方块移动,有才能继续出现新的数字块
self.grow()
def move(self, pos_list):
num_list = [] # 为某一列或行的数字块海龟的坐标
for i in pos_list:
num_list.append(block_dic[i].num) # 把这些海龟的NUM形成list
new_num_list, count = self.list_oper(num_list) # 只是list_oper的方法形成新的list
for j in range(len(new_num_list)): # 把新的list依次赋值给对应的海龟.num属性并调用draw()方法
block_dic[pos_list[j]].num = new_num_list[j]
block_dic[pos_list[j]].draw()
return count
def list_oper(self, num_list): # num_list的操作,假设其为【2,0,2,2】
global score
count = True
temp = []
new_temp = []
for j in num_list:
if j != 0:
temp.append(j) # temp=[2,2,2]
flag = True
for k in range(len(temp)):
if flag:
if k len(temp)-1 and temp[k] == temp[k+1]:
new_temp.append(temp[k]*2)
flag = False
score += temp[k]
else:
new_temp.append(temp[k]) # new_temp=[4,2]
else:
flag = True
for m in range(len(num_list)-len(new_temp)):
new_temp.append(0) # new_temp=[4,2,0,0]
if new_temp == num_list:
count = False # 此变量判断num_list没有变化,数字块无移动
return(new_temp, count)
if __name__ == '__main__':
ms = turtle.Screen() # 主窗口的设置
ms.setup(430, 630, 400, 50)
ms.bgcolor('gray')
ms.title('2048')
ms.tracer(0)
ms.register_shape('bg.gif')
ms.register_shape('title.gif')
ms.register_shape('score.gif')
ms.register_shape('top_score.gif')
block_dic = {} # 放数字方块海龟的字典,位置坐标为key,对应海龟为value
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
flag_win = True # 达成2048的判断,让达成的文字仅出现一次
flag_win_lose_text = True # 用来判断失败或成功的提示文字是否有被清除,不清除不能继续移动方块
score = 0
with open('.\\score.txt', 'r') as f:
top_score = int(f.read()) # 读取score中的数据
show_score_text = BackGround()
win_lose_text = BackGround()
game = Game()
game.init()
ms.listen()
ms.onkey(game.move_up, 'Up')
ms.onkey(game.move_down, 'Down')
ms.onkey(game.move_left, 'Left')
ms.onkey(game.move_right, 'Right')
ms.onkey(win_lose_text.win_lose_clear, 'Return')
ms.onkey(game.restart, 'space')
ms.mainloop()
这是游戏界面:
欢迎挑战最高分。
要运行出来,必须本地要有这些文件:bg.gif,score.gif,title.gif,top_score.gif,score.txt
我把这些文件放在了群里,还有一些学习的资料,群号642109462,欢迎对python感兴趣的进群讨论。
支持作者的,可以关注和点赞。感谢你们!
python简单小游戏代码
1、Python猜拳小游戏代码:
2、import random #导入随机模块
3、
4、num = 1
5、yin_num = 0
6、shu_num = 0
7、while num2:
12、 print('不能出大于2的值')
13、 else:
14、 data = ['石头', '剪刀', '布']
15、 com = random.randint(0, 2)
16、 print(您出的是{},电脑出的是{}.format(data[user], data[com]))
17、 if user == com:
18、 print('平局')
19、 continue
20、 elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):
21、 print('你赢了')
22、 yin_num += 1
23、 else:
24、 print('你输了')
25、 shu_num += 1
26、 num += 1
27、Python数字炸弹小游戏代码:
28、import random
29、import time
30、
31、bomb = random.randint(1, 99)
32、print(bomb)
33、start = 0
34、end = 99
35、while 1 == 1:
36、
37、 people = int(input('请输入{}到{}之间的数:'.format(start, end)))
38、 if people bomb:
39、 print('大了')
40、 end = people
41、 elif people bomb:
42、 print('小了')
43、 start = people
44、 else:
45、 print('BOOM!!!')
46、 break
47、 print('等待电脑了输入{}到{}之间的数:'.format(start, end))
48、 time.sleep(1)
49、 com = random.randint(start + 1, end - 1)
50、 print('电脑输入:{}'.format(com))
51、 if com bomb:
52、 print('大了')
53、 end = com
54、 elif com bomb:
55、 print('小了')
56、 start = com
57、 else:
58、 print('BOOM!!!')
59、 break
关于python小游戏源码和python小游戏源码开源网站的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
2、本站永久网址:https://www.yuanmacun.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
源码村资源网 » python小游戏源码(python小游戏源码开源网站)
1 评论