请用PYTHON编一个小游戏,如五子棋,连连看,贪吃蛇,扫雷,计算器等等

作者&投稿:吁琴 (若有异议请与网页底部的电邮联系)
请用PYTHON编一个小游戏,如五子棋,连连看,贪吃蛇,扫雷等等 邮箱python3833553@163.com~

tttttttttttttttttttt

#五子棋import appuifw,e32,key_codesfrom graphics import *def cn(x):return x.decode('utf-8')def quit(): _quit=1global runningrunning=1def redraw(rect): canvas.blit(img)def default(): global con,color,font con={"l":15,"x":15,"y":33,"r":13,"n":15} color={"bg":0x7777bb,"fg":0x333333,"p1":0x000000,"p2":0xffffff,"w":0xff0000} font=u"Sans MT 936_S60"def initial(): global img,canvas,con,color,cur_x,cur_y,turn,pos1,pos2,pos appuifw.app.screen='full' appuifw.app.body=canvas=appuifw.Canvas() img=Image.new((240,320)) img.clear(color["bg"]) cur_x=7 cur_y=7 turn=1 pos1=[] pos2=[] pos=[] for i in range(con["n"]*con["n"]): pos.append(0)def paint_back(): global img,color,font #img.text((90,25),cn('欢乐五子棋'),color["fg"],font) for i in range(con["x"],con["x"]+con["l"]*con["n"]-1,con["l"]): img.line((i,con["y"],i,con["y"]+con["l"]*(con["n"]-1)),color["fg"]) for i in range(con["y"],con["y"]+con["l"]*con["n"]-1,con["l"]): img.line((con["x"],i,con["x"]+con["l"]*(con["n"]-1),i),color["fg"]) img.text((40,270),cn('玩家1'),color["p1"],font) img.text((160,270),cn('玩家2'),color["p2"],font) img.point((90,263),color["p1"],width=con["r"],fill=color["p1"]) img.point((144,263),color["p2"],width=con["r"],fill=color["p2"]) def paint_cur(x,y,sh): global img,con,color,pos1,pos2,running if running1:return ax=con["x"]+con["l"]*x ay=con["y"]+con["l"]*y b=con["l"]/2 if sh0: c=color["p"+str(sh)] if rp((x,y))0: c=color["w"] if sh==0: c=color["bg"] img.line((ax-b,ay-2,ax-b,ay-b,ax-2,ay-b),c) img.line((ax-b,ay+2,ax-b,ay+b,ax-2,ay+b),c) img.line((ax+b,ay-2,ax+b,ay-b,ax+2,ay-b),c) img.line((ax+b,ay+2,ax+b,ay+b,ax+2,ay+b),c) redraw(())def paint_q(x,y,z): global img,con,color ax=con["x"]+con["l"]*x ay=con["y"]+con["l"]*y b=con["l"]/2 if z==0: c=color["bg"] else: c=color["p"+str(z)] img.point((ax,ay),c,width=con["r"],fill=c) redraw(()) if z==0: img.line((ax-b,ay,ax+b,ay),c) img.line((ax,ay-b,ax,ay+b),c) def k_up(): global cur_x,cur_y,con,turn paint_cur(cur_x,cur_y,0) cur_y=cur_y-1 if cur_y==-1: cur_y=con["n"]-1 paint_cur(cur_x,cur_y,turn)def k_down(): global cur_x,cur_y,con,turn paint_cur(cur_x,cur_y,0) cur_y=cur_y+1 if cur_y==con["n"]: cur_y=0 paint_cur(cur_x,cur_y,turn)def k_left(): global cur_x,cur_y,con,turn paint_cur(cur_x,cur_y,0) cur_x=cur_x-1 if cur_x==-1: cur_x=con["n"]-1 paint_cur(cur_x,cur_y,turn)def k_right(): global cur_x,cur_y,con,turn paint_cur(cur_x,cur_y,0) cur_x=cur_x+1 if cur_x==con["n"]: cur_x=0 paint_cur(cur_x,cur_y,turn)def rp(x): global con,pos if (x[0]=con["n"] or x[1]=con["n"]):return 0 #print x,pos[x[0]*con["n"]+x[1]] return pos[x[0]*con["n"]+x[1]]def wp(x,y): global con,pos pos[x[0]*con["n"]+x[1]]=y def win(): for i in pos1: k=0 for j in range(0,6): if rp((i[0]+j,i[1]))==1: k=k+1 else: break if k>=5: return 1 k=0 for j in range(0,6): if rp((i[0],i[1]+j))==1: k=k+1 else: break if k>=5: return 1 k=0 for j in range(0,6): if rp((i[0]+j,i[1]+j))==1: k=k+1 else: break if k>=5: return 1 k=0 for j in range(0,6): if rp((i[0]+j,i[1]-j))==1: k=k+1 else: break if k>=5: return 1 for i in pos2: k=0 for j in range(0,6): if rp((i[0]+j,i[1]))==2: k=k+1 else: break if k>=5: return 2 k=0 for j in range(0,6): if rp((i[0],i[1]+j))==2: k=k+1 else: break if k>=5: return 2 k=0 for j in range(0,6): if rp((i[0]+j,i[1]+j))==2: k=k+1 else: break if k>=5: return 2 k=0 for j in range(0,6): if rp((i[0]+j,i[1]-j))==2: k=k+1 else: break if k>=5: return 2 return 0 def k_enter(): global cur_x,cur_y,turn,pos1,pos2,con,color,font,running if running1:return if rp((cur_x,cur_y))==0: if turn==1: pos1.append((cur_x,cur_y)) img.rectangle((35,255,100,272),color["bg"]) img.rectangle((135,255,200,272),color["p2"]) if turn==2: pos2.append((cur_x,cur_y)) img.rectangle((35,255,100,272),color["p1"]) img.rectangle((135,255,200,272),color["bg"]) paint_q(cur_x,cur_y,turn) wp((cur_x,cur_y),turn) if win()0: #img.text((80,300),cn('玩家')+str(turn)+cn("获胜!"),color["fg"],font) img.rectangle((35,255,100,272),color["bg"]) img.rectangle((135,255,200,272),color["bg"]) paint_cur(cur_x,cur_y,0) running=2 turn=3-turn paint_cur(cur_x,cur_y,turn)def bindkey(): canvas.bind(key_codes.EKeyUpArrow, k_up) canvas.bind(key_codes.EKeyDownArrow,k_down) canvas.bind(key_codes.EKeyLeftArrow, k_left) canvas.bind(key_codes.EKeyRightArrow,k_right) canvas.bind(key_codes.EKeySelect,k_enter)default()initial()paint_back()paint_cur(cur_x,cur_y,1)img.rectangle((35,255,100,272),color["p1"])bindkey()redraw(())appuifw.app.exit_key_handler = quit()_quit=0while (1-_quit): e32.ao_sleep(0.2) redraw(())

