python大鱼吃小鱼_python 游戏编程 大鱼吃小鱼
# 游戲編程:按照以下游戲編寫一個烏龜類和魚類,并嘗試編寫游戲。
# 假設游戲場景(x,y)為0<=x<=10,0<=y<=10
# 游戲生成1只烏龜和10只魚
# 他們的移動方向均隨機
# 烏龜的最大移動速度為2,它可以隨機選擇1還是2移動,魚兒的最大移動能力是1
# 當移動到最大邊界時,自動反方向移動
# 烏龜初始化體力為100(上限)
# 烏龜每移動一次,體力消耗1
# 當烏龜和魚坐標重疊,烏龜吃掉魚,烏龜體力增加20
# 魚不考慮體力
# 當烏龜體力為0或者魚兒的數量為0時游戲結束
import random as r
# from random import choice
# 定義邊界
boundary_x = [0, 10]
boundary_y = [0, 10]
# 定義烏龜類
class Tortoise:
def __init__(self):
# count=1
self.physical_power = 100
self.x = r.randint(boundary_x[0], boundary_x[1])
self.y = r.randint(boundary_y[0], boundary_y[1])
def move(self):
# 隨機選擇移動速度和移動方向
new_x = self.x + r.choice([1, 2, -1, -2])
new_y = self.y + r.choice([1, 2, -1, -2])
# print("烏龜當前坐標是:",self.x,self.y)
# print("烏龜當前速度是:",self.speed)
# 當移動到X軸最大邊界時,自動反方向移動
if new_x > boundary_x[1]:
self.x = boundary_x[1] - (new_x - boundary_x[1])
elif new_x < boundary_x[0]:
self.x = boundary_x[0] - (new_x - boundary_x[0])
else:
self.x = new_x
# 當移動到Y軸最大邊界時,自動反方向移動
if new_y > boundary_y[1]:
self.x = boundary_y[1] - (new_y - boundary_y[1])
elif new_y < boundary_y[0]:
self.y = boundary_y[0] - (new_y - boundary_y[0])
else:
self.y = new_y
# 體力消耗加1
self.physical_power -= 1
return (self.x, self.y)
def eat(self):
self.physical_power += 20 # 體力增加20
if self.physical_power > 100:
self.physical_power = 100
class Fish:
def __init__(self):
# count=10
self.x = r.randint(boundary_x[0], boundary_x[1])
self.y = r.randint(boundary_y[0], boundary_y[1])
# 設置移動速度
# speed = 1
def move(self):
# 隨機選擇移動速度和移動方向
new_x = self.x + r.choice([1, -1])
new_y = self.y + r.choice([1, -1])
# 當移動到X軸最大邊界時,自動反方向移動
if new_x > boundary_x[1]:
self.x = boundary_x[1] - (new_x - boundary_x[1])
elif new_x < boundary_x[0]:
self.x = boundary_x[0] - (new_x - boundary_x[0])
else:
self.x = new_x
# 當移動到Y軸最大邊界時,自動反方向移動
if new_y > boundary_y[1]:
self.x = boundary_y[1] - (new_y - boundary_y[1])
elif new_y < boundary_y[0]:
self.y = boundary_y[0] - (new_y - boundary_y[0])
else:
self.y = new_y
return (self.x, self.y)
fish = []
tor = Tortoise()
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while 1:
if len(fish) == 0:
print("魚兒都被吃光了,游戲結束!")
break
if tor.physical_power == 0:
print("烏龜體力耗完了,游戲結束!")
break
pos = tor.move()
print("烏龜坐標是:", pos)
for each_fish in fish[:]:
f = each_fish.move()
print("魚兒坐標是: ", f)
if f == pos:
tor.eat()
fish.remove(each_fish)
print("------------有一條魚被吃掉了!----------------")
本文分享自微信公眾號 - 軟件測試經驗與教訓(udatest)
原文出處及轉載信息見文內詳細說明,如有侵權,請聯系 yunjia_community@tencent.com 刪除。
原始發表時間:2018-02-07
本文參與騰訊云自媒體分享計劃,歡迎正在閱讀的你也加入,一起分享。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python大鱼吃小鱼_python 游戏编程 大鱼吃小鱼的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LG 新款 C4 / G4 OLED 电
- 下一篇: 暴雪国服简体中文官网可正常登录 但下载问