《极速切水果游戏》有Python版了,曾风靡一时的手游能否富过“二代”?
前言
大家好!我是梨子同學!
希望大家多多支持我!哈哈
為了感謝每一個關注我的小可愛:💓每篇文章的項目源碼都是無償分享滴💓👇👇👇
點這里藍色這行字體自取,需要什么源碼記得說標題名字哈!私信我也可!
小編也一直在學習編程,如果代碼小程序出現(xiàn)錯誤歡迎大家評論區(qū)留言哈!
最后——如果文章有幫助到你,記得“關注”、“點贊”、“評論”三連哦~
正文
隨著冬季到來,又到了一年中采摘草莓的最佳季節(jié)。
小編在周末的時候已經去過幾次拉,很nice 哈哈哈.jpg 但是草莓園的話小伙伴兒們需要好好選擇
拉,之前還踩了坑的。說到草莓🍓的話今天小編就給大家制作一款切水果小游戲!
環(huán)境安裝——
1)準備好相應的識別的圖片,這里是隨機到網上尋找的素材圖片!
2)環(huán)境安裝準備好Python版本基本上都可以、小編用的Python3.7、Pycharm2021的,游戲模塊
Pygame,然后一些自帶的不用管 直接導入即可!
安裝模塊也就是第三方模塊的小編經常用的方法是:pip install +模塊名或者提速需要用到鏡像源,
百度下或者csdn搜下就會出來很多安裝模塊的鏡像源這里就不一一介紹了!
一、準備中
1)素材背景
這塊兒的話就是整體的界面小程序的背景圖,大家可以自己找的這邊小編找的如下👇
還有各種水果的圖片素材,大家自由尋找即可!
完整的源碼:
import pygame, sys import os import randomplayer_lives = 3 # 生命 score = 0 # 得分 fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb'] # 水果和炸彈# 游戲窗口 WIDTH = 800 HEIGHT = 500 FPS = 12 # gameDisplay的幀率,1/12秒刷新一次 pygame.init() pygame.display.set_caption('極速切水果小游戲') # 標題 gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 游戲窗口 clock = pygame.time.Clock()# 用到的顏色 WHITE = (255,255,255) BLACK = (0,0,0) RED = (255,0,0) GREEN = (0,255,0) BLUE = (0,0,255)background = pygame.image.load('背景圖/03.png') # 背景 font = pygame.font.Font(os.path.join(os.getcwd(), '字體/comic.ttf'), 42) # 字體 score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分的字體樣式# 隨機生成水果的位置與數(shù)據(jù)存放 def generate_random_fruits(fruit):fruit_path = "images/" + fruit + ".png"data[fruit] = {'img': pygame.image.load(fruit_path),'x' : random.randint(100,500), # 水果在x坐標軸上的位置'y' : 800,'speed_x': random.randint(-10,10), # 水果在x方向時的速度和對角線移動'speed_y': random.randint(-80, -60), # y方向時的速度'throw': False, # 如果生成水果的位置在gameDisplay之外,將被丟棄't': 0, 'hit': False,}if random.random() >= 0.75: # 返回在[0.0, 1.0]范圍內的下一個隨機浮點數(shù),以保持水果在游戲中的顯示。data[fruit]['throw'] = Trueelse:data[fruit]['throw'] = False# 用一個字典來存放水果的數(shù)據(jù) data = {} for fruit in fruits:generate_random_fruits(fruit)def hide_cross_lives(x, y):gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))# 在屏幕中繪制字體 font_name = pygame.font.match_font('comic.ttf') def draw_text(display, text, size, x, y):font = pygame.font.Font(font_name, size)text_surface = font.render(text, True, WHITE)text_rect = text_surface.get_rect()text_rect.midtop = (x, y)gameDisplay.blit(text_surface, text_rect)# 繪制玩家的生命 def draw_lives(display, x, y, lives, image) :for i in range(lives) :img = pygame.image.load(image)img_rect = img.get_rect() img_rect.x = int(x + 35 * i) img_rect.y = y display.blit(img, img_rect)# 游戲開始與結束畫面 def show_gameover_screen():gameDisplay.blit(background, (0,0))draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)if not game_over :draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)draw_text(gameDisplay, "Press any key to start the game", 64, WIDTH / 2, HEIGHT * 3 / 4)pygame.display.flip()waiting = Truewhile waiting:clock.tick(FPS)for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()if event.type == pygame.KEYUP:waiting = False# 游戲主循環(huán) first_round = True game_over = True # 超過3個炸彈,終止游戲循環(huán) game_running = True # 管理游戲循環(huán) while game_running :if game_over :if first_round :show_gameover_screen()first_round = Falsegame_over = Falseplayer_lives = 3draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')score = 0for event in pygame.event.get():# 檢查是否關閉窗口if event.type == pygame.QUIT:game_running = FalsegameDisplay.blit(background, (0, 0))gameDisplay.blit(score_text, (0, 0))draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')for key, value in data.items():if value['throw']:value['x'] += value['speed_x'] # x方向上移動水果value['y'] += value['speed_y'] # y方向上移動 value['speed_y'] += (1 * value['t']) # 遞增value['t'] += 1 if value['y'] <= 800:gameDisplay.blit(value['img'], (value['x'], value['y'])) # 動態(tài)顯示水果else:generate_random_fruits(key)current_position = pygame.mouse.get_pos() # 獲取鼠標的位置,單位為像素if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \and current_position[1] > value['y'] and current_position[1] < value['y']+60:if key == 'bomb':player_lives -= 1if player_lives == 0:hide_cross_lives(690, 15)elif player_lives == 1 :hide_cross_lives(725, 15)elif player_lives == 2 :hide_cross_lives(760, 15)# 超過3次炸彈,提示游戲結束,重置窗口if player_lives < 0 :show_gameover_screen()game_over = Truehalf_fruit_path = "images/explosion.png"else:half_fruit_path = "images/" + "half_" + key + ".png"value['img'] = pygame.image.load(half_fruit_path)value['speed_x'] += 10if key != 'bomb' :score += 1score_text = font.render('Score : ' + str(score), True, (255, 255, 255))value['hit'] = Trueelse:generate_random_fruits(key)pygame.display.update()clock.tick(FPS) pygame.quit()效果展示:
游戲規(guī)則:相應的水果鼠標點擊即可加分,點到雷即消失一個生命值,總三個生命值消失級游戲結
束!
游戲界面——
?
?游戲運行中——
?點到雷了——
?
?游戲結束——
?總結
好啦!《極速切水果🍓小游戲》就到這里結束拉,老規(guī)矩源碼私信我即可拉!
關注小編獲取更多精彩內容!
?制作不易,記得一鍵三連哦!!?如需打包好的源碼+素材免費分享滴!!傳送門
總結
以上是生活随笔為你收集整理的《极速切水果游戏》有Python版了,曾风靡一时的手游能否富过“二代”?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【文字识别小程序】快速识别文字,一款用了
- 下一篇: 【颜值打分小程序】最火爆的“颜值测试”,