#!/usr/bin/python
from Tkinter import *
import random
class snake(Frame):
        def __init__(self, master=None):
                Frame.__init__(self, master)
                self.body = [(0,0)]
                self.bodyid = []
                self.food = [ -1, -1 ]
                self.foodid = -1
                self.gridcount = 10
                self.size = 500
                self.di = 3
                self.speed = 500
                self.top = self.winfo_toplevel()
                self.top.resizable(False, False)
                self.grid()
                self.canvas = Canvas(self)
                self.canvas.grid()
                self.canvas.config(width=self.size, height=self.size,relief=RIDGE)
                self.drawgrid()
                s = self.size/self.gridcount
                id = self.canvas.create_rectangle(self.body[0][0]*s,self.body[0][1]*s,
                        (self.body[0][0]+1)*s, (self.body[0][1]+1)*s, fill="yellow")
                self.bodyid.insert(0, id)
                self.bind_all("<KeyRelease>", self.keyrelease)
                self.drawfood()
                self.after(self.speed, self.drawsnake)
        def drawgrid(self):
                s = self.size/self.gridcount
                for i in range(0, self.gridcount+1):
                        self.canvas.create_line(i*s, 0, i*s, self.size)
                        self.canvas.create_line(0, i*s, self.size, i*s)
        def drawsnake(self):
                s = self.size/self.gridcount
                head = self.body[0]
                new = [head[0], head[1]]
                if self.di == 1:
                        new[1] = (head[1]-1) % self.gridcount
                elif self.di == 2:
                        new[0] = (head[0]+1) % self.gridcount
                elif self.di == 3:
                        new[1] = (head[1]+1) % self.gridcount
                else:
                        new[0] = (head[0]-1) % self.gridcount
                next = ( new[0], new[1] )
                if next in self.body:
                        exit()
                elif next == (self.food[0], self.food[1]):
                        self.body.insert(0, next)
                        self.bodyid.insert(0, self.foodid)
                        self.drawfood()
                else:
                        tail = self.body.pop()
                        id = self.bodyid.pop()
                        self.canvas.move(id, (next[0]-tail[0])*s, (next[1]-tail[1])*s)
                        self.body.insert(0, next)
                        self.bodyid.insert(0, id)
                self.after(self.speed, self.drawsnake)
        def drawfood(self):
                s = self.size/self.gridcount
                x = random.randrange(0, self.gridcount)
                y = random.randrange(0, self.gridcount)
                while (x, y) in self.body:
                        x = random.randrange(0, self.gridcount)
                        y = random.randrange(0, self.gridcount)
                id = self.canvas.create_rectangle(x*s,y*s, (x+1)*s, (y+1)*s, fill="yellow")
                self.food[0] = x
                self.food[1] = y
                self.foodid = id
        def keyrelease(self, event):
                if event.keysym == "Up" and self.di != 3:
                        self.di = 1
                elif event.keysym == "Right" and self.di !=4:
                        self.di = 2
                elif event.keysym == "Down" and self.di != 1:
                        self.di = 3
                elif event.keysym == "Left" and self.di != 2:
                        self.di = 4
app = snake()
app.master.title("Greedy Snake")
app.mainloop()

贪食蛇



这些游戏都不小啊,很体现功力的。贪吃蛇好象看到过C语言版本的,python的应该了有。
不过很多需要先安装pygame包的。

五子棋、贪吃蛇、扫雷、俄罗斯方块、坦克大战、FlappyBird,

源码地址github



http://download.csdn.net/download/y453717489/4731713

这个真的没有,你百度吧


如何用Python编写一个素数环?
代码:n = int(input("请输入最大数n:"))lists = [[1]]#多个素数环 surplusnum = list(range(1,n+1)) #剩余的数 def sumisprime(x, y):x与y之和是否是素数 isprime=True#是否是素数 s = x + y#和 for i in range(2, int(s**0.5)+1):素数判定法:从2开始直到此数的...

如何用python语言编能将字符串中的每个字符的ASCII码加20后输出对应字符...
1、打开python语言命令窗口,定义一个字符串变量a并赋值,调用zfill方法,这个方法是使用0填充字符串。2、如果字符串中包含了字母,调用upper方法是将字符串中的字母全部转换为大写字母。3、字符串中的translate方法是替换指定位置的字符串,注意参数传值。4、如果只是要每个单词的第一个字母大写,调用title...

python编写一个程序将输入的内容保存到txt文件中,用到while循环。每次...
with open('1.txt', 'a+') as f: print('Input is starting, press q to quit.') loop = True while loop: input_string = input('please input something') if input_string != 'q': f.write(input_string + '\\n') else: loop = False ...

用python编写一个程序?
1 #!\/usr\/bin\/env python3 2 # coding=utf-8 3 4 def fun2(n):5 i = 1; j = 0; res = []6 while i <= n:7 if i % 3 == 0:8 res.append(i)9 j = j + 1 10 i = i + 1 11 return res 12 13 import sys 14 15 print("请输入n:")...

编写一个Python程序,输入N,返回N!的值?
1. 采用循环的方法求解 def factorial(N):total = 1 for i in range(1, N + 1):total = total * i return total 2. 采用函数式的方法求解 from functools import reduce from operator import mul def factorial(N):return reduce(mul, range(1, N + 1))...

编写一个Python程序,请使用同步赋值语句实现随机输入的两个变量值的交 ...
a = input('输入第一个:')b = input('输入第二个:')print('交换前,a = {},b = {}。'.format(a, b))a, b = b, a print('交换后,a = {},b = {}。'.format(a, b))

编一个程序,输入两个字符串str1和str2,计算str2在str1中出现的位置,输 ...
以python语言为例,示例代码如下 !\/usr\/bin\/env python -*- coding: utf-8 -*- 编一个程序,输入两个字符串str1和str2,计算str2在str1中出现的位置,输出位置结果 str1 = raw_input()str2 = raw_input()if str2 in str1:print len(str1.split(str2)[0]) + 1 运行程序,分别输入...

