坦克大战4
1.玩家坦克
class Player:
"""玩家管理"""
def __init__(self
,view_manager
,screen
):
self
.hp
= 100
self
.kill
= 0
self
.move
= pv
.MOVE_STAND
self
.lastmove
= pv
.MOVE_UP
self
.speed
= 5
self
.x
= view_manager
.screen_width
/ 2
self
.y
= view_manager
.screen_height
* 3 / 4
self
.bullets
= []
self
.guncenterx
= -1
self
.guncentery
= -1
self
.firedelay
= 0.3
self
.firetime
= datetime
.datetime
.now
() - datetime
.timedelta
(days
=1)
self
.font
= view_manager
.font
[1]
def is_die(self
):
if self
.hp
<= 0:
return True
else:
return False
def move_position(self
,view_manager
,screen
):
if self
.move
== pv
.MOVE_STAND
:
return
if self
.move
== pv
.MOVE_LEFT
:
self
.x
-=self
.speed
if self
.x
<= 0:
self
.x
= 0
elif self
.move
== pv
.MOVE_RIGHT
:
self
.x
+=self
.speed
bodysize
= view_manager
.player_body
[3].get_rect
()
if self
.x
+ bodysize
.width
>= view_manager
.screen_width
:
self
.x
= view_manager
.screen_width
- bodysize
.width
elif self
.move
== pv
.MOVE_UP
:
self
.y
-=self
.speed
if self
.y
<= 0:
self
.y
= 0
elif self
.move
== pv
.MOVE_DOWN
:
self
.y
+=self
.speed
bodysize
= view_manager
.player_body
[1].get_rect
()
if self
.y
+ bodysize
.height
>= view_manager
.screen_height
:
self
.y
= view_manager
.screen_height
- bodysize
.height
def getrect(self
,view_manager
):
if self
.lastmove
== pv
.MOVE_UP
:
return view_manager
.player_body
[0].get_rect
()
if self
.lastmove
== pv
.MOVE_DOWN
:
return view_manager
.player_body
[1].get_rect
()
if self
.lastmove
== pv
.MOVE_LEFT
:
return view_manager
.player_body
[2].get_rect
()
if self
.lastmove
== pv
.MOVE_RIGHT
:
return view_manager
.player_body
[3].get_rect
()
return None
def draw(self
,view_manager
,screen
):
direct
= self
.move
if self
.move
== pv
.MOVE_STAND
:
direct
= self
.lastmove
if direct
== pv
.MOVE_UP
:
screen
.blit
(view_manager
.player_body
[0],(self
.x
,self
.y
))
if direct
== pv
.MOVE_DOWN
:
screen
.blit
(view_manager
.player_body
[1],(self
.x
,self
.y
))
if direct
== pv
.MOVE_LEFT
:
screen
.blit
(view_manager
.player_body
[2],(self
.x
,self
.y
))
if direct
== pv
.MOVE_RIGHT
:
screen
.blit
(view_manager
.player_body
[3],(self
.x
,self
.y
))
pygame
.draw
.rect
(screen
,pv
.COLOR_RED
,Rect
((self
.x
+ 5 , self
.y
- 5),(self
.hp
, 5)))
killtxt
= self
.font
.render
('击毁:' + str(self
.kill
) ,1,pv
.COLOR_RED
)
screen
.blit
(killtxt
,(5,5))
def work(self
,screen
,view_manager
):
self
.move_position
(view_manager
,screen
)
self
.draw
(view_manager
,screen
)
for b
in self
.bullets
:
if b
.is_effect
== False:
self
.bullets
.remove
(b
)
for b
in self
.bullets
:
b
.move_position
(view_manager
,screen
)
b
.draw
(view_manager
,screen
)
def guncenter(self
,view_manager
):
if self
.lastmove
== pv
.MOVE_UP
:
bodyrect
= view_manager
.player_body
[0].get_rect
()
self
.guncenterx
= self
.x
+ bodyrect
.width
/ 2 - 4
self
.guncentery
= self
.y
return
if self
.lastmove
== pv
.MOVE_DOWN
:
bodyrect
= view_manager
.player_body
[1].get_rect
()
self
.guncenterx
= self
.x
+ bodyrect
.width
/ 2 - 4
self
.guncentery
= self
.y
+ bodyrect
.height
return
if self
.lastmove
== pv
.MOVE_LEFT
:
bodyrect
= view_manager
.player_body
[2].get_rect
()
self
.guncenterx
= self
.x
self
.guncentery
= self
.y
+ bodyrect
.height
/ 2 - 4
return
if self
.lastmove
== pv
.MOVE_RIGHT
:
bodyrect
= view_manager
.player_body
[3].get_rect
()
self
.guncenterx
= self
.x
+ bodyrect
.width
self
.guncentery
= self
.y
+ bodyrect
.height
/ 2 - 4
return
def fire(self
,screen
,view_manager
):
firepass
= (datetime
.datetime
.now
() - self
.firetime
).microseconds
/ 1000000
if firepass
< self
.firedelay
:
return
self
.firetime
= datetime
.datetime
.now
()
self
.guncenter
(view_manager
)
newbullet
= Bullet
(view_manager
,screen
,self
.guncenterx
,self
.guncentery
,self
.lastmove
)
self
.bullets
.append
(newbullet
)
view_manager
.sounds
[0].play
()
2.敌对坦克
class Monster:
"""敌人处理"""
def __init__(self
,view_manager
,screen
):
self
.hp
= 100
self
.move
= pv
.MOVE_DOWN
self
.lastmove
= pv
.MOVE_DOWN
self
.speed
= 4
self
.x
= random
.randint
(100,view_manager
.screen_width
- 300)
self
.y
= random
.randint
(0,view_manager
.screen_height
/ 2)
self
.bullets
= []
self
.guncenterx
= -1
self
.guncentery
= -1
self
.firedelay
= 1
self
.firetime
= datetime
.datetime
.now
() - datetime
.timedelta
(days
=1)
self
.changemovedelay
= random
.randint
(0,3)
self
.changemovetime
= datetime
.datetime
.now
() - datetime
.timedelta
(days
=1)
def move_position(self
,view_manager
,screen
):
changepass
= (datetime
.datetime
.now
() - self
.changemovetime
).seconds
if changepass
> self
.changemovedelay
:
self
.changemovetime
= datetime
.datetime
.now
()
self
.move
= random
.randint
(1,4)
self
.lastmove
= self
.move
self
.changemovedelay
= random
.randint
(0,3)
if self
.move
== pv
.MOVE_STAND
:
return
if self
.move
== pv
.MOVE_LEFT
:
self
.x
-=self
.speed
if self
.x
<= 0:
self
.x
= 0
self
.move
= pv
.MOVE_RIGHT
self
.lastmove
= self
.move
elif self
.move
== pv
.MOVE_RIGHT
:
self
.x
+=self
.speed
bodysize
= view_manager
.monster_body
[3].get_rect
()
if self
.x
+ bodysize
.width
>= view_manager
.screen_width
:
self
.x
= view_manager
.screen_width
- bodysize
.width
self
.move
= pv
.MOVE_LEFT
self
.lastmove
= self
.move
elif self
.move
== pv
.MOVE_UP
:
self
.y
-=self
.speed
if self
.y
<= 0:
self
.y
= 0
self
.move
= pv
.MOVE_DOWN
self
.lastmove
= self
.move
elif self
.move
== pv
.MOVE_DOWN
:
self
.y
+=self
.speed
bodysize
= view_manager
.monster_body
[1].get_rect
()
if self
.y
+ bodysize
.height
>= view_manager
.screen_height
:
self
.y
= view_manager
.screen_height
- bodysize
.height
self
.move
= pv
.MOVE_UP
self
.lastmove
= self
.move
def getrect(self
,view_manager
):
if self
.lastmove
== pv
.MOVE_UP
:
return view_manager
.monster_body
[0].get_rect
()
if self
.lastmove
== pv
.MOVE_DOWN
:
return view_manager
.monster_body
[1].get_rect
()
if self
.lastmove
== pv
.MOVE_LEFT
:
return view_manager
.monster_body
[2].get_rect
()
if self
.lastmove
== pv
.MOVE_RIGHT
:
return view_manager
.monster_body
[3].get_rect
()
return None
def draw(self
,view_manager
,screen
):
direct
= self
.move
if direct
== pv
.MOVE_STAND
:
direct
= self
.lastmove
if direct
== pv
.MOVE_UP
:
screen
.blit
(view_manager
.monster_body
[0],(self
.x
,self
.y
))
if direct
== pv
.MOVE_DOWN
:
screen
.blit
(view_manager
.monster_body
[1],(self
.x
,self
.y
))
if direct
== pv
.MOVE_LEFT
:
screen
.blit
(view_manager
.monster_body
[2],(self
.x
,self
.y
))
if direct
== pv
.MOVE_RIGHT
:
screen
.blit
(view_manager
.monster_body
[3],(self
.x
,self
.y
))
pygame
.draw
.rect
(screen
,pv
.COLOR_RED
,Rect
((self
.x
+ 5 , self
.y
- 5),(self
.hp
, 5)))
def work(self
,screen
,view_manager
):
self
.move_position
(view_manager
,screen
)
self
.draw
(view_manager
,screen
)
self
.fire
(screen
,view_manager
)
for b
in self
.bullets
:
if b
.is_effect
== False:
self
.bullets
.remove
(b
)
for b
in self
.bullets
:
b
.move_position
(view_manager
,screen
)
b
.draw
(view_manager
,screen
)
def guncenter(self
,view_manager
):
if self
.lastmove
== pv
.MOVE_UP
:
bodyrect
= view_manager
.monster_body
[0].get_rect
()
self
.guncenterx
= self
.x
+ bodyrect
.width
/ 2 - 4
self
.guncentery
= self
.y
return
if self
.lastmove
== pv
.MOVE_DOWN
:
bodyrect
= view_manager
.monster_body
[1].get_rect
()
self
.guncenterx
= self
.x
+ bodyrect
.width
/ 2 - 4
self
.guncentery
= self
.y
+ bodyrect
.height
return
if self
.lastmove
== pv
.MOVE_LEFT
:
bodyrect
= view_manager
.monster_body
[2].get_rect
()
self
.guncenterx
= self
.x
self
.guncentery
= self
.y
+ bodyrect
.height
/ 2 - 4
return
if self
.lastmove
== pv
.MOVE_RIGHT
:
bodyrect
= view_manager
.monster_body
[3].get_rect
()
self
.guncenterx
= self
.x
+ bodyrect
.width
self
.guncentery
= self
.y
+ bodyrect
.height
/ 2 - 4
return
def fire(self
,screen
,view_manager
):
firepass
= (datetime
.datetime
.now
() - self
.firetime
).seconds
if firepass
< self
.firedelay
:
return
self
.firetime
= datetime
.datetime
.now
()
self
.guncenter
(view_manager
)
newbullet
= Bullet
(view_manager
,screen
,self
.guncenterx
,self
.guncentery
,self
.lastmove
)
self
.bullets
.append
(newbullet
)
view_manager
.sounds
[0].play
()
python坦克大战(5)战斗处理与炮弹
转载请注明原文地址: https://mac.8miu.com/read-1071.html