十個Python經(jīng)典小游戲的代碼合集
更新時間:2022年05月14日 11:43:08 作者:五包辣條!
這篇文章主要為大家分享十個Python經(jīng)典的小游戲代碼,非常適合Python初學者練手。文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下
1.小貓運動
游戲源碼
# @Author : 辣條 ''' 多行注釋 本程序運行后會有一只小貓向前走 安裝模塊 pip install sprites ''' # 從精靈模塊導入所有命令 from sprites import * # 新建角色,造型序列為images images = 'res/cat1.png','res/cat2.png' # 新建角色 cat = Sprite(shape=images) # 播放喵聲 cat.play('喵.wav') # 當成立的時候(重復執(zhí)行) while True: # 前進10 cat.fd(10) # 下一個造型 cat.nextcostume() # 等待0.3秒 cat.wait(0.3)
游戲效果
2.彈球
游戲源碼
# @Author : 辣條 """ 彈球,本程序主要演示碰到邊緣就反彈這個命令 """ # 從精靈模塊導入所有命令 from sprites import * # 1代表彈球 ball = Sprite(1) while True: # 前進0.1 ball.fd(0.1) # 碰到邊緣就反彈 ball.bounce_on_edge()
游戲效果
3.畫正方形
游戲源碼
# @Author : 大海 from sprites import * # 新建蟲子精靈 bug=Sprite() # 繪畫延時為10毫秒 bug.screen.delay(10) # 畫筆尺寸 bug.pensize(4) # 畫筆顏色 # 寶藍 bug.color('dodger blue') # 落筆 bug.pendown() # 重復4次 for _ in range(4): # 前進100 bug.fd(100) # 右轉90 bug.rt(90) # 抬筆 bug.penup()
游戲效果
4.彩點實驗
游戲源碼
# @Author : 大海 ''' 彩點實驗 ''' import time from sprites import * d = Sprite(shape='black',visible=False) d.screen.bgcolor('black') while True: d.randompos() d.randomcolor() time.sleep(0.1) d.dot(random.randint(10,100))
游戲效果
5.彩點實驗圓形
游戲源碼
from sprites import * d = Sprite(shape='blank',visible=False) d.screen.bgcolor('black') radius = 200 while True: d.randomcolor() d.randomheading() d.fd(random.randint(1,radius)) d.dot(10) d.home()
游戲效果
6.彩點實驗下三角
游戲源碼
''' 6.彩點實驗下三角 ''' from sprites import * d = Sprite(shape='blank',visible=False) d.screen.bgcolor('black') while True: d.randompos() if d.xcor() > d.ycor(): d.randomcolor() d.dot(10)
游戲效果
7.彩點實驗拋物線
游戲源碼
''' 7.彩點實驗拋物線 ''' from sprites import * d = Sprite(shape='blank',visible=False) d.screen.bgcolor('black') while True: d.randompos() if d.ycor() > 0.5 * d.xcor() * d.xcor() /100: d.randomcolor() d.dot(10)
游戲效果
8.彩點實驗橢圓形
游戲源碼
''' 8.彩點實驗橢圓形 ''' from sprites import * d = Sprite(shape='blank',visible=False) d.screen.bgcolor('black') dot1 = (-100,0) dot2 = (100,0) while True: d.randompos() d1 = d.distance(dot1) d2 = d.distance(dot2) if d1 + d2 < 300: d.randomcolor() d.dot(10)
游戲效果
9.旋轉文字
游戲源碼
from sprites import * # 新建屏幕 screen = Screen() # 設定背景顏色 screen.bgcolor('dodger blue') # 設定標題 screen.title('旋轉的文字') # 新建不可見的角色 t=Sprite(visible=False) # 設定角色為白色 t.color('white') # a是一個全局變量,這里代表角度 a= 0 # 要旋轉的文字 info = '大海老師大帥比' # 定義字體樣式 ft = ('黑體',32,'normal') # 定義rotate函數(shù) def rotate(): # 申明a為全局變量 global a # 清除以前所寫內容 t.clear() t.write(info,align='center',font=ft,angle=a) a = a + 10 screen.ontimer(rotate,50) rotate() screen.mainloop()
游戲效果
10.迷宮游戲
游戲源碼
""" 大迷宮游戲.py 小老鼠在一個偌大的迷宮中迷失了方向,請按上下左右方向箭頭操作它移動。 找到出口,走出迷宮。 本程序需要python精靈模塊1.35版本以上支持。 安裝最新版本請用cmd打開命令提示符管理員窗口輸入以下命令: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sprites --upgrade 本程序主要演示的像素級碰撞檢測命令pixelcollide。它返回碰撞點坐標, 主碰方(在這里是老鼠)碰撞點的像素值,被碰方(這時是迷宮),重疊區(qū)域矩形。就像下面這樣: ((14.0, 3.0), (45, 45, 45), (104, 104, 104), (-17.0, 9.0, 18.0, -9.0, 630.0)) 最后重疊區(qū)域是一個五元組,這個五元組最后一個值是重疊區(qū)域的面積。 問題是,小老鼠最后找到了綠色的出口,可怎么也無法進門! 當你理解了pixelcollide的返回值后,相信你能對此程序進行修改,從而解決小老鼠的問題。 """ from sprites import * # 從精靈模塊導入所有命令 from pygame import mixer # 從pygame模塊導入混音器 screen = Screen() screen.setup(480,360) screen.title('大迷宮游戲') mixer.init() mixer.music.load('胡偉立 - 周旋過場.mp3') mixer.music.play(-1,0) # 從頭開始循環(huán)播放 maze = Sprite('1.png') # 新建迷宮 maze.scale(2) # 迷宮長寬擴大為原來2倍 maze.ondrag(None) # 讓迷宮不可拖動,(maze.draggable()能讓它重新可拖動) # 新建有兩個造型的老鼠角色 rat = Sprite(['res/rat1.png','res/rat2.png']) rat.shapesize(0.5,0.6) leftkey = Key("Left") # 向左方向箭頭 rightkey = Key("Right") # 向右方向箭頭 upkey = Key("Up") # 向上方向箭頭 downkey = Key("Down") # 向下方向箭頭 screen.listen() # 監(jiān)聽屏幕按鍵 r = None while True: if leftkey.down(): # 按左方向箭頭往左移 rat.nextcostume() rat.setheading(180) maze.addx(5) r = rat.pixelcollide(maze) if r:maze.addx(-5) if rightkey.down(): # 按右方向箭頭往左移 rat.nextcostume() rat.setheading(0) maze.addx(-5) r = rat.pixelcollide(maze) if r:maze.addx(5) if upkey.down(): # 按上方向箭頭往左移 rat.nextcostume() rat.setheading(90) maze.addy(-5) r = rat.pixelcollide(maze) if r:maze.addy(5) if downkey.down(): # 按下方向箭頭往左移 rat.nextcostume() rat.setheading(-90) maze.addy(5) r = rat.pixelcollide(maze) # 對rat和maze進行像素極碰撞檢測 if r:maze.addy(-5) if r: print(r) screen.update()
游戲效果
以上就是十個Python經(jīng)典小游戲的代碼合集的詳細內容,更多關于Python游戲合集的資料請關注腳本之家其它相關文章!
相關文章
Python實現(xiàn)刪除時保留特定文件夾和文件的示例
下面小編就為大家分享一篇Python實現(xiàn)刪除時保留特定文件夾和文件的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04