Commit 3dab9b3e by BellCodeEditor

save project

parent c424c5ab
Showing with 86 additions and 35 deletions
class Hero: # 英雄类 import pygame
def __init__(self, name): # 实例属性 import random
self.name = name
self.level = 1
self.hp = 250 from pygame import locals
self.attack = 40
self.max_hp = self.hp # 初始化pygame,为使用硬件做准备
pygame.init()
def combat(self, enemy): # 攻击功能 # 创建一个窗口
info1 = self.name+"对"+enemy.name+"发起进攻," screen = pygame.display.set_mode((660, 480))
info2 = "造成"+str(self.attack)+"点伤害," FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
enemy.hp -= self.attack
if enemy.hp > 0: # 背景
info3 = enemy.name+"还剩下"+str(enemy.hp)+"血量" background = pygame.image.load('bg.png')
info = info1+info2+info3 right = pygame.image.load('right.png') # 头 朝右
print(info) food = pygame.image.load('apple.png') # 食物 苹果
else: body = pygame.image.load('body.png') # 身体
info3 = enemy.name+"阵亡,游戏结束" left = pygame.image.load('left.png') # 头 朝左
info = info1+info2+info3 up = pygame.image.load('up.png') # 头 朝上
print(info) down = pygame.image.load('down.png') # 头 朝下
zxcvbnm = pygame.font.Font('neuropol.ttf',18)
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading = "right"
snake_head = right
apple_x = 480
apple_y = 360
score = 0
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left":
class Player(Hero): # 玩家英雄 setheading = 'right'
def __init__(self,name,Hero_type): snake_head = right
super().__init__(name) if event.key == locals.K_LEFT and setheading != "right":
self.hp = 200 setheading = 'left'
self.attack = 50 snake_head = left
self.Hero_type = Hero_type if event.key == locals.K_UP and setheading != "down":
print("角色"+self.name+"创建成功,角色类型为:",self.Hero_type) setheading = 'up'
print("当前等级血量攻击力分别为",self.level,self.hp,self.attack) snake_head = up
if event.key == locals.K_DOWN and setheading != "up":
houyi= Player("后羿","射手") setheading = 'down'
yase = Hero("垭瑟") snake_head = down
\ No newline at end of file
# 设置贪吃蛇的头部坐标
if setheading == "right":
x += 30
elif setheading == "left":
x -= 30
elif setheading == "up":
y -= 30
else:
y += 30
position.append((x, y))
if apple_x == x and apple_y == y:
apple_x = random.randint(0,21)*30
apple_y = random.randint(0,15)*30
score += 10
if x<0 or x >650 or y<0 or y>450:
exit()
else:
position.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body, position[i])
info = "score: "+str(score)
text = zxcvbnm.render(info,True,(0,0,0,))
screen.blit(text,(500,30))
# 将果实画上去
screen.blit(food, (apple_x,apple_y ))
# 刷新画面
pygame.display.update()
FPSCLOCK.tick(3)
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