烏龜吃魚遊戲
遊戲規則:
1). 假設遊戲場景爲範圍(x,y)爲0<=x<=10,0<=y<=10
2). 遊戲生成1只烏龜和10條魚, 它們的移動方向均隨機
3). 烏龜的最大移動能力爲2(它能夠隨機選擇1仍是2移動),
魚兒的最大移動能力是1當移動到場景邊緣,自動向反方向移動
4). 烏龜初始化體力爲100(上限), 烏龜每移動一次,體力消耗1
當烏龜和魚座標重疊,烏龜吃掉魚,烏龜體力增長20, 魚暫不計算體力
5). 當烏龜體力值爲0(掛掉)或者魚兒的數量爲0遊戲結束app
import random class Turtle(object): """ 烏龜類 屬性: (x,y), power 方法: move(), eat() """ def __init__(self): self.x = random.randint(0, 10) self.y = random.randint(0, 10) self.power = 100 def move(self): """烏龜移動的方法""" #烏龜的最大移動能力是2當移動到場景邊緣, move_skills = [-2, -1, 1, 2] #計算魚最新的x軸座標;() new_x = self.x + random.choice(move_skills) new_y = self.y + random.choice(move_skills) #當移動到場景邊緣如何處理? #(10, 0) ---- (11, 1) ----- (1, 1) #(10, 0) --- (9, -1) -----(9, 9) self.x = new_x % 10 self.y = new_y % 10 def eat(self): """烏龜吃魚""" self.power += 20 print("烏龜吃魚, 能量+20!") class Fish(object): """ 魚類 屬性: (x,y) 方法: move() """ def __init__(self): self.x = random.randint(0, 10) self.y = random.randint(0, 10) def move(self): """ 魚兒的最大移動能力是1當移動到場景邊緣, """ #魚兒的最大移動能力是1當移動到場景邊緣, move_skills = [-1, 1] #計算魚最新的x軸座標;() new_x = self.x + random.choice(move_skills) new_y = self.y + random.choice(move_skills) #當移動到場景邊緣如何處理? self.x = new_x % 10 self.y = new_y % 10 if __name__ == '__main__': # 遊戲生成1只烏龜和10條魚, 它們的移動方向均隨機. tur = Turtle() fishes = [Fish() for item in range(10)]"""
import random class BaseAnimal(object): def __init__(self): self.x = random.randint(0, 10) self.y = random.randint(0, 10) def move(self, move_skills=(-1, 1)): new_x = self.x + random.choice(move_skills) new_y = self.y + random.choice(move_skills) #當移動到場景邊緣如何處理? #(10, 0) ---- (11, 1) ----- (1, 1) #(10, 0) --- (9, -1) -----(9, 9) self.x = new_x % 10 self.y = new_y % 10 class Turtle(BaseAnimal): """ 烏龜類 屬性: (x,y), power 方法: move(), eat() """ def __init__(self): super(Turtle, self).__init__() self.power = 100 def eat(self): """烏龜吃魚""" self.power += 20 print("烏龜吃魚, 能量+20!") class Fish(BaseAnimal): pass if __name__ == '__main__': # 遊戲生成1只烏龜和10條魚, 它們的移動方向均隨機. tur = Turtle() fishes = [Fish() for item in range(10)]"""
import pygame #1). 初始化pygame pygame.init() #2). 顯示遊戲界面 pygame.display.set_mode((800, 800)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: print("遊戲結束......") exit(0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: print("UP") elif event.key == pygame.K_DOWN: print('DOWN')"""
import random import pygame class SiCong(object): """ 思聰類 屬性: (x,y), power 方法: move(), eat() """ def __init__(self): self.x = random.randint(50, width - 50) self.y = random.randint(50, height - 50) self.power = 100 def move(self, new_x, new_y): """思聰移動的方法""" self.x = new_x % width self.y = new_y % height def eat(self): """思聰吃熱狗""" self.power += 20 print("思聰吃熱狗, 能量+20!") class HotDog(object): """ 熱狗類 屬性: (x,y) 方法: move() """ def __init__(self): self.x = random.randint(50, width - 50) self.y = random.randint(50, height - 50) def move(self): """ 魚兒的最大移動能力是1當移動到場景邊緣, """ #魚兒的最大移動能力是1當移動到場景邊緣, move_skills = [-10] #計算魚最新的x軸座標;() new_x = self.x + random.choice(move_skills) #當移動到場景邊緣如何處理? self.x = new_x % width def main(): pygame.init() #顯示遊戲界面 screen = pygame.display.set_mode((width, height)) #設置界面標題 pygame.display.set_caption("吃熱狗遊戲") #加載遊戲中須要的圖片 bg = pygame.image.load('./img/bigger_bg1.jpg').convert() hotdogImg = pygame.image.load('./img/hot-dog.png').convert_alpha() sicongImg = pygame.image.load('./img/sicong.png').convert_alpha() hd_width, hd_height = hotdogImg.get_width(), hotdogImg.get_height() sc_width, sc_height = sicongImg.get_width(), sicongImg.get_height() #加載遊戲音樂(背景音樂和吃掉熱狗的音樂) pygame.mixer.music.load('./img/game_music.mp3') pygame.mixer.music.play(loops=0, start=0.0) # 播放設置, 不循環且從0.0s開始播放 #設置分數顯示參數信息(顯示位置、字體顏色、字體大小) scoreCount = 0 font = pygame.font.SysFont('arial', 20) # 系統設置字體的類型和大小 #顏色表示法: RGB (255, 0, 0)-紅色 (255, 255, 255)-白色 (0, 0, 0)-黑色 score = font.render("Score: %s" % (scoreCount), True, (0, 0, 0)) #建立一個Clock對象,跟蹤遊戲運行時間 fpsClock = pygame.time.Clock() #建立一個思聰和10個熱狗 sicong = SiCong() hotdogs = [HotDog() for item in range(10)] while True: for event in pygame.event.get(): if event.type == pygame.QUIT: print("遊戲結束......") exit(0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: # 移動人物向上多少個像素 sicong.move(sicong.x, sicong.y - 10) elif event.key == pygame.K_DOWN: sicong.move(sicong.x, sicong.y + 10) if event.key == pygame.K_LEFT: # 移動人物向上多少個像素 sicong.move(sicong.x - 10, sicong.y) elif event.key == pygame.K_RIGHT: sicong.move(sicong.x + 10, sicong.y) #繪製背景和分數 screen.blit(bg, (0, 0)) screen.blit(score, (200, 20)) #繪製熱狗,並實現熱狗的移動 for hd in hotdogs: screen.blit(hotdogImg, (hd.x, hd.y)) hd.move() #繪製sicong screen.blit(sicongImg, (sicong.x, sicong.y)) #判斷遊戲是否結束: 當人物體力值爲0(掛掉)或者熱狗的數量爲0遊戲結束 if sicong.power == 0: print("Game Over: Sicong Poer is 0") exit(1) if len(hotdogs) == 0: print("Game Over: hot-dog count is 0") exit(2) #判斷人物是否吃到熱狗:人物和熱狗的座標值相同, 則認爲吃掉 for hd in hotdogs: if 0 < sicong.x - hd.x < 50 and 0 < sicong.y - hd.y < 50: # 增長人物的能量值 sicong.eat() #移除被吃掉的熱狗 hotdogs.remove(hd) #增長得分 scoreCount += 10 #從新設置得分信息 score = font.render("Score: %s" % (scoreCount), True, (0, 0, 0)) #更新內容到遊戲窗口 pygame.display.update() fpsClock.tick(10) # 每秒更新10幀 if __name__ == '__main__': width =1000 height = 666 main()
import random import time import pygame import sys from pygame.locals import * # 導入一些經常使用的函數 width = 474 height = 233 pygame.init() screen = pygame.display.set_mode([width, height]) pygame.display.set_caption('烏龜吃魚') # 定義窗口的標題爲'烏龜吃魚' background = pygame.image.load("./img/bg.jpg").convert() fishImg = pygame.image.load("./img/hot-dog.png").convert_alpha() wuguiImg = pygame.image.load("./img/sicong.png").convert_alpha() #烏龜吃掉小魚的音樂 mp3格式的不行,wav格式的 #eatsound = pygame.mixer.Sound("achievement.wav") #背景音樂 pygame.mixer.music.load("./img/game_music.mp3") pygame.mixer.music.play(loops=0, start=0.0) #成績文字顯示 count = 0 font = pygame.font.SysFont("arial", 20) score = font.render("score %d" % count, True, (255, 255, 255)) #顯示遊戲狀態 status = font.render("Gaming", True, (255, 255, 255)) w_width = wuguiImg.get_width() - 5 # 獲得烏龜圖片的寬度,後面留着吃魚的時候用 w_height = wuguiImg.get_height() - 5 # 獲得烏龜圖片的高度 y_width = fishImg.get_width() - 5 # 獲得魚圖片的寬度 y_height = fishImg.get_height() - 5 # 獲得魚圖片的高度 fpsClock = pygame.time.Clock() # 建立一個新的Clock對象,能夠用來跟蹤總共的時間 #烏龜類 class Turtle: def __init__(self): self.power = 100 # 體力 #烏龜座標 self.x = random.randint(0, width - w_width) self.y = random.randint(0, height - w_height) #烏龜移動的方法:移動方向均隨機 第四條 def move(self, new_x, new_y): self.x = new_x % width self.y = new_y % height self.power -= 1 # 烏龜每移動一次,體力消耗1 def eat(self): self.power += 20 # 烏龜吃掉魚,烏龜體力增長20 if self.power > 100: self.power = 100 # 烏龜體力100(上限) #魚類 class Fish: def __init__(self): # 魚座標 self.x = random.randint(0, width - y_width) self.y = random.randint(0, height - y_height) def move(self): new_x = self.x + random.choice([-10]) self.x = new_x % width tur = Turtle() # 生成1只烏龜 fish = [] # 生成10條魚 for item in range(10): newfish = Fish() fish.append(newfish) # 把生成的魚放到魚缸裏 #pygame有一個事件循環,不斷檢查用戶在作什麼。事件循環中,如何讓循環中斷下來(pygame造成的窗口中右邊的插號在未定義前是不起做用的) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: # 經過上下左右方向鍵控制烏龜的動向 if event.key == pygame.K_LEFT: tur.move(tur.x - 10, tur.y) if event.key == pygame.K_RIGHT: tur.move(tur.x + 10, tur.y) if event.key == pygame.K_UP: tur.move(tur.x, tur.y - 10) if event.key == pygame.K_DOWN: tur.move(tur.x, tur.y + 10) screen.blit(background, (0, 0)) # 繪製背景圖片 screen.blit(score, (400, 20)) # 繪製分數 screen.blit(status, (0, 20)) # 繪製分數 #繪製魚 for item in fish: screen.blit(fishImg, (item.x, item.y)) #pygame.time.delay(100) item.move() # 魚移動 screen.blit(wuguiImg, (tur.x, tur.y)) # 繪製烏龜 #判斷遊戲是否結束:當烏龜體力值爲0(掛掉)或者魚兒的數量爲0遊戲結束 if tur.power < 0: print("Game Over: Turtle power is 0") #顯示遊戲狀態 status = font.render("Game Over: Turtle power is 0", True, (255, 255, 255)) pygame.display.update() # 更新到遊戲窗口 time.sleep(1) sys.exit(0) elif len(fish) == 0: status = font.render("Game Over: Fish is empty", True, (255, 255, 255)) pygame.display.update() # 更新到遊戲窗口 sys.exit(0) for item in fish: # print("魚", item.x, item.y, y_width, y_height) #print("烏龜", tur.x, tur.y, w_width, w_height) #判斷魚和烏龜是否碰撞? if ((tur.x < item.x + y_width) and (tur.x + w_width > item.x) and (tur.y < item.y + y_height) and (w_height + tur.y > item.y)): fish.remove(item) # 魚死掉 #吃魚音樂 #eatsound.play() count = count + 1 # 累加 score = font.render("score %d" % count, True, (255, 255, 255)) #print("死了一隻魚") #print("烏龜最新體力值爲 %d" % tur.power) pygame.display.update() # 更新到遊戲窗口 fpsClock.tick(10) # 經過每幀調用一次fpsClock.tick(10),這個程序就永遠不會以超過每秒10幀的速度運行