Commit 7ef8a4db by BellCodeEditor

save project

parent 4ecd1926
class Hero(object):
def __init__(self, name):
self.name = name
self.level = 1
self.hp = 250
self.attack = 40
self.max_hp=self.hp
def combat(self, enemy): # 攻击
info1 = self.name + "对" + enemy.name + "发起进攻,"
info2 = "造成" + str(self.attack) + "点伤害,"
enemy.hp -= self.attack
if enemy.hp > 0:
info3 = enemy.name + "还剩下" + str(enemy.hp) + "血量"
info = info1 + info2 + info3
print(info)
else:
info3 = enemy.name + "阵亡,游戏结束"
info = info1 + info2 + info3
print(info)
exit()
def cure(self):
self.hp+=60
if self.hp>self.max_hp:
self.hp=self.max_hp
print(self.name+'使用了治疗术,增加'+str(60)+'血量,目前血量为'+str(self.hp))
self.max_self=self.hp
class Player(Hero):
def __init__(self, hero_type, name):
super().__init__(name)
self.hp = 200
self.attack = 50
self.max_hp=self.hp
self.hero_type = hero_type
print("角色" + self.name + "创建成功,英雄类型为:", self.hero_type)
print("当前等级、血量、攻击力分别为:", self.level, self.hp, self.attack)
houyi = Player("射手", "后羿")
yase = Hero("垭瑟")
houyi.combat(yase)
yase.combat(houyi)
houyi.cure()
yase.cure()
import pygame
from pygame import locals
# 初始化pygame,为使用硬件做准备
pygame.init()
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
A = pygame.time.Clock()
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png')
food = pygame.image.load('apple.png')
body = pygame.image.load('body.png')
left = pygame.image.load('left.png')
up = pygame.image.load('up.png')
down = pygame.image.load('down.png')
x,y = 240, 120
position = [(180, 90),(180, 120),(210, 120),(x, y)]
setheading = "right"
snake_head = right
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
elif event,type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left":
setheading = "right"
snake_head = right
elif event.key == locals.K_LEFT and setheading != "right":
setheading = "left"
snake_head = left
elif event.key == locals.K_UP and setheading != "down":
setheading = "up"
snake_head = up
else:
setheading = "down"
snake_head = down
if seatheading == "right":
x+=30
elif seatheading == "left":
x-=30
elif seatheading == "up":
y-=30
else:
y+=30
x=x+30
y=y+30
position.append((x,y))
position.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇画上去
screen.blit(right, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body,position[i])
# 将果实画上去
screen.blit(food, (360, 300))
# 刷新画面
pygame.display.update()
A.tick(3)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment