您好,登錄后才能下訂單哦!
怎么在Python中利用Pycharm編寫一個猴子摘桃小游戲?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
源碼及注釋:
import pygame from sys import exit from random import randint import time import os # 定義窗口分辨率 SCREEN_WIDTH = 700 SCREEN_HEIGHT = 600 current_path = os.path.abspath(os.path.dirname(__file__)) root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \ + "resource\\images\\" # 圖片 BACKGROUND_IMAGE_PATH = root_path + "background.jpg" MONKEY_IMAGE_PATH = root_path + "monkey.png" APPLE_IMAGE_PATH = root_path + "apple.png" JUMP_STATUS = False OVER_FLAG = False START_TIME = None offset = {pygame.K_LEFT: 0, pygame.K_RIGHT: 0, pygame.K_UP: 0, pygame.K_DOWN: 0} # 定義畫面幀率 FRAME_RATE = 60 # 定義動畫周期(幀數) ANIMATE_CYCLE = 30 ticks = 0 clock = pygame.time.Clock() # 猴子類 class Monkey(pygame.sprite.Sprite): # 蘋果的數量 apple_num = 0 def __init__(self, mon_surface, monkey_pos): pygame.sprite.Sprite.__init__(self) self.image = mon_surface self.rect = self.image.get_rect() self.rect.topleft = monkey_pos self.speed = 5 # 控制猴子的移動 def move(self, _offset): global JUMP_STATUS x = self.rect.left + _offset[pygame.K_RIGHT] - _offset[pygame.K_LEFT] y = self.rect.top + _offset[pygame.K_DOWN] - _offset[pygame.K_UP] if y < 0: self.rect.top = 0 JUMP_STATUS = True elif y >= SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height JUMP_STATUS = False else: self.rect.top = y JUMP_STATUS = True if x < 0: self.rect.left = 0 elif x > SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left = x # 接蘋果 def picking_apple(self, app_group): # 判斷接到幾個蘋果 picked_apples = pygame.sprite.spritecollide(self, app_group, True) # 添加分數 self.apple_num += len(picked_apples) # 接到的蘋果消失 for picked_apple in picked_apples: picked_apple.kill() # 蘋果類 class Apple(pygame.sprite.Sprite): def __init__(self, app_surface, apple_pos): pygame.sprite.Sprite.__init__(self) self.image = app_surface self.rect = self.image.get_rect() self.rect.topleft = apple_pos self.speed = 1 def update(self): global START_TIME if START_TIME is None: START_TIME = time.time() self.rect.top += (self.speed * (1 + (time.time() - START_TIME) / 40)) if self.rect.top > SCREEN_HEIGHT: # 蘋果落地游戲結束 global OVER_FLAG OVER_FLAG = True self.kill() # 初始化游戲 pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("猴子接蘋果") # 載入圖片 background_surface = pygame.image.load(BACKGROUND_IMAGE_PATH).convert() monkey_surface = pygame.image.load(MONKEY_IMAGE_PATH).convert_alpha() apple_surface = pygame.image.load(APPLE_IMAGE_PATH).convert_alpha() # 創建猴子 monkey = Monkey(monkey_surface, (200, 500)) # 創建蘋果組 apple_group = pygame.sprite.Group() # 分數字體 score_font = pygame.font.SysFont("arial", 40) # 主循環 while True: if OVER_FLAG: break # 控制游戲最大幀率 clock.tick(FRAME_RATE) # 繪制背景 screen.blit(background_surface, (0, 0)) if ticks >= ANIMATE_CYCLE: ticks = 0 # 產生蘋果 if ticks % 30 == 0: apple = Apple(apple_surface, [randint(0, SCREEN_WIDTH - apple_surface.get_width()), -apple_surface.get_height()]) apple_group.add(apple) # 控制蘋果 apple_group.update() # 繪制蘋果組 apple_group.draw(screen) # 繪制猴子 screen.blit(monkey_surface, monkey.rect) ticks += 1 # 接蘋果 monkey.picking_apple(apple_group) # 更新分數 score_surface = score_font.render(str(monkey.apple_num), True, (0, 0, 255)) screen.blit(score_surface, (620, 10)) # 更新屏幕 pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() # 控制方向 if event.type == pygame.KEYDOWN: if event.key in offset: if event.key == pygame.K_UP: offset[event.key] = 80 else: offset[event.key] = monkey.speed elif event.type == pygame.KEYUP: if event.key in offset: offset[event.key] = 0 # 移動猴子 if JUMP_STATUS: offset[pygame.K_DOWN] = 5 offset[pygame.K_UP] = 0 monkey.move(offset) # 游戲結束推出界面 score_surface = score_font.render(str(monkey.apple_num), True, (0, 0, 255)) over_surface = score_font.render(u"Game Over!", True, (0, 0, 255)) screen.blit(background_surface, (0, 0)) screen.blit(score_surface, (620, 10)) screen.blit(over_surface, (250, 270)) while True: pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit()
文件目錄:
更改的代碼位置:
root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \ + "resource\\images\\"
關于怎么在Python中利用Pycharm編寫一個猴子摘桃小游戲問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。