求一个用python2.6编的扫雷!!!

求一个用python2.6编的扫雷!!!,第1张

Python调出窗口的那种不会做,但是可以做一个在IDLE里面直接输出的吵迟扒简升昌易扫雷程序,如下:

mine = []

show = []

for i in xrange(10):

mine.append([0] * 10)

show.append(['#'] * 10)

import random,string

for i in xrange(10):

while True:

x = random.randint(0, 9)

y = random.randint(0, 9)

if mine[x][y] == 0:

print x, y

mine[x][y] = 1

break

if True:

for i in xrange(10):

for j in xrange(10):

print mine[i][j],

print

def getX(self):

print('X='),

xRet = raw_input()

while xRet=='' or (False == isNumber(xRet)) or 0>int(xRet):

print 'Input Error,Input again(0-9):'

print('X='),

xRet = raw_input()

return int(xRet)

def getY(self):

print('Y='),

yRet = raw_input()

while yRet=='' or (False == isNumber(yRet)) or 0>int(yRet):

print 'Input Error,Input again(0-9):'

print('Y='),

yRet = raw_input()

return int(yRet)

def isNumber(strVal):

nums = string.digits

for i in strVal:

if i not in nums:

return False

return True

def open_blk(x, y):

n = 0

for i in xrange(x - 1, x + 2):

for j in xrange(y - 1, y + 2):

if i <0 or i >9 or j <0 or j >9:

continue

if mine[i][j] == 1:

n += 1

show[x][y] = str(n)

if n != 0:

return

for i in xrange(x - 1, x + 2):

for j in xrange(y - 1, y + 2):

if i <0 or i >9 or j <0 or j >旦知 9:

continue

if show[i][j] == '#':

open_blk(i, j)

print 'start'

while True:

end = True

for i in xrange(10):

for j in xrange(10):

if show[i][j] == '#' and mine[i][j] == 0:

end = False

if end:

break

for i in xrange(10):

print ' '.join(show[i])

if mine[getX(x)][getY(y)] == 1:

print 'you lost'

break

open_blk(x, y)

输入 x/y 选择位置扫雷

本回答非原创,转自开源代码分享,以下是作者信息:

作者:边雪冬

技术支持:冒泡

Email:bxd602@163.com

转载注明出处

希望我的回答能够帮到你,望采纳!

#!/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()

贪食蛇


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12556679.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存