最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)吃蘋果小游戲

 更新時間:2020年03月21日 15:55:07   作者:心心強  
這篇文章主要為大家詳細介紹了python實現(xiàn)吃蘋果小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)吃蘋果小游戲的具體代碼,供大家參考,具體內(nèi)容如下

1.公共類模塊

import pygame
from pygame.rect import Rect
 
 
def print_text(font, x, y, text, color=(255, 255, 255)):
 imgText=font.render(text, True, color)
 screen=pygame.display.get_surface()
 screen.blit(imgText,(x, y))
 
class MySprite(pygame.sprite.Sprite):
 def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.master_image=None
  self.frame = 0
  self.old_frame = -1
  self.frame_width = 1
  self.frame_height = 1
  self.first_frame = 0
  self.last_frame = 0
  self.columns = 0
  self.last_time = 1
  self.direction = 0
  self.velocity = 0
 
 def _getx(self): return self.rect.x
 def _gety(self): return self.rect.y
 
 def _setx(self, value): self.rect.x = value
 def _sety(self, value): self.rect.y = value
 """
 描述
 property() 函數(shù)的作用是在新式類中返回屬性值。
 
 語法
 以下是 property() 方法的語法:
 
 class property([fget[, fset[, fdel[, doc]]]])
 參數(shù)
 fget -- 獲取屬性值的函數(shù)
 fset -- 設(shè)置屬性值的函數(shù)
 fdel -- 刪除屬性值函數(shù)
 doc -- 屬性描述信息
 返回值
 返回新式類屬性。
 """
 X = property(_getx, _setx)
 Y = property(_gety, _sety)
 
 #位置屬性
 def _getpos(self): return self.rect.topleft
 def _setpos(self, pos): self.rect.topleft = pos
 position = property(_getpos, _setpos)
 
 def load(self, filename, width, height, columns):
  self.master_image=pygame.image.load(filename).convert_alpha()
  self.frame_height = height
  self.frame_width = width
  self.rect = Rect(0, 0, width, height)
  self.columns = columns
 
  rect = self.master_image.get_rect()
  self.last_frame = (rect.width//width)*(rect.height//height) - 1
 
 def update(self, current_time, rate=30):
  #跟新動畫幀
  if current_time > self.last_time + rate:
   self.frame += 1
   if self.frame > self.last_frame:
    self.frame = self.first_frame
   self.last_time = current_time
  #僅當(dāng)更改時才創(chuàng)建幀
  if self.frame != self.old_frame:
   frame_x = (self.frame % self.columns) * self.frame_width
   frame_y = (self.frame // self.columns) * self.frame_height
   rect=Rect(frame_x, frame_y, self.frame_width, self.frame_height)
   self.image = self.master_image.subsurface(rect)
   self.old_frame = self.frame
class Point(object):
 def __init__(self, x, y):
  self.x = x
  self.y = y
 def getx(self): return self.x
 def gety(self): return self.y
 def setx(self,value): self.x=value
 def sety(self,value): self.y=value
 
 x=property(getx,setx)
 y=property(gety,sety)
 
 def __str__(self):
  return 'x:'+"{:.0f}".format(self.x) + 'y:'+"{:.0f}".format(self.y)

2.首先生成隨機蘋果,然后監(jiān)聽鍵盤移動,播放動畫。精靈和蘋果碰撞檢測,檢測是吃掉蘋果

import pygame
from pygame.rect import Rect
 
from . import MyLibrary
import random
import sys
 
 
def calc_velocity(direction, vel=1.0):
 velocity = MyLibrary.Point(0, 0)
 if direction == 0:#上
  velocity.y = -vel
 elif direction == 2:#右
  velocity.x = vel
 elif direction == 4:#下
  velocity.y == vel
 elif direction == 6:#左
  velocity.x == -vel
 return velocity
 
pygame.init()
screen = pygame.display.set_mode(800,600)
font=pygame.font.Font(None, 36)
timer=pygame.time.Clock()
 
#創(chuàng)建精靈組
player_group=pygame.sprite.Group()
food_group=pygame.sprite.Group()
 
#初始化玩家精靈組
player=MyLibrary.MySprite()
player.load('farmer.png',96,96,8)############-----------------圖片
player.position=80, 80
player.direction= 4
player_group.add(player)
 
#初始化food精靈組
for n in range(1, 50):
 food= MyLibrary.MySprite()
 food.load('food.png',35,35,1) #################-----------食物圖片
 food.position=random.randint(0, 780), random.randint(0, 580)
 food_group.add(food)
 
game_over= False
player_moving = False
plyer_health = False
 
while True:
 timer.tick(30)
 ticks = pygame.time.get_ticks()
 
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   pygame.quit()
   sys.exit()
 keys = pygame.key.get_pressed()
 if keys[pygame.key.K_UP]:
  player.direction=0
  player_moving = True
 elif keys[pygame.key.k_DOWN]:
  player_moving = True
  player.direction=4
 elif keys[pygame.key.K_RIGHT]:
  player.direction = 2
  player_moving = True
 elif keys[pygame.key.K_LEFT]:
  player.direction = 6
  player_moving = True
 else:
  player_moving = False
 
 if not game_over:
  # 根據(jù)不同的方向,角色移動不同的動畫幀
  player.first_frame = player.direction*player.columns
  player.last_time = player.first_frame + player.columns -1
  if player.frame < player.first_frame:
   player.frame = player.first_frame
 
  if not player_moving:
   #停止更新動畫幀
   player.frame = player.first_frame=player.last_frame
  else:
   player.velocity = calc_velocity(player.direction,1.5)
   player.velocity.x*=1.5
   player.velocity.y*=1.5
 
  #跟新玩家精靈組
  player_group.update(ticks,50)
 
  #移動玩家
  if player_moving:
   player.X += player.velocity.x
   player.Y += player.velocity.y
   if player.X < 0: player.X = 0
   elif player.X >700: player.X =700
   if player.Y < 0: player.Y = 0
   elif player.Y >500:player.Y =500
 
  #檢測玩家是否與食物沖突,是否吃到蘋果
  attacker = None
  attacker = pygame.sprite.spritecollideany(player,food_group)
  if attacker != None:
   if pygame.sprite.collide_circle_ratio(0.65)(player,food_group):
    plyer_health += 2
    food_group.remove(attacker)
  if plyer_health > 100:
   plyer_health=100
   #跟新食物精靈組
   food_group.update(ticks, 50)
   if food_group.__len__() == 0:
    game_over = True
 
  screen.fill((50,50,100))
  food_group.draw(screen)
  player_group.draw(screen)
 
  pygame.draw.rect(screen,(510,150,50,180),Rect(300,570,plyer_health*2,25))
  pygame.draw.rect(screen, (100, 200, 100, 180), Rect(300, 570, 200, 2))
 
  if game_over:
   MyLibrary.print_text(font, 300, 100,'Game Over!')
  pygame.display.update()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例

    詳解pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例

    這篇文章主要介紹了pandas庫pd.read_excel操作讀取excel文件參數(shù)整理與實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • TensorFlow 多元函數(shù)的極值實例

    TensorFlow 多元函數(shù)的極值實例

    今天小編就為大家分享一篇TensorFlow 多元函數(shù)的極值實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python使用yaml 管理selenium元素的示例

    python使用yaml 管理selenium元素的示例

    這篇文章主要介紹了python使用yaml 管理selenium元素的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-12-12
  • 解決python中導(dǎo)入win32com.client出錯的問題

    解決python中導(dǎo)入win32com.client出錯的問題

    今天小編就為大家分享一篇解決python中導(dǎo)入win32com.client出錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python中使用PDB庫調(diào)試程序

    Python中使用PDB庫調(diào)試程序

    這篇文章主要介紹了Python中使用PDB庫調(diào)試程序,本文講解了使用PDB的二種模式以及PDB模式下的常用調(diào)試命令,需要的朋友可以參考下
    2015-04-04
  • Python返回數(shù)組/List長度的實例

    Python返回數(shù)組/List長度的實例

    今天小編就為大家分享一篇Python返回數(shù)組/List長度的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 基于Django?drf框架序列化視圖

    基于Django?drf框架序列化視圖

    這篇文章主要為大家介紹了基于Django?drf框架序列化視圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 11個并不被常用但對開發(fā)非常有幫助的Python庫

    11個并不被常用但對開發(fā)非常有幫助的Python庫

    這篇文章主要介紹了11個并不被常用但對開發(fā)非常有幫助的Python庫,這些庫大都被放在Github上開源、并且經(jīng)過一段時間的編寫和維護,對Python開發(fā)有一定的幫助,需要的朋友可以參考下
    2015-03-03
  • 詳解Python中4種超參自動優(yōu)化算法的實現(xiàn)

    詳解Python中4種超參自動優(yōu)化算法的實現(xiàn)

    要想模型效果好,每個算法工程師都應(yīng)該了解的流行超參數(shù)調(diào)優(yōu)技術(shù)。今天給大家總結(jié)超參自動優(yōu)化方法:網(wǎng)格搜索、隨機搜索、貝葉斯優(yōu)化?和?Hyperband,感興趣的可以了解一下
    2022-05-05
  • Python sort 自定義函數(shù)排序問題

    Python sort 自定義函數(shù)排序問題

    這篇文章主要介紹了Python sort 自定義函數(shù)排序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論

南阳市| 页游| 宣恩县| 兴城市| 库尔勒市| 商城县| 靖宇县| 广西| 玉环县| 石楼县| 黄山市| 塘沽区| 仪陇县| 来凤县| 板桥市| 米泉市| 比如县| 阜新市| 新源县| 芜湖市| 大竹县| 如东县| 同江市| 巴彦淖尔市| 凤庆县| 华阴市| 奉化市| 三门县| 报价| 上栗县| 邹平县| 道孚县| 平舆县| 宁津县| 宾川县| 沿河| 金坛市| 宜川县| 巢湖市| 赤壁市| 乐平市|