您好,登錄后才能下訂單哦!
這篇文章主要介紹了python如何實現吃蘋果小游戲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
本文實例為大家分享了python實現吃蘋果小游戲的具體代碼,具體內容如下
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() 函數的作用是在新式類中返回屬性值。 語法 以下是 property() 方法的語法: class property([fget[, fset[, fdel[, doc]]]]) 參數 fget -- 獲取屬性值的函數 fset -- 設置屬性值的函數 fdel -- 刪除屬性值函數 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 #僅當更改時才創建幀 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.首先生成隨機蘋果,然后監聽鍵盤移動,播放動畫。精靈和蘋果碰撞檢測,檢測是吃掉蘋果
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() #創建精靈組 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: # 根據不同的方向,角色移動不同的動畫幀 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()
感謝你能夠認真閱讀完這篇文章,希望小編分享的“python如何實現吃蘋果小游戲”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。