大家好,給大家分享一下python簡單小游戲代碼100行,很多人還不知道這一點(diǎn)。下面詳細(xì)解釋一下?,F(xiàn)在讓我們來看看!
download: python小游戲代碼
1、python 編寫一個(gè)彩票游戲?
按照題目要求編寫燃悔中的Python程序如下
import random
numlist=random.sample(range(0,10),5)
while numlist[0]==0:
? ? numlist=random.sample(range(0,10),5)
num=int(''.join([str(i) for i in numlist]))
inputnum=int(input("輸入號:"))
bonus=0
count=0
if inputnum==num:
?bonus=10000
else:
?for i in set(str(inputnum)):
? if int(i) in numlist:
?? count+=1
?bonus=1000*count
print("彩票號:%d" % num)
print("獎(jiǎng)皮山金:前喚%d元" % bonus)
(注意源代碼的縮進(jìn))
2、請用PYTHON編一個(gè)小游戲,如五子棋,連連看,貪吃蛇,掃雷,計(jì)算器等等
按照題目要求編寫燃悔中的Python程序如下
import random
numlist=random.sample(range(0,10),5)
while numlist[0]==0:
? ? numlist=random.sample(range(0,10),5)
num=int(''.join([str(i) for i in numlist]))
inputnum=int(input("輸入號:"))
bonus=0
count=0
if inputnum==num:
?bonus=10000
else:
?for i in set(str(inputnum)):
? if int(i) in numlist:
?? count+=1
?bonus=1000*count
print("彩票號:%d" % num)
print("獎(jiǎng)皮山金:前喚%d元" % bonus)
(注意源代碼的縮進(jìn))
#!/usr/bin/python from Tkinter import * import random class snake(Frame): ????????def __init__(self, master=None): ?宴和改???????????????Frame.__init__(self, master) ???????????????? = [(0,0)] ????????????????id = [] ???????????????? = [ -1, -1 ] ????????????????id = -1 ????????????????count = 10 ???????????????? = 500 ???????????????? = 3 ????????????????self.speed = 500 ???????????????? = self.winfo_toplevel() ????????????????.resizable(False, False) ????????????????() ????????????????self.canvas = Canvas(self) ????????????????() ????????????????self.canvas.config(, ,relief=RIDGE) ????????????????self.drawgrid() ????????????????s = ????????????????id = self.canvas.create_rectangle([0][0]*s,[0][1]*s, ????????????????????????([0][0]+1)*s, ([0][1]+1)*s, fill="yellow") ????????????????id.insert(0, id) ????????????????self.bind_all("<KeyRelease>", self.keyrelease) ????????????????self.drawfood() ????????????????self.after(self.speed, self.drawsnake) ????????def drawgrid(self): ????????????????s = ????????????????for i in range(0, count+1): ????????????????????????self.canvas.create_line(i*s, 0, i*s, ) ????????????????????????self.canvas.create_line(0, i*s, , i*s) ????????def drawsnake(self): ????????????????s = ????????????????head = [0] ????????????????new = [head[0], head[1]] ????????????????if == 1: ????????????????????????new[1] = (head[1]-1) % count ????????????????elif == 2: ????????????????????????new[0] = (head[0]+1) % count ????????????????elif == 3: ????????????????????????new[1] = (head[1]+1) % count ????????????????else: ????????????????????????new[0] = (head[0]-1) % count ????????????????next = ( new[0], new[1] ) ????????????????if next in : ????????????????????????exit() ????????????????elif next == ([0], [1]): ????????????????????????.insert(0, next) ??????????????????????晌判??id.insert(0, id) ????????????????????????self.drawfood() ????????????????else: ????????????????????????tail = () ????????????????????????id = () ????????????????????????(id, (next[0]-tail[0])*s, (next[1]-tail[1])*s) ?????????????棚和???????????.insert(0, next) ????????????????????????id.insert(0, id) ????????????????self.after(self.speed, self.drawsnake) ????????def drawfood(self): ????????????????s = ????????????????x = random.randrange(0, count) ????????????????y = random.randrange(0, count) ????????????????while (x, y) in : ????????????????????????x = random.randrange(0, count) ????????????????????????y = random.randrange(0, count) ????????????????id = self.canvas.create_rectangle(x*s,y*s, (x+1)*s, (y+1)*s, fill="yellow") ????????????????[0] = x ????????????????[1] = y ????????????????id = id ????????def keyrelease(self, event): ????????????????if event.keysym == "Up" and != 3: ???????????????????????? = 1 ????????????????elif event.keysym == "Right" and !=4: ???????????????????????? = 2 ????????????????elif event.keysym == "Down" and != 1: ???????????????????????? = 3 ????????????????elif event.keysym == "Left" and != 2: ???????????????????????? = 4 app = snake() app.master.title("Greedy Snake") app.mainloop()
貪食蛇
3、好玩的python代碼
按照題目要求編寫燃悔中的Python程序如下
import random
numlist=random.sample(range(0,10),5)
while numlist[0]==0:
? ? numlist=random.sample(range(0,10),5)
num=int(''.join([str(i) for i in numlist]))
inputnum=int(input("輸入號:"))
bonus=0
count=0
if inputnum==num:
?bonus=10000
else:
?for i in set(str(inputnum)):
? if int(i) in numlist:
?? count+=1
?bonus=1000*count
print("彩票號:%d" % num)
print("獎(jiǎng)皮山金:前喚%d元" % bonus)
(注意源代碼的縮進(jìn))
#!/usr/bin/python from Tkinter import * import random class snake(Frame): ????????def __init__(self, master=None): ?宴和改???????????????Frame.__init__(self, master) ???????????????? = [(0,0)] ????????????????id = [] ???????????????? = [ -1, -1 ] ????????????????id = -1 ????????????????count = 10 ???????????????? = 500 ???????????????? = 3 ????????????????self.speed = 500 ???????????????? = self.winfo_toplevel() ????????????????.resizable(False, False) ????????????????() ????????????????self.canvas = Canvas(self) ????????????????() ????????????????self.canvas.config(, ,relief=RIDGE) ????????????????self.drawgrid() ????????????????s = ????????????????id = self.canvas.create_rectangle([0][0]*s,[0][1]*s, ????????????????????????([0][0]+1)*s, ([0][1]+1)*s, fill="yellow") ????????????????id.insert(0, id) ????????????????self.bind_all("<KeyRelease>", self.keyrelease) ????????????????self.drawfood() ????????????????self.after(self.speed, self.drawsnake) ????????def drawgrid(self): ????????????????s = ????????????????for i in range(0, count+1): ????????????????????????self.canvas.create_line(i*s, 0, i*s, ) ????????????????????????self.canvas.create_line(0, i*s, , i*s) ????????def drawsnake(self): ????????????????s = ????????????????head = [0] ????????????????new = [head[0], head[1]] ????????????????if == 1: ????????????????????????new[1] = (head[1]-1) % count ????????????????elif == 2: ????????????????????????new[0] = (head[0]+1) % count ????????????????elif == 3: ????????????????????????new[1] = (head[1]+1) % count ????????????????else: ????????????????????????new[0] = (head[0]-1) % count ????????????????next = ( new[0], new[1] ) ????????????????if next in : ????????????????????????exit() ????????????????elif next == ([0], [1]): ????????????????????????.insert(0, next) ??????????????????????晌判??id.insert(0, id) ????????????????????????self.drawfood() ????????????????else: ????????????????????????tail = () ????????????????????????id = () ????????????????????????(id, (next[0]-tail[0])*s, (next[1]-tail[1])*s) ?????????????棚和???????????.insert(0, next) ????????????????????????id.insert(0, id) ????????????????self.after(self.speed, self.drawsnake) ????????def drawfood(self): ????????????????s = ????????????????x = random.randrange(0, count) ????????????????y = random.randrange(0, count) ????????????????while (x, y) in : ????????????????????????x = random.randrange(0, count) ????????????????????????y = random.randrange(0, count) ????????????????id = self.canvas.create_rectangle(x*s,y*s, (x+1)*s, (y+1)*s, fill="yellow") ????????????????[0] = x ????????????????[1] = y ????????????????id = id ????????def keyrelease(self, event): ????????????????if event.keysym == "Up" and != 3: ???????????????????????? = 1 ????????????????elif event.keysym == "Right" and !=4: ???????????????????????? = 2 ????????????????elif event.keysym == "Down" and != 1: ???????????????????????? = 3 ????????????????elif event.keysym == "Left" and != 2: ???????????????????????? = 4 app = snake() app.master.title("Greedy Snake") app.mainloop()
貪食蛇
先選取了6個(gè)自己認(rèn)為值得玩味的python代碼,希望對正在學(xué)習(xí)python的你有所幫助。
如下代碼塊可以用來計(jì)算執(zhí)行特定代碼所花費(fèi)的時(shí)間。該算法會打亂列表元素的順序,它主要會通過Fisher-Yates算法對新列表進(jìn)行排序:不需要額外的操作就能交換兩個(gè)變量的值。
Python做游戲。Pygame還是不錯(cuò)的,但只適合做小游戲。用Pygame寫個(gè)植物大戰(zhàn)僵尸還是可以的猜團(tuán)。推薦教程用Python和Pygame寫游戲空嫌。Python在游戲服務(wù)器方面也有應(yīng)用。EVE這種游戲都大量用Python。
這里不再一股腦把數(shù)學(xué)知識全丟出來,我們邊寫代碼邊說理論。首先,確保你安裝和導(dǎo)入了Tkinter,它是Python的標(biāo)準(zhǔn)GUI庫,廣泛應(yīng)用于各種各樣的項(xiàng)目和程序開發(fā),在Python中使用Tkinter可以快速的創(chuàng)建GUI應(yīng)用程序。
ant螞蟻bagels百吉餅另外貪吃蛇,吃豆子都是可以的,直接代碼就可以玩。Python包括隨您的安裝一起分發(fā)的大量標(biāo)準(zhǔn)庫。標(biāo)準(zhǔn)庫有一個(gè)稱為Turtle的模塊,這是一種向普通人介紹python編程的流行斗兆手方法。
簡單好玩的編程代碼如下所示:gsh=msgbox(已經(jīng)準(zhǔn)備好格式化,準(zhǔn)備開始。文章來源:http://www.zghlxwxcb.cn/news/detail-721248.html
4、球球各位大神怎么用python寫一個(gè)猜詞小游戲的代碼?
按照題目要求編寫燃悔中的Python程序如下
import random
numlist=random.sample(range(0,10),5)
while numlist[0]==0:
? ? numlist=random.sample(range(0,10),5)
num=int(''.join([str(i) for i in numlist]))
inputnum=int(input("輸入號:"))
bonus=0
count=0
if inputnum==num:
?bonus=10000
else:
?for i in set(str(inputnum)):
? if int(i) in numlist:
?? count+=1
?bonus=1000*count
print("彩票號:%d" % num)
print("獎(jiǎng)皮山金:前喚%d元" % bonus)
(注意源代碼的縮進(jìn))
#!/usr/bin/python from Tkinter import * import random class snake(Frame): ????????def __init__(self, master=None): ?宴和改???????????????Frame.__init__(self, master) ???????????????? = [(0,0)] ????????????????id = [] ???????????????? = [ -1, -1 ] ????????????????id = -1 ????????????????count = 10 ???????????????? = 500 ???????????????? = 3 ????????????????self.speed = 500 ???????????????? = self.winfo_toplevel() ????????????????.resizable(False, False) ????????????????() ????????????????self.canvas = Canvas(self) ????????????????() ????????????????self.canvas.config(, ,relief=RIDGE) ????????????????self.drawgrid() ????????????????s = ????????????????id = self.canvas.create_rectangle([0][0]*s,[0][1]*s, ????????????????????????([0][0]+1)*s, ([0][1]+1)*s, fill="yellow") ????????????????id.insert(0, id) ????????????????self.bind_all("<KeyRelease>", self.keyrelease) ????????????????self.drawfood() ????????????????self.after(self.speed, self.drawsnake) ????????def drawgrid(self): ????????????????s = ????????????????for i in range(0, count+1): ????????????????????????self.canvas.create_line(i*s, 0, i*s, ) ????????????????????????self.canvas.create_line(0, i*s, , i*s) ????????def drawsnake(self): ????????????????s = ????????????????head = [0] ????????????????new = [head[0], head[1]] ????????????????if == 1: ????????????????????????new[1] = (head[1]-1) % count ????????????????elif == 2: ????????????????????????new[0] = (head[0]+1) % count ????????????????elif == 3: ????????????????????????new[1] = (head[1]+1) % count ????????????????else: ????????????????????????new[0] = (head[0]-1) % count ????????????????next = ( new[0], new[1] ) ????????????????if next in : ????????????????????????exit() ????????????????elif next == ([0], [1]): ????????????????????????.insert(0, next) ??????????????????????晌判??id.insert(0, id) ????????????????????????self.drawfood() ????????????????else: ????????????????????????tail = () ????????????????????????id = () ????????????????????????(id, (next[0]-tail[0])*s, (next[1]-tail[1])*s) ?????????????棚和???????????.insert(0, next) ????????????????????????id.insert(0, id) ????????????????self.after(self.speed, self.drawsnake) ????????def drawfood(self): ????????????????s = ????????????????x = random.randrange(0, count) ????????????????y = random.randrange(0, count) ????????????????while (x, y) in : ????????????????????????x = random.randrange(0, count) ????????????????????????y = random.randrange(0, count) ????????????????id = self.canvas.create_rectangle(x*s,y*s, (x+1)*s, (y+1)*s, fill="yellow") ????????????????[0] = x ????????????????[1] = y ????????????????id = id ????????def keyrelease(self, event): ????????????????if event.keysym == "Up" and != 3: ???????????????????????? = 1 ????????????????elif event.keysym == "Right" and !=4: ???????????????????????? = 2 ????????????????elif event.keysym == "Down" and != 1: ???????????????????????? = 3 ????????????????elif event.keysym == "Left" and != 2: ???????????????????????? = 4 app = snake() app.master.title("Greedy Snake") app.mainloop()
貪食蛇
先選取了6個(gè)自己認(rèn)為值得玩味的python代碼,希望對正在學(xué)習(xí)python的你有所幫助。
如下代碼塊可以用來計(jì)算執(zhí)行特定代碼所花費(fèi)的時(shí)間。該算法會打亂列表元素的順序,它主要會通過Fisher-Yates算法對新列表進(jìn)行排序:不需要額外的操作就能交換兩個(gè)變量的值。
Python做游戲。Pygame還是不錯(cuò)的,但只適合做小游戲。用Pygame寫個(gè)植物大戰(zhàn)僵尸還是可以的猜團(tuán)。推薦教程用Python和Pygame寫游戲空嫌。Python在游戲服務(wù)器方面也有應(yīng)用。EVE這種游戲都大量用Python。
這里不再一股腦把數(shù)學(xué)知識全丟出來,我們邊寫代碼邊說理論。首先,確保你安裝和導(dǎo)入了Tkinter,它是Python的標(biāo)準(zhǔn)GUI庫,廣泛應(yīng)用于各種各樣的項(xiàng)目和程序開發(fā),在Python中使用Tkinter可以快速的創(chuàng)建GUI應(yīng)用程序。
ant螞蟻bagels百吉餅另外貪吃蛇,吃豆子都是可以的,直接代碼就可以玩。Python包括隨您的安裝一起分發(fā)的大量標(biāo)準(zhǔn)庫。標(biāo)準(zhǔn)庫有一個(gè)稱為Turtle的模塊,這是一種向普通人介紹python編程的流行斗兆手方法。
簡單好玩的編程代碼如下所示:gsh=msgbox(已經(jīng)準(zhǔn)備好格式化,準(zhǔn)備開始。
key = input('請輸入一個(gè)單詞:')
description = input('輸入單詞描述:')
chance = 5
mark = 5
print('現(xiàn)在開始游戲')
print(description + ' '+'\t 這是單詞的描述,請你輸入這個(gè)單詞: ')
for i in range(0, 5):
a = input('請你輸入單巖豎詞:')
if a == key:
print('恭喜你答對了,您的分?jǐn)?shù)%d', mark)
else:
print('對不起,你打錯(cuò)了,你還有 %d 次機(jī)會,你粗歷大的分?jǐn)?shù)%d' % (chance-1, mark-1))
if chance == 0:
print('很爛敗抱歉,你已經(jīng)沒有機(jī)會了,最后得分%d' % mark)
chance -= 1
mark -= 1文章來源地址http://www.zghlxwxcb.cn/news/detail-721248.html
到了這里,關(guān)于python簡單小游戲代碼100行,python小游戲代碼大全的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!