基于Python實(shí)現(xiàn)代碼版彩票小游戲
導(dǎo)語(yǔ)
彩票是一個(gè)恒古不變的話題,現(xiàn)在的生活越來(lái)越好,大部分人開(kāi)始關(guān)注福利彩票的事情,當(dāng)然也有很多人都想中將是真的啦~哈哈哈,但是大家還是要適當(dāng)哦!
就話不多說(shuō),開(kāi)始今天 主題吧,小編今天給大家做了一款簡(jiǎn)易的彩票小游戲,讓我們看看誰(shuí)能中一等獎(jiǎng)吧?誰(shuí)又是二等獎(jiǎng)、三等獎(jiǎng)呢?
一、游戲規(guī)則
游戲里面有提前設(shè)置好的獎(jiǎng)項(xiàng),分為三個(gè),一等獎(jiǎng),二等獎(jiǎng),三等獎(jiǎng),用戶需要在14個(gè)隨機(jī)數(shù)中,連續(xù)猜6次,每次填寫(xiě)一個(gè)1~14的數(shù)字,填寫(xiě)完數(shù)字之后使用鍵盤(pán)和鼠標(biāo)點(diǎn)擊,揭示中獎(jiǎng)號(hào)碼。如果猜中6個(gè)數(shù)字表示中一等獎(jiǎng),一等獎(jiǎng)給用戶加300積分。
5個(gè)數(shù)字表示中二等獎(jiǎng),二等獎(jiǎng)給用戶加200積分。4個(gè)數(shù)字表示中三等獎(jiǎng),三等獎(jiǎng)給用戶加100積分。除此之外表示不中獎(jiǎng),不中獎(jiǎng)就會(huì)扣除用戶的50積分,每揭示中獎(jiǎng)號(hào)碼一次,會(huì)要求用戶輸入是否繼續(xù)??梢允謩?dòng)結(jié)束游戲,或者積分用完自動(dòng)結(jié)束游戲。
二、環(huán)境準(zhǔn)備
1)運(yùn)行環(huán)境
開(kāi)發(fā)環(huán)境:Python3、Pycharm社區(qū)版、Pygame,部分自帶的模塊安裝Python即可使用。
2)模塊安裝
第三方庫(kù)的安裝方式如下:
一般安裝:pip install +模塊名
鏡像源安裝:pip install -i pypi.douban.com/simple/+模塊名…
(還有很多國(guó)內(nèi)鏡像源,這里是豆瓣的用習(xí)慣了,其他鏡像源可以去看下之前文章都有的)
三、代碼展示
1)導(dǎo)入庫(kù)
import pygame from pygame.locals import * import sys,random,time,easygui
2)主程序
#pygame環(huán)境初始化
pygame.init()
#設(shè)置一個(gè)長(zhǎng)為1250,寬為700的窗口
canvas = pygame.display.set_mode((600, 450))
canvas.fill([255,255,255])
# 設(shè)置窗口標(biāo)題
pygame.display.set_caption("")
# 圖片加載
bg1 = pygame.image.load('images/bg(1).jpg')
bg2 = pygame.image.load('images/bg(2).jpg')
ball = pygame.image.load('images/ball.jpg')
h = pygame.image.load('images/h.jpg')
def handleEvent():
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
#專門(mén)寫(xiě)字
def write(str,x,y):
text=pygame.font.SysFont('simsunnsimsun',30)
laohu=text.render(str,True,(255,0,0))
canvas.blit(laohu,(x,y))
def write2(str,x,y):
text=pygame.font.SysFont('微軟雅黑',60)
laohu=text.render(str,True,(0,255,0))
canvas.blit(laohu,(x,y))
#變換狀態(tài)
def changestate():
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN and event.button==1:
if game.state==game.states[0]:
game.state=game.states[1]
if event.type==KEYDOWN and event.key==K_SPACE:
if game.state==game.states[2]:
game.state=game.states[3]
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
#用戶類(lèi)
class User():
def __init__(self):
self.socre=300
self.numbers=[]
def win(self,num):
self.socre=self.socre+num
def lose(self):
self.socre=self.socre-50
user=User()
#創(chuàng)建ball類(lèi)
class Ball():
def __init__(self,x,y):
self.img=ball
self.x=x
self.y=y
def paint(self):
canvas.blit(self.img,(self.x,self.y))
#創(chuàng)建游戲類(lèi)
class game():
#0-5個(gè)狀態(tài)
states=['歡迎','投注','隨機(jī)','依次停止','計(jì)算','是否']
state=states[0]
numbers=[]
index=0
#創(chuàng)建ball對(duì)象
def creatball():
game.balls=[Ball(30,350),
Ball(130,350),
Ball(230,350),
Ball(330,350),
Ball(430,350),
Ball(530,350)]
#揭示號(hào)碼
def removeball():
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN and event.button==1:
game.balls.pop(0)
game.index+=1
#揭示數(shù)字:
def shownumber():
write2(str(game.numbers[0]),30,350)
write2(str(game.numbers[1]),130,350)
write2(str(game.numbers[2]),230,350)
write2(str(game.numbers[3]),330,350)
write2(str(game.numbers[4]),430,350)
write2(str(game.numbers[5]),530,350)
#產(chǎn)生6個(gè)不重復(fù)的隨機(jī)數(shù),添加到列表里,算法
def creatnumber():
if not len(game.numbers)==0:
game.numbers=[]
while True:
num=random.randint(1,13)
if not num in game.numbers:
game.numbers.append(num)
if len(game.numbers)==6:
break
#清除用戶數(shù)據(jù)
def clean_user():
if len(user.numbers)==0:
return 0
else :
for i in range(len(user.numbers)):
user.numbers.pop()
#固定的幾個(gè)頁(yè)面封裝起來(lái)
def paint():
canvas.blit(bg1,(0,0))
canvas.blit(bg2,(300,0))
n=random.randint(0,14)
write(str(n),110,90)
write(str(n),410,90)
for i in game.balls:
i.paint()
#猜對(duì)了幾個(gè)?
def jisuan():
n=0
t=True
for i in user.numbers:
for j in game.numbers:
if i==j:
n+=1
if n==6:
user.win(300)
elif n==5:
user.win(200)
elif n==4:
user.win(100)
else :
user.lose()
t=False
return t
#用來(lái)在界面表達(dá)文字,數(shù)字等。
def conpaint():
canvas.blit(h,(0,0))
if game.state == game.states[0]:
write('歡迎來(lái)到偷偷樂(lè)',200,100)
write('請(qǐng)點(diǎn)擊鼠標(biāo)進(jìn)入下一個(gè)環(huán)節(jié)!',70,300)
elif game.state == game.states[1]:
creatnumber()
creatball()
clean_user()
game.index=0
easygui.msgbox('您的積分為'+str(user.socre)+'\n'+'祝您游戲愉快!!')
for i in range(6):
n=int(easygui.enterbox('請(qǐng)輸入1-14中的一個(gè)數(shù)字'))
user.numbers.append(n)
game.state=game.states[2]
elif game.state == game.states[2]:
paint()
write('請(qǐng)按下空格進(jìn)入show環(huán)節(jié)',150,130)
elif game.state == game.states[3]:
shownumber()
removeball()
paint()
write('請(qǐng)點(diǎn)擊鼠標(biāo)揭示中獎(jiǎng)號(hào)碼!',150,130)
if game.index==6:
game.state=game.states[4]
elif game.state==game.states[4]:
shownumber()
paint()
if jisuan():
easygui.msgbox('恭喜你中獎(jiǎng)了,您的積分為'+str(user.socre))
q=easygui.enterbox('是否繼續(xù)?y/n')
if q=='y':
game.state=game.states[1]
elif q=='n':
easygui.msgbox('您最終積分為'+str(user.socre))
game.state=game.states[5]
elif game.state==game.states[5]:
write('歡迎下次再來(lái)??!',200,100)
#總控制程序
def control():
if user.socre>0:
conpaint()
changestate()
else :
canvas.blit(h,(0,0))
easygui.msgbox('您的積分已經(jīng)用完')
write('歡迎下次再來(lái)??!',200,100)
time.sleep(2)
while True:
control()
pygame.display.update()
handleEvent()四、效果展示
1)游戲界面
?

