有很多 Python 新手留言問:“Python 入門很久了,但項目經驗很少,有沒有什麼項目,可以讓自己實踐一下呢?”
這是個很普遍的問題,今天呢就給大家分享五個适合新手練手的趣味遊戲項目
一、Python實現“小兔子和Bun”遊戲準備工作:安裝Python
python版本:3.5.4
pygame版本:1.9.3(pip安裝即可)
效果
部分源碼
'''遊戲初始化'''
definitGame():
#初始化pygame,設置展示窗口
pygame.init()
pygame.mixer.init()
screen=pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('BunniesandBadgers——Charles的皮卡丘')
#加載必要的遊戲素材
game_images={}
forkey,valueincfg.IMAGE_PATHS.items():
game_images[key]=pygame.image.load(value)
game_sounds={}
forkey,valueincfg.SOUNDS_PATHS.items():
ifkey!='moonlight':
game_sounds[key]=pygame.mixer.Sound(value)
returnscreen,game_images,game_sounds
'''主函數'''
defmain():
#初始化
screen,game_images,game_sounds=initGame()
#播放背景音樂
pygame.mixer.music.load(cfg.SOUNDS_PATHS['moonlight'])
pygame.mixer.music.play(-1,0.0)
#字體加載
font=pygame.font.Font(None,24)
#定義兔子
bunny=BunnySprite(image=game_images.get('rabbit'),position=(100,100))
#跟蹤玩家的精度變量,記錄了射出的箭頭數和被擊中的獾的數量.
acc_record=[0.,0.]
#生命值
healthvalue=194
#弓箭
arrow_sprites_group=pygame.sprite.Group()
#獾
badguy_sprites_group=pygame.sprite.Group()
badguy=BadguySprite(game_images.get('badguy'),position=(640,100))
badguy_sprites_group.add(badguy)
#定義了一個定時器,使得遊戲裡經過一段時間後就新建一支獾
badtimer=100
badtimer1=0
#遊戲主循環,running變量會跟蹤遊戲是否結束,exitcode變量會跟蹤玩家是否勝利.
running,exitcode=True,False
clock=pygame.time.Clock()
whilerunning:
#--在給屏幕畫任何東西之前用黑色進行填充
screen.fill(0)
#--添加的風景也需要畫在屏幕上
forxinrange(cfg.SCREENSIZE[0]//game_images['grass'].get_width() 1):
foryinrange(cfg.SCREENSIZE[1]//game_images['grass'].get_height() 1):
screen.blit(game_images['grass'],(x*100,y*100))
foriinrange(4):screen.blit(game_images['castle'],(0,30 105*i))
#--倒計時信息
countdown_text=font.render(str((90000-pygame.time.get_ticks())//60000) ":" str((90000-pygame.time.get_ticks())//1000`).zfill(2),True,(0,0,0))
countdown_rect=countdown_text.get_rect()
countdown_rect.topright=[635,5]
screen.blit(countdown_text,countdown_rect)
#--按鍵檢測
#----退出與射擊
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
pygame.quit()
sys.exit()
elifevent.type==pygame.MOUSEBUTTONDOWN:
game_sounds['shoot'].play()
acc_record[1] =1
mouse_pos=pygame.mouse.get_pos()
angle=math.atan2(mouse_pos[1]-(bunny.rotated_position[1] 32),mouse_pos[0]-(bunny.rotated_position[0] 26))
arrow=ArrowSprite(game_images.get('arrow'),(angle,bunny.rotated_position[0] 32,bunny.rotated_position[1] 26))
arrow_sprites_group.add(arrow)
#----移動兔子
key_pressed=pygame.key.get_pressed()
ifkey_pressed[pygame.K_w]:
bunny.move(cfg.SCREENSIZE,'up')
elifkey_pressed[pygame.K_s]:
bunny.move(cfg.SCREENSIZE,'down')
elifkey_pressed[pygame.K_a]:
bunny.move(cfg.SCREENSIZE,'left')
elifkey_pressed[pygame.K_d]:
bunny.move(cfg.SCREENSIZE,'right')
#--更新弓箭
forarrowinarrow_sprites_group:
ifarrow.update(cfg.SCREENSIZE):
arrow_sprites_group.remove(arrow)
#--更新獾
ifbadtimer==0:
badguy=BadguySprite(game_images.get('badguy'),position=(640,random.randint(50,430)))
badguy_sprites_group.add(badguy)
badtimer=100-(badtimer1*2)
badtimer1=20ifbadtimer1>=20elsebadtimer1 2
badtimer-=1
forbadguyinbadguy_sprites_group:
ifbadguy.update():
game_sounds['hit'].play()
healthvalue-=random.randint(4,8)
badguy_sprites_group.remove(badguy)
#--碰撞檢測
forarrowinarrow_sprites_group:
forbadguyinbadguy_sprites_group:
ifpygame.sprite.collide_mask(arrow,badguy):
game_sounds['enemy'].play()
arrow_sprites_group.remove(arrow)
badguy_sprites_group.remove(badguy)
acc_record[0] =1
#--畫出弓箭
arrow_sprites_group.draw(screen)
#--畫出獾
badguy_sprites_group.draw(screen)
#--畫出兔子
bunny.draw(screen,pygame.mouse.get_pos())
#--畫出城堡健康值,首先畫了一個全紅色的生命值條,然後根據城堡的生命值往生命條裡面添加綠色.
screen.blit(game_images.get('healthbar'),(5,5))
foriinrange(healthvalue):
screen.blit(game_images.get('health'),(i 8,8))
#--判斷遊戲是否結束
ifpygame.time.get_ticks()>=90000:
running,exitcode=False,True
ifhealthvalue<=0:
running,exitcode=False,False
#--更新屏幕
pygame.display.flip()
clock.tick(cfg.FPS)
#計算準确率
accuracy=acc_record[0]/acc_record[1]*100ifacc_record[1]>0else0
accuracy='%.2f'%accuracy
showEndGameInterface(screen,exitcode,accuracy,game_images)
開發工具
Python版本:3.6.4
相關模塊:
cocos2d模塊;
pyaudio模塊;
以及一些Python自帶的模塊。
相信很多人對八音符這款遊戲并不陌生吧,其核心玩法是利用聲音控制一個帶辮子的小黑球不斷前進,大概是長這樣子的吧:
遊戲主要使用了cocos2d模塊和pyaudio模塊,前者用于搭建遊戲框架,後者用于獲得麥克風的聲音。
部分源碼
'''定義聲控遊戲類'''
classVCGame(cocos.layer.ColorLayer):
def__init__(self):
super(VCGame,self).__init__(255,255,255,255,800,600)
#frames_per_buffer
self.num_samples=1000
#聲控條
self.vbar=Sprite(cfg.BLOCK_IMAGE_PATH)
self.vbar.position=20,450
self.vbar.scale_y=0.1
self.vbar.image_anchor=0,0
self.add(self.vbar)
#皮卡丘
self.pikachu=Pikachu(cfg.PIKACHU_IMAGE_PATH)
self.add(self.pikachu)
#地面
self.floor=cocos.cocosnode.CocosNode()
self.add(self.floor)
position=0,100
foriinrange(120):
b=Block(cfg.BLOCK_IMAGE_PATH,position)
self.floor.add(b)
position=b.x b.width,b.height
#聲音輸入
audio=PyAudio()
self.stream=audio.open(format=paInt16,channels=1,rate=int(audio.get_device_info_by_index(0)['defaultSampleRate']),input=True,frames_per_buffer=self.num_samples)
#屏幕更新
self.schedule(self.update)
'''碰撞檢測'''
defcollide(self):
diffx=self.pikachu.x-self.floor.x
forbinself.floor.get_children():
if(b.x<=diffx self.pikachu.width*0.8)and(diffx self.pikachu.width*0.2<=b.x b.width):
ifself.pikachu.y<b.height:
self.pikachu.land(b.height)
break
'''定義遊戲規則'''
defupdate(self,dt):
#獲取每幀的音量
audio_data=self.stream.read(self.num_samples)
k=max(struct.unpack('1000h',audio_data))
self.vbar.scale_x=k/10000.0
ifk>3000:
self.floor.x-=min((k/20.0),150)*dt
#皮卡丘跳躍
ifk>8000:
self.pikachu.jump((k-8000)/1000.0)
#碰撞檢測
self.collide()
'''重置'''
defreset(self):
self.floor.x=0
'''run'''
if__name__=='__main__':
cocos.director.director.init(caption="PikachuGoGoGo")
cocos.director.director.run(cocos.scene.Scene(VCGame()))
遊戲簡介:
将圖像分為m×n個矩形塊,并将圖像右下角的矩形塊替換為空白塊後,将這些矩形塊随機擺放成原圖像的形狀。遊戲目标為通過移動非空白塊将随機擺放獲得的圖像恢複成原圖像的模樣,且規定移動操作僅存在于非空白塊移動到空白塊。
例如下圖所示:
部分源碼
while True:
game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells)
if not isGameOver(game_board, size):
break
# 遊戲主循環
is_running = True
while is_running:
# --事件捕獲
for event in pygame.event.get():
# ----退出遊戲
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
# ----鍵盤操作
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)
elif event.key == pygame.K_RIGHT or event.key == ord('d'):
blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)
elif event.key == pygame.K_UP or event.key == ord('w'):
blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)
elif event.key == pygame.K_DOWN or event.key == ord('s'):
blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)
# ----鼠标操作
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = pygame.mouse.get_pos()
x_pos = x // cell_width
y_pos = y // cell_height
idx = x_pos y_pos * num_cols
if idx == blank_cell_idx-1:
blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)
elif idx == blank_cell_idx 1:
blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)
elif idx == blank_cell_idx num_cols:
blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)
elif idx == blank_cell_idx-num_cols:
blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)
# --判斷遊戲是否結束
if isGameOver(game_board, size):
game_board[blank_cell_idx] = num_cells - 1
is_running = False
# --更新屏幕
screen.fill(cfg.BACKGROUNDCOLOR)
for i in range(num_cells):
if game_board[i] == -1:
continue
x_pos = i // num_cols
y_pos = i % num_cols
rect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height)
img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height)
screen.blit(game_img_used, rect, img_area)
for i in range(num_cols 1):
pygame.draw.line(screen, cfg.BLACK, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height))
for i in range(num_rows 1):
pygame.draw.line(screen, cfg.BLACK, (0, i*cell_height), (game_img_used_rect.width, i*cell_height))
pygame.display.update()
clock.tick(cfg.FPS)
遊戲規則:
玩家通過“AD”鍵或者“←→”操控前進中的滑雪者,努力避開路上的樹,盡量撿到路上的小旗。
如果碰到樹,則得分減50,如果撿到小旗子,則得分加10。
效果
部分源碼
'''遊戲主循環'''
while True:
# --事件捕獲
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
speed = skier.turn(-1)
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
speed = skier.turn(1)
'''--更新當前遊戲幀的數據'''
skier.move()
distance = speed[1]
if distance >= 640 and obstaclesflag == 0:
obstaclesflag = 1
obstacles0 = createObstacles(20, 29)
obstacles = AddObstacles(obstacles0, obstacles1)
if distance >= 1280 and obstaclesflag == 1:
obstaclesflag = 0
distance -= 1280
for obstacle in obstacles0:
obstacle.location[1] = obstacle.location[1] - 1280
obstacles1 = createObstacles(10, 19)
obstacles = AddObstacles(obstacles0, obstacles1)
for obstacle in obstacles:
obstacle.move(distance)
'''--碰撞檢測'''
hitted_obstacles = pygame.sprite.spritecollide(skier, obstacles, False)
if hitted_obstacles:
if hitted_obstacles[0].attribute == "tree" and not hitted_obstacles[0].passed:
score -= 50
skier.setFall()
updateFrame(screen, obstacles, skier, score)
pygame.time.delay(1000)
skier.setForward()
speed = [0, 6]
hitted_obstacles[0].passed = True
elif hitted_obstacles[0].attribute == "flag" and not hitted_obstacles[0].passed:
score = 10
obstacles.remove(hitted_obstacles[0])
'''--更新屏幕'''
updateFrame(screen, obstacles, skier, score)
clock.tick(cfg.FPS)
遊戲規則:
遊戲有單人和雙人兩種模式,己方大本營被破或者己方坦克被殲滅則遊戲失敗,成功通過所有關卡則遊戲勝利。另外,玩家可以通過射擊特定的坦克使地圖上随機出現一個道具,若己方坦克撿到該道具,則觸發一個事件,例如坦克能力的增強。
效果圖
部分源碼
'''主函數'''
def main(cfg):
# 遊戲初始化
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((cfg.WIDTH, cfg.HEIGHT))
pygame.display.set_caption(cfg.TITLE)
# 加載遊戲素材
sounds = {}
for key, value in cfg.AUDIO_PATHS.items():
sounds[key] = pygame.mixer.Sound(value)
sounds[key].set_volume(1)
# 開始界面
is_dual_mode = gameStartInterface(screen, cfg)
# 關卡數
levelfilepaths = [os.path.join(cfg.LEVELFILEDIR, filename) for filename in sorted(os.listdir(cfg.LEVELFILEDIR))]
# 主循環
for idx, levelfilepath in enumerate(levelfilepaths):
switchLevelIterface(screen, cfg, idx 1)
game_level = GameLevel(idx 1, levelfilepath, sounds, is_dual_mode, cfg)
is_win = game_level.start(screen)
if not is_win: break
is_quit_game = gameEndIterface(screen, cfg, is_win)
return is_quit_game
'''run'''
if __name__ == '__main__':
while True:
is_quit_game = main(cfg)
if is_quit_game:
break
以上這五款遊戲都是我在學習的過程中完成的,我們可以發現 Python 是一種非常實用的編程語言,他可以開發遊戲以外,還可以适用于開發各種類型和規模的應用程序。此外,Python 配置的程序包對于開發者來說價值巨大,能夠極大地簡化開發過程。
如果有正在跟我一樣的自學的朋友,需要我本篇的代碼或者其他的Python學習資料可以私信我(轉發此文私信發我“經典遊戲”)
最後,我想說的是,Python 的應用潛力無限,你唯一缺少的就是找準适當的創意。
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!