编写一个函数,判断三个数是否能构成一个三角形,python
!\/usr\/bin\/env python# -*- coding: utf-8 -*-a = input()b = input()c = input()if (a + b > c and a + c > b and b + c > a and abs(a - b) < c and abs(a - c) < b and abs(b - c) < a):print "能组成三角形"else:print "不能组成三角形"程序条件...

一元二次方程求根python程序
二次方程,先计算判别式,判别式小于0 的,说明方程有复数根,那么就用Complex类型来表示就行了,Complex类型是python的内置类型。已知一元二次方程的3个参数a,b,c,编写python程序求方程实根首先要了解一元二次方差的求法,然后逐步编写程序。方程为:ax^2加bx加c等于0我们先编写一个最简单的版本,...

python根据年龄算属相,python 编写一个程序,判断一个给定的
提起python根据年龄算属相,大家都知道,有人问根据属相算年龄怎么算??另外,还有人想问使用Python,根据出生日期算属相,你知道这是怎么回事?其实如何根据年龄推算属相,下面就一起来看看python 编写一个程序,判断一个给定的年份属于哪一个生肖?希望能够帮助到大家!python根据年龄算属相 defshengxiao(...

故城县17552533437: 如何用Python编写一款游戏 -
大狐饼欣丰: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50importrandomclassBig_or_Small:choices=['Big','Small'] def__init__ (self,now_bet,n=3,total=0,...

故城县17552533437: 如何用python做一个小游戏 -
大狐饼欣丰: use `pygame`

故城县17552533437: 我能用Python做什么? - -------------------------怎么做? -
大狐饼欣丰: 说小点,是掌握一门编程语言,再说大点,你可以用python解决一定的问题(前提是你知道解决问题的方法),再说大点,是把你的思想给电脑,让电脑替你做事,当你用python做一个小游戏(比如五子棋)后,会很有成就感

故城县17552533437: 如何用python编写一个游戏 -
大狐饼欣丰: 学会python 创建游戏逻辑 编写代码 测试 我的课程里就教了不少小游戏.其实学做一些简单的游戏,对于锻炼编程思维很有帮助.有兴趣可以搜 用python做些事.一起来学吧.

故城县17552533437: 如何用Python写一个小游戏 -
大狐饼欣丰: 下载pygame包 然后找本书看一下,很容易的. python基础好的话,只要2-3天就搞定了.

故城县17552533437: 用python写猜数字小游戏 -
大狐饼欣丰: 核心代码给你,具体的功能还需要自己完善. import time, random class GuessNum:def __init__(self):self._num = ''self.input_num = []self.count = 1 #猜对所用次数self.sec = 0 #猜对所用时间self._generate_num() def _generate_num(self):...

故城县17552533437: 有谁能帮我用Python解一个题或者做个小游戏,题目自行设计,大概50行, -
大狐饼欣丰: 代码如下: >>> import commands >>> dir(commands) ['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'getoutput', 'getstatus','getstatusoutput', 'mk2arg', 'mkarg'] >>> commands.getoutput("date") 'Wed Jun 10 19:39:57 CST 2009' >>> >...

故城县17552533437: - ------用Python编写骰子游戏-------几乎完成,但是有不明白的地方…… -
大狐饼欣丰: 骰子游戏 最近写了一个小游戏,就是酒吧里常玩的一种扔骰子然后猜骰子个数的游戏.写了3个版本,分别用Java,C++,python各写了一遍.Java的太简单,基本功能都没有完善,规则也不正确,就算了.C++版和python版的比较,C++版已经比...

故城县17552533437: 我想用python做一个猜四位数字的小游戏 -
大狐饼欣丰: #用的python2#代码已经放在网盘中,可以去查阅!链接:https://pan.baidu.com/s/1wLuClD2i2w6wR-L-7WahOw 提取码:2f5n --------------我是一条可爱的分割线-------------- print "人生苦短,python当歌!"

故城县17552533437: python程序:两个小孩玩剪刀石头布游戏,一人十分,赢一次得一分,输一次减一分,平手不扣分,当没 -
大狐饼欣丰: 《Linux_C编程一站式学习》里面也有这个例子,不过是人和电脑玩的. 思路: 1. 定义一个列表 gesture = ["scissor", "stone", "cloth"] 2. 两个小孩选择后记录对应的编号child1和child2,记得转换类型为整数 child1 = Int(raw_input("Child 1...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网