python大鱼吃小鱼_python 游戏编程 大鱼吃小鱼
# 游戲編程:按照以下游戲編寫(xiě)一個(gè)烏龜類和魚(yú)類,并嘗試編寫(xiě)游戲。
# 假設(shè)游戲場(chǎng)景(x,y)為0<=x<=10,0<=y<=10
# 游戲生成1只烏龜和10只魚(yú)
# 他們的移動(dòng)方向均隨機(jī)
# 烏龜?shù)淖畲笠苿?dòng)速度為2,它可以隨機(jī)選擇1還是2移動(dòng),魚(yú)兒的最大移動(dòng)能力是1
# 當(dāng)移動(dòng)到最大邊界時(shí),自動(dòng)反方向移動(dòng)
# 烏龜初始化體力為100(上限)
# 烏龜每移動(dòng)一次,體力消耗1
# 當(dāng)烏龜和魚(yú)坐標(biāo)重疊,烏龜吃掉魚(yú),烏龜體力增加20
# 魚(yú)不考慮體力
# 當(dāng)烏龜體力為0或者魚(yú)兒的數(shù)量為0時(shí)游戲結(jié)束
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):
# 隨機(jī)選擇移動(dòng)速度和移動(dòng)方向
new_x = self.x + r.choice([1, 2, -1, -2])
new_y = self.y + r.choice([1, 2, -1, -2])
# print("烏龜當(dāng)前坐標(biāo)是:",self.x,self.y)
# print("烏龜當(dāng)前速度是:",self.speed)
# 當(dāng)移動(dòng)到X軸最大邊界時(shí),自動(dòng)反方向移動(dòng)
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
# 當(dāng)移動(dòng)到Y(jié)軸最大邊界時(shí),自動(dòng)反方向移動(dòng)
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])
# 設(shè)置移動(dòng)速度
# speed = 1
def move(self):
# 隨機(jī)選擇移動(dòng)速度和移動(dòng)方向
new_x = self.x + r.choice([1, -1])
new_y = self.y + r.choice([1, -1])
# 當(dāng)移動(dòng)到X軸最大邊界時(shí),自動(dòng)反方向移動(dòng)
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
# 當(dāng)移動(dòng)到Y(jié)軸最大邊界時(shí),自動(dòng)反方向移動(dòng)
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("魚(yú)兒都被吃光了,游戲結(jié)束!")
break
if tor.physical_power == 0:
print("烏龜體力耗完了,游戲結(jié)束!")
break
pos = tor.move()
print("烏龜坐標(biāo)是:", pos)
for each_fish in fish[:]:
f = each_fish.move()
print("魚(yú)兒坐標(biāo)是: ", f)
if f == pos:
tor.eat()
fish.remove(each_fish)
print("------------有一條魚(yú)被吃掉了!----------------")
本文分享自微信公眾號(hào) - 軟件測(cè)試經(jīng)驗(yàn)與教訓(xùn)(udatest)
原文出處及轉(zhuǎn)載信息見(jiàn)文內(nèi)詳細(xì)說(shuō)明,如有侵權(quán),請(qǐng)聯(lián)系 yunjia_community@tencent.com 刪除。
原始發(fā)表時(shí)間:2018-02-07
本文參與騰訊云自媒體分享計(jì)劃,歡迎正在閱讀的你也加入,一起分享。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python大鱼吃小鱼_python 游戏编程 大鱼吃小鱼的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LG 新款 C4 / G4 OLED 电
- 下一篇: python 复制文件_python 复