
前言:解放军VS木马是本人创造的一个射击小游戏。在这个游戏中狡猾的外星人把自己装在玻璃盒子里,妄想入侵地球。但是被解放军发现了,勇猛无比的一个解放军叔叔把外星人打得可是鬼哭狼嚎。请按a,d键移动坦克,单击鼠标发射炮弹,击败外星人的入侵。如果有一个外星人安全着陆,游戏就失败了!解放军VS木马是一个坦克射击类小游戏。作品共有8个py文件和相关素材。这里展示的是炮弹Shell类的源代码。每个模块都能单独运行。
下面是部分代码预览:
from turtle import *
class Shell(Turtle):
"""炮弹类,继承自海龟类,实例化后它就会移动,直到碰到边缘或外星人"""
def __init__(self,x,y,h):
"""x:横坐标,y:纵坐标,h:方向"""
Turtle.__init__(self,visible = False,shape="circle")
self.color('navy') # 设颜色
self.penup() # 抬笔
def move(self):
"""移动"""
def is_to_edge(self):
"""中心点是否到了边缘判断"""
x = self.xcor()
y = self.ycor()
c1 = x < self.left_edge
c2 = x > self.right_edge
c3 = y < self.bottom_edge
c4 = y >self.top_edge
return c1 or c2 or c3 or c4
if __name__ =="__main__":
screen = Screen()
screen.bgcolor("cyan")
screen.title("解放军VS木马炮弹类")
screen.delay(0)
def spawn_shell(x,y):
[Shell(0,0,h) for h in range(0,360,36)]
screen.onscreenclick(spawn_shell)
screen.mainloop()
如需要查看完整源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
