2 Star 5 Fork 2

猫姐/LearnCC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Player.py 7.43 KB
一键复制 编辑 原始数据 按行查看 历史
猫姐 提交于 2021-10-17 16:57 . 代码重构暂时结束
import pygame
import random
from setting import pos_play_list, pos_zombie_list, all_cc_labels,\
GAME_LEVEL_MAX, total_cc_count
ZOMBIE_HIT_MUSIC = "./sounds/zombie_hit.wav"
PLAYER_HIT_MUSIC = "./sounds/player_hit.wav"
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.pos_index = random.randint(0, 5)
self.pos_x = pos_play_list[self.pos_index][0]
self.pos_y = pos_play_list[self.pos_index][1]
self.image_index = 0
self.image_list = []
self.live = 3
self.redraw_heart = True
red_heart_image = pygame.image.load("./images/red_heart.png")
self.red_heart_image = pygame.transform.scale(red_heart_image,
(int(red_heart_image.get_width()/1.2), int(red_heart_image.get_height()/1.2)))
gray_heart_image = pygame.image.load("./images/gray_heart.png")
self.gray_heart_image = pygame.transform.scale(gray_heart_image,
(int(gray_heart_image.get_width()/1.2), int(gray_heart_image.get_height()/1.2)))
for i in range(5):
image_filename = "./images/rest/player" + str(i) + ".png"
image = pygame.image.load(image_filename)
# image = pygame.transform.scale(image, (120, 110))
self.image_list.append(image)
# self.image = ["red", "green", "blue", "yellow"]
self.rect = self.image_list[0].get_rect()
self.rect.topleft = pos_play_list[self.pos_index]
self.frequency = 0
self.MAX_FREQUENCY = 50
self.hurt_music = pygame.mixer.Sound(PLAYER_HIT_MUSIC)
self.hurt_music.set_volume(0.8)
def get_pos_index(self):
return self.pos_index
def play_hurt_sound(self):
self.hurt_music.play()
def update_pos(self, direction):
if direction == "UP":
self.pos_index -= 1
if self.pos_index < 0:
self.pos_index = 0
elif direction == "DOWN":
self.pos_index += 1
if self.pos_index > 5:
self.pos_index = 5
else:
# print("wrong direction " + direction)
pass
self.pos_x = pos_play_list[self.pos_index][0]
self.pos_y = pos_play_list[self.pos_index][1]
self.rect.x = self.pos_x
self.rect.y = self.pos_y
def decrease_live(self):
self.live -= 1
def get_live(self):
return self.live
def is_live(self):
if self.live == 0:
return False
else:
return True
def is_collide_with_zombies(self, zombies):
if pygame.sprite.spritecollide(self, zombies, False):
self.kill()
# stage = GAME_OVER_FAIL_MODE
return True
return False
def is_collide_with_bullets(self, bullets):
is_collided = pygame.sprite.spritecollide(self, bullets, False)
if is_collided:
return_bullet = is_collided[0]
return_bullet.kill()
self.play_hurt_sound()
self.decrease_live()
if not self.is_live():
return True
return False
def is_collide_with_them(self, zombies, bullets): # role= zombie, bullet
if self.is_collide_with_zombies(zombies):
return True
if self.is_collide_with_bullets(bullets):
return True
return False
def update(self):
if self.frequency == self.MAX_FREQUENCY:
self.frequency = 0
self.image_index += 1
self.image_index %= 4
self.frequency += 1
def draw(self, screen):
# screen.fill(self.image[self.image_index], self.rect)
screen.blit(self.image_list[self.image_index], (self.pos_x, self.pos_y))
offset = 0
for i in range(3):
screen.blit(self.gray_heart_image, (850 + offset, 20))
offset += 65
offset = 0
for i in range(self.live):
screen.blit(self.red_heart_image, (850 + offset, 20))
offset += 65
class Zombie(pygame.sprite.Sprite):
def __init__(self, index, text_index):
super(Zombie, self).__init__()
self.text_index = text_index
text_image = pygame.image.load("./images/cc/zi/" + str(all_cc_labels[self.text_index]) + ".png")
self.text_image = pygame.transform.scale(text_image,
(int(text_image.get_width()/6), int(text_image.get_height()/6)))
self.pos_index = index
self.pos_x = pos_zombie_list[self.pos_index][0]
self.pos_y = pos_zombie_list[self.pos_index][1]
self.image_index = 0
self.image_list = []
# self.image = ["red", "green", "blue", "yellow"]
zombie_name = ["tom", "tim", "him"]
zombie_index = random.randint(0, 2)
for i in range(5):
image_filename = "./images/rest/" + zombie_name[zombie_index] + str(i) + ".png"
image = pygame.image.load(image_filename)
image = pygame.transform.scale(image, (int(image.get_width()/1.2), int(image.get_height()/1.2)))
self.image_list.append(image)
# self.rect = pygame.Rect(self.pos_x, self.pos_y, 50, 45)
self.rect = self.image_list[self.image_index].get_rect(left=self.pos_x, top=self.pos_y)
self.frequency = 0
self.MAX_FREQUENCY = 7
# self.dx = random.uniform(1, 3.3)
self.hit_music = pygame.mixer.Sound(ZOMBIE_HIT_MUSIC)
self.hit_music.set_volume(0.2)
self.live = 3
def set_speed_level(self, game_level):
gate1 = GAME_LEVEL_MAX//3
gate2 = GAME_LEVEL_MAX*2//3
if game_level <= gate1:
self.dx = random.uniform(0.5, 1.2)
elif game_level <= gate2:
self.dx = random.uniform(1.2, 2.2)
else:
self.dx = random.uniform(2.2, 3.3)
def get_text_index(self):
return self.text_index
def decrease_live(self):
self.live -= 1
def get_live(self):
return self.live
def is_live(self):
if self.live == 0:
return False
else:
return True
def move(self):
self.pos_x -= self.dx
self.rect.x = self.pos_x + 100
if self.rect.x < 70:
self.kill()
def get_pos_x(self):
return self.pos_x
def play_hurt_sound(self):
self.hit_music.play()
def update(self):
self.move()
self.frequency += 1
# screen.fill(self.image[self.image_index], self.rect)
# screen.fill("red",self.rect)
self.image = self.image_list[self.image_index]
if self.frequency == self.MAX_FREQUENCY:
self.frequency = 0
self.image_index += 1
self.image_index %= 5
def draw(self, screen):
screen.blit(self.image, (self.pos_x, self.pos_y))
screen.blit(self.text_image, (self.pos_x + 86, self.pos_y))
def create_new_zombies(current_text_index):
count = 0
temp_index = [0, 1, 2]
random.shuffle(temp_index)
zombies = pygame.sprite.Group()
for i in temp_index:
text_index = (current_text_index + count) % total_cc_count
zombie = Zombie(i, text_index)
speed_factor = current_text_index // 7
zombie.set_speed_level(speed_factor)
zombies.add(zombie)
count += 2
return zombies
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python4fun/LearnCC.git
[email protected]:python4fun/LearnCC.git
python4fun
LearnCC
LearnCC
master

搜索帮助