python写的battle ship小游戏 - 1.0
最近学python,这是今天写的一个小游戏。
from random import randint class Board(object):
board = []
def __init__(self,row,col):
self.board = []
self.row = row
self.col = col
for i in range(row):
self.board.append( ["O"] * col ) def print_board(self):
space = (self.col * 2 - 8)/2
print "-" * space + " Board " + "-" * space
print "-" * self.col * 2
for r in self.board:
print " ".join(r)
print "-" * self.col * 2 class Game(object):
loop_time = 4
def __init__(self,row,col):
self.row = row
self.col =col
self.actual_row = -1
self.actual_col = -1
self.guess_row = -100
self.guess_col = -100
self.main_loop() def random_row(self):
return randint(1, self.row) def random_col(self):
return randint(1, self.col) def set_ship(self):
"""the battle ship is here:"""
self.actual_col = self.random_col()
self.actual_row = self.random_row()
print self.actual_row
print self.actual_col
self.my_board_actual.board[self.actual_row - 1][self.actual_col - 1] = "S" def get_input_from_player(self):
print "Please select where to hit on the board:"
self.guess_row = int(raw_input("To hit Row:")) - 1
self.guess_col = int(raw_input("To hit Col:")) - 1 def check_if_hit(self):
if self.my_board_actual.board[self.guess_row][self.guess_col] == "S":
print "Congratulations! You sunk my battle ship!"
self.my_board.board[self.guess_row][self.guess_col] = "S"
return True
else:
if self.guess_row < 0 or self.guess_col < 0 or self.guess_row > self.row or self.guess_col > self.col:
print "Ooops, that's not even in the ocean."
elif self.my_board.board[self.guess_row][self.guess_col] == "X":
print "You guessed that one already."
else:
print "You missed my battleship!"
self.my_board.board[self.guess_row][self.guess_col] = "X"
return False def main_loop(self): print "Game Start: Let's play Battleship!"
turn = 1
#print "Turn", turn
my_board = Board(self.row,self.col)
my_board_actual = Board(self.row,self.col)
self.my_board = my_board
self.my_board_actual = my_board_actual
my_board.print_board() self.set_ship() result = False while turn < self.loop_time + 1:
print "\nTurn", turn
self.get_input_from_player()
result = self.check_if_hit()
my_board.print_board()
if result:
turn = self.loop_time + 2
else:
turn += 1
else:
if not result:
print "Game Over" my_game = Game(5,5)
python写的battle ship小游戏 - 1.0的更多相关文章
- 每个人都可以用C语言写的推箱子小游戏!今天你就可以写出属于自己项目~
C语言,作为大多数人的第一门编程语言,重要性不言而喻,很多编程习惯,逻辑方式在此时就已经形成了.这个是我在大一学习 C语言 后写的推箱子小游戏,自己的逻辑能力得到了提升,在这里同大家分享这个推箱子小游 ...
- canvas写个简单的小游戏
之前在HTML5 Canvas属性和方法汇总一文中,介绍过Canvas的各种属性以及方法的说明,并列举了自己写的一些Canvas demo,接下来开始写一个简单的小游戏吧,有多简单,这么说吧,代码不到 ...
- [py]python写一个通讯录step by step V3.0
python写一个通讯录step by step V3.0 参考: http://blog.51cto.com/lovelace/1631831 更新功能: 数据库进行数据存入和读取操作 字典配合函数 ...
- 从Python小白到第一个小游戏发布
1.安装必要的环境(附图两张) 直接下载安装程序,本人win10系统,根据电脑系统下载并安装对应的python.exe,安装路径可以选择D盘的,具体安装细节这里就不说了,不知道的可以留言或者找度娘 2 ...
- 利用 Python 写一个颜值测试小工具
我们知道现在有一些利用照片来测试颜值的网站或软件,其实使用 Python 就可以实现这一功能,本文我们使用 Python 来写一个颜值测试小工具. 很多人学习python,不知道从何学起.很多人学习p ...
- 初识python:tkinter 实现 弹球小游戏(面向对象)
使用蹩脚式面相对象,实现弹球小游戏(非面向对象实现,主要介绍tk基础用法). #!/user/bin env python # author:Simple-Sir # time:2020/8/7 10 ...
- 初识python:tkinter 实现 弹球小游戏(非面相对象)
通过 tkinter 采用非面相对象式实现弹球小游戏(使用蹩脚式面相对象实现). #!/user/bin env python # author:Simple-Sir # time:2020/8/3 ...
- 用canvas写个接水管小游戏
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 过年的十八天假期迷迷糊糊一下子就过去了(LZ还是实习生,鉴于大学最后一个寒假了,所以就多请了好多天假),又要返工上班了.这是年后的第一篇博 ...
- 软件工程:黄金G点小游戏1.0
我们要做的是黄金G点小游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值. ...
随机推荐
- RDO部署openstack(1)
1. 安装系统CentOS 6.5 2. 网络配置 Eth0 设置 # cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ON ...
- DNS查询指令nslookup
描述: Nslookup指令是一个查询internet域名服务的程序.Nslookup指令有两个模式:交互式和非交互式. 交互式模式允许用户查询不同种类的主机和域名或在一个域名里输出主机列表,目的是查 ...
- android学习笔记46——File存储
File存储--IO操作文件 openFileOutput.openFileInput Context提供了如下两个方法来打开本应用程序的数据文件夹里面的文件IO流. 1.FileInputStrea ...
- 103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- Hadoop学习3--安装ssh服务
题前语:为什么要安装这个东西呢? 是因为我们要在多台机器之间通信,这个服务就相当于支持这种通信的一个桥梁,打个比喻,相当于windows里,通过远程桌面连接到其他机器. 所以,安装这个服务,的目的是: ...
- 黄聪:Mysql5.6缓存命中率
MySQL缓存命中率,网上说法不一,下面我说下我的看法,大家轻拍: 总的select查询数等于com_select(没命中) + qcache_hits(命中) + 解析错误的查询. 再来看看Com_ ...
- iphone Dev 开发实例9:Create Grid Layout Using UICollectionView in iOS 6
In this tutorial, we will build a simple app to display a collection of recipe photos in grid layout ...
- 如何获取客户端IP、操作系统、浏览器
request.getRemoteAddr();//获取IP request.getHeader("User-Agent");//获取操作系统信息.浏览器信息. protected ...
- Authentication token manipulation error for ubuntu ubuntu-16.04.1-desktop-amd64
https://ubuntuforums.org/showthread.php?t=1772894 Hi, I faced the same problem when I tried to recov ...
- Codeforces Round #363 (Div. 2) One Bomb
One Bomb 题意: 只有一个炸弹,并且一个只能炸一行和一列的'*',问最后能否炸完所以'*',如果可以输出炸弹坐标 题解: 这题做的时候真的没什么好想法,明知道b题应该不难,但只会瞎写,最后越写 ...