基于Python實(shí)現(xiàn)牛牛套圈小游戲的示例代碼
前言
“幸運(yùn)牛牛套圈圈”套住歡樂,圈住幸福,等你來挑戰(zhàn)!
哈嘍,大家上午好,我是你們的栗子同學(xué),今天來給大家?guī)硪豢钚掠螒?,是不是很久沒給大家更新游戲板塊了呀~期不期待呢? 哈哈哈。——憶童年
沒有網(wǎng)絡(luò)也沒有智能手機(jī)的年代,現(xiàn)在回想起來,那么多無聊的時(shí)間,我們是怎么打發(fā)的呢?
每個(gè)“大孩子”應(yīng)該都有自己的游戲回憶錄,見證了那個(gè)時(shí)候的天馬行空。
那個(gè)時(shí)候的我們邊玩著古董游戲機(jī),邊哼唱超級(jí)瑪麗和魂斗羅的配樂,偶爾逛逛公園里擺滿玩具物件的套圈攤子,偷偷在課間翻看風(fēng)靡兒時(shí)的連環(huán)畫……
童年匆匆,沒有任何儀式與記載便從我們的年華里溜走,幸好它還留下了一些天真爛漫的日子供我們懷念。今天來給大家做了一個(gè)新款的斗牛士的套圈小游戲,希望大家喜歡哦~
當(dāng)然,嫌棄牛牛的話可以換成其他的可愛的小寵物哦!
一、環(huán)境配置
1)運(yùn)行環(huán)境
Python 3 、Pycharm、Pygame。其他內(nèi)置模塊,安裝好python環(huán)境就可以了。
(win + R 輸入cmd 輸入安裝命令 pip install 模塊名 (如果你覺得安 裝速度比較慢, 你可 以切換國內(nèi)鏡像源))
2)第三方庫安裝
pip install + 模塊名 或者 帶鏡像源 pip install -i pypi.douban.com/simple/ +模塊名
3)素材內(nèi)容

二、代碼展示
1)主程序
import pygame,sys
from pygame.locals import *
pygame.init()
canvas = pygame.display.set_mode((1000,700))
pygame.display.set_caption("無敵斗牛士套圈小游戲")
bg = pygame.image.load("images/bg.png")
bull_img = pygame.image.load("images/bull.png")
rope_img = pygame.image.load("images/rope.png")
win = pygame.image.load("images/win.png")
lose = pygame.image.load("images/lose.png")
bow = pygame.image.load("images/bow.png")
print("測試Test")
print("[測試Test:1]碰撞檢測:False")
state = "RUNNING"
print("[測試Test:4]state更改為RUNNING")
on_off = 0
def handleEvent():
global on_off,state
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
print("[測試Test:2]結(jié)束測試Test")
print("結(jié)束測試Test")
if state == "RUNNING":
raise UserWarning("[Error:1]中途退出")
pygame.quit()
sys.exit()
if event.tye == KEYDOWN and event.key == K_SPACE:
on_off = 1
print("[測試Test:3]on_off更改為1")
class Rope():
def __init__(self,x,y,width,height,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
self.speedY = -1
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
global on_off
if on_off == 1:
self.y += self.speedY * 10
if self.y < 400:
self.speedY = 1
elif self.y > 600:
self.speedY = -1
on_off = 0
print("[測試Test:3]on_off更改為0")
def hit(self,b):
return self.y == b.y and self.x + 40 > b.x and self.x < b.x + 40
class Bull():
def __init__(self,x,y,width,height,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
self.x = self.x + 10
if self.x > 1000:
self.x = 0
def comPaint():
canvas.blit(bg,(0,0))
bull.paint()
rope.paint()
def comMove():
bull.move()
rope.move()
def checkHit():
global on_off,state
if rope.hit(bull):
print("[測試Test:1]碰撞檢測:True")
on_off = 0
state = "SUCCESS"
print("[測試Test:4]state更改為SUCCESS")
print("[測試Test:5]停止運(yùn)動(dòng)")
rope = Rope(385,600,39,52,rope_img)
print("[測試Test:6]rope創(chuàng)建")
bull = Bull(0,400,192,141,bull_img)
print("[測試Test:6]bull創(chuàng)建")
while True:
if state == "RUNNING":
comPaint()
comMove()
checkHit()
elif state == "SUCCESS":
comPaint()
canvas.blit(win,(0,0))
handleEvent()
pygame.display.update()
pygame.time.delay(10)三、效果展示
這款游戲很簡單,效果做的不多啦:斗牛士動(dòng)態(tài)的效果,空格移動(dòng)套圈,套中游戲勝利即可結(jié)束。
1)界面截圖

?2)套空

3)套中勝利

到此這篇關(guān)于基于Python實(shí)現(xiàn)牛牛套圈小游戲的示例代碼的文章就介紹到這了,更多相關(guān)Python牛牛套圈游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Python中使用MongoEngine操作數(shù)據(jù)庫教程實(shí)例
這篇文章主要介紹了在Python中使用MongoEngine操作數(shù)據(jù)庫教程實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
pyCharm 設(shè)置調(diào)試輸出窗口中文顯示方式(字符碼轉(zhuǎn)換)
這篇文章主要介紹了pyCharm 設(shè)置調(diào)試輸出窗口中文顯示方式(字符碼轉(zhuǎn)換),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
基于DataFrame篩選數(shù)據(jù)與loc的用法詳解
今天小編就為大家分享一篇基于DataFrame篩選數(shù)據(jù)與loc的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python tkinter庫繪制春聯(lián)和福字的示例詳解
馬上要過年了,這篇文章將用到Python中的tkinter庫來寫一副春聯(lián)&福字送給大家。文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以試一試2022-01-01
Django添加bootstrap框架時(shí)無法加載靜態(tài)文件的解決方式
這篇文章主要介紹了Django添加bootstrap框架時(shí)無法加載靜態(tài)文件的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03

