<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
根據神廟逃亡,實現一個人躲避殭屍的小遊戲,主要的是精靈、精靈組之間相撞、相交的處理。
遊戲開始隨機出現一定的殭屍,隨機移動,玩家在一位置上,如果殭屍靠近玩家一定距離,則玩家持續掉血。玩家通過上下左右移動躲避殭屍,螢幕會隨機重新整理一個加血包,玩家吃了就會加一定的血,並在此重新整理血包。
這個函數在類中返回新的屬性
property(get,set,del,doc)
引數如上所示,get、set、del分別是獲取設值刪除呼叫的,doc是描述的。
在原來的精靈類中新增方向和屬性即可。
class MySprite(pygame.sprite.Sprite): def __init__(self, target): 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 = 1 self.last_time = 0 self.direction = 0 self.classification = "玩家" def load(self, filename, width, height, columns, direction, classification="玩家"): # 精靈的屬性 self.classification = classification # 方向 self.direction = direction # 載入圖片 # 780 * 300 self.master_image = pygame.image.load(filename).convert_alpha() # 載入圖片 self.frame_width = width self.frame_height = height 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): # current_time 更新頻率 為30 if current_time > self.last_time + rate: # 如果當前事件 大於 最後的時間 + 當前的節奏 self.frame += 1 # 當前的幀數加一 if self.frame > self.last_frame: # 當前最後一幀 則從第一幀開始 self.frame = self.first_frame # 從0開始 self.last_time = current_time # 將最後幀值為30 # build current frame only if it changed 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 = (frame_x, frame_y, self.frame_width, self.frame_height) # 更新對應的位置 self.image = self.master_image.subsurface(rect) # 迴圈箱已有的方向 self.old_frame = self.frame def __str__(self): return str(self.frame) + "," + str(self.first_frame) + "," + str(self.last_frame) + "," + str(self.frame_width) + "," + str(self.frame_height) + "," + str(self.columns)
和原來一樣,先建立簡單的畫面。
import mySprite1 import pygame, sys, random from pygame.locals import * def print_text(font, x, y, text, color=(255, 255, 255)): imgText = font.render(text, True, color) screen.blit(imgText, (x, y)) # 設定視窗 pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("勇闖後半夜") font = pygame.font.Font(None, 30) timer = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() key = pygame.key.get_pressed() if key[K_ESCAPE]: sys.exit() screen.fill((50, 50, 100)) pygame.display.update()
如果是殭屍遇到牆壁自動反方向走,根據方向改變對應的位置。
def reversal_direction(mySprite): direction = mySprite.direction if direction == 0: direction = 4 elif direction == 2: direction = 6 elif direction == 4: direction = 0 elif direction == 6: direction = 2 mySprite.direction = direction def increment(mySprite, offset=1): # 上下左右 direction = mySprite.direction rect = mySprite.rect if direction == 0: rect.y -= offset elif direction == 2: rect.x += offset elif direction == 4: rect.y += offset elif direction == 6: rect.x -= offset # 超出邊界的處理 # 超出邊界flg boundary = False if rect.x < 0: rect.x = 0 boundary = True if rect.x + mySprite.frame_width > 800: rect.x = 800 - mySprite.frame_width boundary = True if rect.y < 0: rect.y = 0 boundary = True if rect.y + mySprite.frame_height > 600: rect.y = 600 - mySprite.frame_height boundary = True # 如果超出邊界而且是殭屍的話 則反轉方向 if boundary and mySprite.classification == "殭屍": reversal_direction(mySprite)
這個是素材的圖,如上所示,奇數行便是對應的方向移動。檔案大小是768 * 768,可以分為96 * 96的8張。
載入玩家
# 玩家 play_group = pygame.sprite.Group() play = mySprite1() play.load("farmer walk.png", 96, 96, 8) play.direction = -1 play_group.rect.x = 96 play_group.rect.y = 96 play_group.add(play) play_group.update(ticks, 50) screen.fill((50, 50, 100)) play_group.draw(screen) pygame.display.update()
通過改變幀數修改,根據遊戲的結束或是否移動,改變對應的事件
if not game_over: play.first_frame = play.direction * play.columns play.last_frame = play.first_frame + play.columns - 1 if play.frame < play.first_frame: play.frame = play.first_frame if not play_moving: play.frame = play.first_frame = play.last_frame else: increment(play)
控制互動
if key[K_ESCAPE]: sys.exit() elif key[K_UP]: play.direction = 0 play_moving = True elif key[K_DOWN]: play.direction = 4 play_moving = True elif key[K_LEFT]: play.direction = 6 play_moving = True elif key[K_RIGHT]: play.direction = 2 play_moving = True else: play_moving = False
# 隨機生成20個殭屍 for n in range(0, 10): zombie = MySprite() random_direction = random.randint(0, 3) * 2 zombie.load("zombie walk.png", 96, 96, 8, random_direction, "殭屍") zombie.rect.x = random.randint(0, 600) zombie.rect.y = random.randint(0, 500) print(zombie.rect) zombie_group.add(zombie) # 設定殭屍 for z in zombie_group: z.first_frame = z.direction * z.columns z.last_frame = z.first_frame + z.columns - 1 if z.frame < z.first_frame: z.frame = z.first_frame increment(z)
血包就是單純的一個圖的展示。
health = MySprite() health.load("health.png", 32, 32, 1) health.rect.x = random.randint(0, 600) health.rect.y = random.randint(0, 500) health_group.add(health)
主要是殭屍和人 、人和血包之間的相撞事件
# 相撞事件 attack = None attack = pygame.sprite.spritecollideany(play, zombie_group) if attack is not None: if pygame.sprite.collide_rect_ratio(0.5)(play, attack): play_health -= 10 attack.rect.x = random.randint(0, 600) attack.rect.y = random.randint(0, 500) else: attack = None if pygame.sprite.collide_rect_ratio(0.5)(play, health): play_health += 30 if play_health > 100: play_health = 100 health.rect.x = random.randint(0, 600) health.rect.y = random.randint(0, 500) if play_health <= 0: game_over = True # 顯示血量 pygame.draw.rect(screen, (100, 200, 100, 180), Rect(300, 575, 200, 25)) pygame.draw.rect(screen, (50, 150, 150, 180), Rect(300, 575, play_health * 2, 25))
import pygame, sys, random from pygame.locals import * from mySprite1 import * def print_text(font, x, y, text, color=(255, 255, 255)): imgText = font.render(text, True, color) screen.blit(imgText, (x, y)) def reversal_direction(mySprite): direction = mySprite.direction if direction == 0: direction = 4 elif direction == 2: direction = 6 elif direction == 4: direction = 0 elif direction == 6: direction = 2 mySprite.direction = direction def increment(mySprite, offset=1): # 上下左右 direction = mySprite.direction rect = mySprite.rect if direction == 0: rect.y -= offset elif direction == 2: rect.x += offset elif direction == 4: rect.y += offset elif direction == 6: rect.x -= offset # 超出邊界的處理 # 超出邊界flg boundary = False if rect.x < 0: rect.x = 0 boundary = True if rect.x + mySprite.frame_width > 800: rect.x = 800 - mySprite.frame_width boundary = True if rect.y < 0: rect.y = 0 boundary = True if rect.y + mySprite.frame_height > 600: rect.y = 600 - mySprite.frame_height boundary = True # 如果超出邊界而且是殭屍的話 則反轉方向 if boundary and mySprite.classification == "殭屍": reversal_direction(mySprite) # 設定視窗 pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("勇闖後半夜") font = pygame.font.Font(None, 30) timer = pygame.time.Clock() game_over = False # 玩家 play_group = pygame.sprite.Group() play = MySprite() play.load("farmer walk.png", 96, 96, 8, 4) play.rect.x = 96 play.rect.y = 96 play_moving = False play_group.add(play) play_health = 100 # 殭屍 zombie_group = pygame.sprite.Group() # 隨機生成20個殭屍 for n in range(0, 10): zombie = MySprite() random_direction = random.randint(0, 3) * 2 zombie.load("zombie walk.png", 96, 96, 8, random_direction, "殭屍") zombie.rect.x = random.randint(0, 600) zombie.rect.y = random.randint(0, 500) print(zombie.rect) zombie_group.add(zombie) # 血包 health_group = pygame.sprite.Group() health = MySprite() health.load("health.png", 32, 32, 1) health.rect.x = random.randint(0, 600) health.rect.y = random.randint(0, 500) health_group.add(health) while True: # 設定執行的頻率 timer.tick(30) ticks = pygame.time.get_ticks() for event in pygame.event.get(): if event.type == QUIT: sys.exit() key = pygame.key.get_pressed() if key[K_ESCAPE]: sys.exit() elif key[K_UP]: play.direction = 0 play_moving = True elif key[K_DOWN]: play.direction = 4 play_moving = True elif key[K_LEFT]: play.direction = 6 play_moving = True elif key[K_RIGHT]: play.direction = 2 play_moving = True else: play_moving = False if not game_over: # 設定玩家 play.first_frame = play.direction * play.columns play.last_frame = play.first_frame + play.columns - 1 if play.frame < play.first_frame: play.frame = play.first_frame # 設定殭屍 for z in zombie_group: z.first_frame = z.direction * z.columns z.last_frame = z.first_frame + z.columns - 1 if z.frame < z.first_frame: z.frame = z.first_frame increment(z) if not play_moving: play.frame = play.first_frame = play.last_frame else: increment(play) # 相撞事件 attack = None attack = pygame.sprite.spritecollideany(play, zombie_group) if attack is not None: if pygame.sprite.collide_rect_ratio(0.5)(play, attack): play_health -= 10 attack.rect.x = random.randint(0, 600) attack.rect.y = random.randint(0, 500) else: attack = None if pygame.sprite.collide_rect_ratio(0.5)(play, health): play_health += 30 if play_health > 100: play_health = 100 health.rect.x = random.randint(0, 600) health.rect.y = random.randint(0, 500) if play_health <= 0: game_over = True play_group.update(ticks, 50) zombie_group.update(ticks, 50) health_group.update(ticks, 50) screen.fill((50, 50, 100)) play_group.draw(screen) zombie_group.draw(screen) health_group.draw(screen) # 顯示血量 pygame.draw.rect(screen, (100, 200, 100, 180), Rect(300, 575, 200, 25)) pygame.draw.rect(screen, (50, 150, 150, 180), Rect(300, 575, play_health * 2, 25)) if game_over: print_text(font, 300, 100, "GAME OVER!!!") pygame.display.update()
到此這篇關於利用Pygame製作躲避殭屍遊戲的文章就介紹到這了,更多相關Pygame躲避殭屍遊戲內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45