主頁(yè) > 知識(shí)庫(kù) > 基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲(源代碼)

基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲(源代碼)

熱門(mén)標(biāo)簽:柳州正規(guī)電銷(xiāo)機(jī)器人收費(fèi) 高德地圖標(biāo)注字母 400電話辦理費(fèi)用收費(fèi) 外呼系統(tǒng)前面有錄音播放嗎 千呼ai電話機(jī)器人免費(fèi) 騰訊地圖標(biāo)注有什么版本 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商 申請(qǐng)辦個(gè)400電話號(hào)碼

源碼及注釋?zhuān)?/p>

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}

# 定義畫(huà)面幀率
FRAME_RATE = 60

# 定義動(dòng)畫(huà)周期(幀數(shù))
ANIMATE_CYCLE = 30

ticks = 0
clock = pygame.time.Clock()


# 猴子類(lèi)
class Monkey(pygame.sprite.Sprite):
 # 蘋(píng)果的數(shù)量
 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

 # 控制猴子的移動(dòng)
 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

 # 接蘋(píng)果
 def picking_apple(self, app_group):

  # 判斷接到幾個(gè)蘋(píng)果
  picked_apples = pygame.sprite.spritecollide(self, app_group, True)

  # 添加分?jǐn)?shù)
  self.apple_num += len(picked_apples)

  # 接到的蘋(píng)果消失
  for picked_apple in picked_apples:
   picked_apple.kill()


# 蘋(píng)果類(lèi)
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:
   # 蘋(píng)果落地游戲結(jié)束
   global OVER_FLAG
   OVER_FLAG = True
   self.kill()


# 初始化游戲
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("猴子接蘋(píng)果")

# 載入圖片
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()

# 創(chuàng)建猴子
monkey = Monkey(monkey_surface, (200, 500))

# 創(chuàng)建蘋(píng)果組
apple_group = pygame.sprite.Group()

# 分?jǐn)?shù)字體
score_font = pygame.font.SysFont("arial", 40)

# 主循環(huán)
while True:

 if OVER_FLAG:
  break

 # 控制游戲最大幀率
 clock.tick(FRAME_RATE)

 # 繪制背景
 screen.blit(background_surface, (0, 0))

 if ticks >= ANIMATE_CYCLE:
  ticks = 0

 # 產(chǎn)生蘋(píng)果
 if ticks % 30 == 0:
  apple = Apple(apple_surface,
      [randint(0, SCREEN_WIDTH - apple_surface.get_width()), -apple_surface.get_height()])
  apple_group.add(apple)

 # 控制蘋(píng)果
 apple_group.update()

 # 繪制蘋(píng)果組
 apple_group.draw(screen)

 # 繪制猴子
 screen.blit(monkey_surface, monkey.rect)
 ticks += 1

 # 接蘋(píng)果
 monkey.picking_apple(apple_group)

 # 更新分?jǐn)?shù)
 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

 # 移動(dòng)猴子
 if JUMP_STATUS:
  offset[pygame.K_DOWN] = 5
  offset[pygame.K_UP] = 0
 monkey.move(offset)

# 游戲結(jié)束推出界面
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()

食用指南: 使用的圖片

monkey.png:

background.jpg:

apple.png:

這是我的文件目錄,學(xué)習(xí)者也可改為自己的:

更改的代碼位置:

root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \

   + "resource\\images\\"

游戲截圖:

到此這篇關(guān)于基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲的文章就介紹到這了,更多相關(guān)Python 猴子摘桃小游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python實(shí)現(xiàn)滑雪者小游戲
  • 教你用Python實(shí)現(xiàn)一個(gè)輪盤(pán)抽獎(jiǎng)小游戲
  • python實(shí)現(xiàn)21點(diǎn)小游戲
  • 教你用Python寫(xiě)一個(gè)植物大戰(zhàn)僵尸小游戲
  • 教你如何用python開(kāi)發(fā)一款數(shù)字推盤(pán)小游戲
  • python反編譯教程之2048小游戲?qū)嵗?/li>
  • Python實(shí)現(xiàn)我的世界小游戲源代碼
  • Python實(shí)現(xiàn)簡(jiǎn)單的2048小游戲
  • Python約瑟夫生者死者小游戲?qū)嵗v解
  • 用Python實(shí)現(xiàn)童年貪吃蛇小游戲功能的實(shí)例代碼
  • 利用python制作拼圖小游戲的全過(guò)程
  • 利用python如何實(shí)現(xiàn)貓捉老鼠小游戲
  • 學(xué)會(huì)用Python實(shí)現(xiàn)滑雪小游戲,再也不用去北海道啦

標(biāo)簽:海南 烏蘭察布 平頂山 哈爾濱 郴州 合肥 烏蘭察布 大慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲(源代碼)》,本文關(guān)鍵詞  基于,Python-Pycharm,實(shí)現(xiàn),的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲(源代碼)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于基于Python-Pycharm實(shí)現(xiàn)的猴子摘桃小游戲(源代碼)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章