一、定义一个门票系统

门票的原价是100元

当周末的时候门票涨价20%

小孩子半票

计算2个成人和1个小孩的平日票价

----------------------------------------------------------

class Ticket():
def __init__(self, weekend=False, child=False):
self.exp = 100
if weekend:
self.inc = 1.2
else:
self.inc = 1 if child:
self.discount = 0.5
else:
self.discount = 1 def cal_price(self, num):
return self.exp * self.inc * self.discount * num adult = Ticket()
child = Ticket(child=True) print("两个成年人和一个小孩子平日的价格是{}".format(adult.cal_price(2)+ child.cal_price(1)))
-------------------------------------------
游戏编程:按一下要求定义一个乌龟类和鱼类并尝试编程
一、假设游戏场景为范围(x,y)为0<=x<=10,0<=y<=10
二、游戏生成1只乌龟和10条鱼
三、他们的移动方向均随机
四、乌龟的最大移动能力是2(乌龟可以随机选择移动是1还是2),鱼的最大移动能力是1
五、当移动到场景边缘,自动向反方向移动
六、乌龟没移动一次,体力消耗1
七、当乌龟和鱼重叠,乌龟吃掉鱼,乌龟体力增加20
八、鱼不计算体力
九、当乌龟体力值为0或者鱼的数量为0时,游戏结束
-------------------------------------------
import random as r

class Turtle(object):
def __init__(self):
self.power = 100 #初始化乌龟的位置
self.x = r.randint(0, 10)
self.y = r.randint(0, 10) def move(self):
new_x = r.choice([1, 2, -1, -2]) + self.x
new_y = r.choice([1, 2, -1, -2]) + self.y # 判断 乌龟的移动是否超出了边界 if new_x < 0:
self.x = 0 - (new_x - 0)
elif new_x > 10:
self.x = 10 - (new_x - 10)
else:
self.x = new_x if new_y < 0:
self.y = 0 - (new_y - 0)
elif new_y > 10:
self.y = 10 - (new_y - 10)
else:
self.y = new_y self.power -= 1
return (self.x, self.y) def eat(self):
self.power += 20
if self.power >= 100:
self.power = 100 class Fish(object): def __init__(self):
self.x = r.randint(0, 10)
self.y = r.randint(0, 10) def move(self):
new_x = self.x + r.choice([1, -1])
new_y = self.y + r.choice([1, -1]) if new_x < 0:
self.x = 0 - (new_x - 0)
elif new_x > 10:
self.x = 10 - (new_x - 10)
else:
self.x = new_x if new_y < 0:
self.y = 0 - (new_y - 0)
elif new_y > 10:
self.y = 10 - (new_y - 10)
else:
self.y = new_y return (self.x, self.y) turtle = Turtle()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish) while True:
if not len(fish):
print("鱼被吃完了,游戏结束")
break
if not turtle.power:
print("乌龟体力被耗尽了,游戏结束了")
break pos = turtle.move() # 在迭代中做列表的删除元素是非常危险的,经常会出现一些意想不到的问题,因为迭代器是直接引用列表元素的数据做的操作
# 所以 我们这里把列表拷贝一份传给迭代器,然后再对原列表做操作
for each_fish in fish[:]:
if each_fish.move() == pos:
turtle.eat()
fish.remove(each_fish)
print("有一条鱼被吃掉了")
-----------------------------------------
定义一个点(point)和直线(Line)类,使用getLen方法获取两点构成直线的长度
-----------------------------------------
import math

class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y def get_x(self):
return self.x def get_y(self):
return self.y class Line(object):
def __init__(self, p1, p2):
self.x = p1.get_x() - p2.get_x()
self.y = p1.get_y() - p2.get_y() self.len = math.sqrt(self.x*self.x + self.y*self.y) def get_len(self):
return self.len p1 = Point(2,3)
p2 = Point(5,7)
line = Line(p1, p2)
line.get_len()
												

最新文章

  1. Flexible 弹性盒子模型之CSS flex-flow
  2. 用canvas画环形圆形图片
  3. 无废话SharePoint入门教程五[创建SharePoint页面布局]
  4. 微软2017校招笔试题3 registration day
  5. ntdll.dll函数原型
  6. 微信平台(一)--获取access_token
  7. HDU 3829 Cat VS Dog(最大独立集)
  8. Android framework浅析[转]
  9. ArrayList内元素按照字典排序
  10. DEDECMS整站复制
  11. cocos2dx中关于Action动作的相关API的具体介绍
  12. python实现一般最小二乘系统辨识方法
  13. Python学习笔记 - 迭代Iteration
  14. 什么是LogDashboard?
  15. hdu-3374(kmp+最小表示法)
  16. centos 查找命令的可用包/命令属于哪个软件包
  17. OneZero——Review报告会
  18. Eclipse中ctrl+shift+r与ctrl+shift+t的区别
  19. linux服务器的所有服务web/dns/dhcp/vsftp-nfs-samba的配置
  20. C#调用非托管dll--路径问题

热门文章

  1. IIS Express 使用方法
  2. 第二周JAVA总结
  3. VS2013启动 外接程序VMDebugger未能加载或导致了异常
  4. Nginx 2.安装与部署配置
  5. JAVA基础--JAVA 集合框架(泛型、file类)
  6. getchar返回int类型
  7. c语言中不允许在函数外部给全局变量赋值
  8. 似乎在梦中见过的样子 (KMP)
  9. [项目实战]训练retinanet(pytorch版)
  10. 修改admin中App的名称与表的名称