?2)初始積分

3)確定環(huán)節(jié)

4)中獎(jiǎng)啦

中將之前自己輸入數(shù)字之前沒(méi)截圖的哈,這里就只展示部分截圖的。其他的需要大家自己探索喲~
到此這篇關(guān)于基于Python實(shí)現(xiàn)代碼版彩票小游戲的文章就介紹到這了,更多相關(guān)Python彩票游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pandas的連接函數(shù)concat()函數(shù)的具體使用方法
這篇文章主要介紹了pandas的連接函數(shù)concat()函數(shù)的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Django1.7+python 2.78+pycharm配置mysql數(shù)據(jù)庫(kù)
這篇文章主要介紹了Django1.7+python 2.78+pycharm配置mysql數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2016-10-10
python中以函數(shù)作為參數(shù)(回調(diào)函數(shù))的實(shí)現(xiàn)方法
這篇文章主要介紹了python中以函數(shù)作為參數(shù)(回調(diào)函數(shù))的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行
這篇文章主要介紹了Python如何提取數(shù)據(jù)中包含關(guān)鍵詞的行已經(jīng)如何去除數(shù)據(jù)中包含關(guān)鍵詞的行,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08
Python中淺拷貝copy與深拷貝deepcopy的簡(jiǎn)單理解
今天小編就為大家分享一篇關(guān)于Python中淺拷貝copy與深拷貝deepcopy的簡(jiǎn)單理解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10

