parent
c70ae29e09
commit
a93a084095
@ -0,0 +1,127 @@
|
||||
import random
|
||||
from typing import Any
|
||||
|
||||
import pygame
|
||||
from pygame.sprite import Sprite
|
||||
from bullet import Bullet
|
||||
from bullet import Bullet1
|
||||
from bullet import Bullet2
|
||||
|
||||
|
||||
class Ship(Sprite):
|
||||
def __init__(self, ai_game):
|
||||
super().__init__()
|
||||
|
||||
# 获取屏幕设置
|
||||
self.screen = ai_game.screen
|
||||
self.screen_rect = ai_game.screen.get_rect()
|
||||
self.settings = ai_game.settings
|
||||
|
||||
# 加载图片
|
||||
self.image = pygame.image.load("resource/image/playerShip1_green.png").convert_alpha()
|
||||
# self.image.set_colorkey(self.settings.BLACK)
|
||||
self.rect = self.image.get_rect()
|
||||
self.radius = self.rect.width / 2
|
||||
# pygame.draw.circle(self.image, self.settings.RED, self.rect.center, self.radius)
|
||||
# 初始化位置
|
||||
self.rect.midbottom = self.screen_rect.midbottom
|
||||
self.rect.centery -= 30
|
||||
# 设置飞船的移动属性
|
||||
self.x = float(self.rect.x)
|
||||
self.speed = 10.0
|
||||
# 子弹精灵
|
||||
self.bullets = self.settings.bullets
|
||||
# 子弹发射
|
||||
self.ready_to_fire = 0
|
||||
# 飞机血条
|
||||
self.hp = 100
|
||||
self.alive = 1
|
||||
# 目标敌人
|
||||
self.target = self.settings.target
|
||||
|
||||
# 飞机等级
|
||||
self.grade = 1
|
||||
self.grade_up_time = 0
|
||||
self.grade_get_time = pygame.time.get_ticks()
|
||||
|
||||
def update(self, *args: Any, **kwargs: Any) -> None:
|
||||
self.moving()
|
||||
self.fire()
|
||||
self.effect_continue_time()
|
||||
|
||||
def moving(self):
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_UP] and self.rect.top > 0:
|
||||
self.rect.y -= self.speed
|
||||
if keys[pygame.K_DOWN] and self.rect.bottom <= self.screen_rect.bottom:
|
||||
self.rect.y += self.speed
|
||||
if keys[pygame.K_LEFT] and self.rect.left > 0:
|
||||
self.rect.x -= self.speed
|
||||
if keys[pygame.K_RIGHT] and self.rect.right <= self.screen_rect.right:
|
||||
self.rect.x += self.speed
|
||||
|
||||
def fire(self):
|
||||
keys = pygame.key.get_pressed()
|
||||
|
||||
if keys[pygame.K_SPACE]:
|
||||
if self.grade == 1: # 等级1
|
||||
if self.ready_to_fire == 0:
|
||||
# 创建子弹
|
||||
bullet = Bullet2(self)
|
||||
# 加入到精灵组中
|
||||
self.bullets.add(bullet)
|
||||
self.ready_to_fire += 1
|
||||
if self.ready_to_fire > 10:
|
||||
self.ready_to_fire = 0
|
||||
elif self.grade == 2: # 等级2时
|
||||
if self.ready_to_fire == 0:
|
||||
# 创建子弹
|
||||
bullet1 = Bullet2(self)
|
||||
bullet2 = Bullet2(self)
|
||||
bullet1.rect.center = self.rect.midleft
|
||||
bullet2.rect.center = self.rect.midright
|
||||
# 加入到精灵组中
|
||||
self.bullets.add(bullet1, bullet2)
|
||||
self.ready_to_fire += 1
|
||||
if self.ready_to_fire > 10:
|
||||
self.ready_to_fire = 0
|
||||
elif self.grade >= 3:
|
||||
if self.ready_to_fire == 0:
|
||||
# 创建子弹
|
||||
bullet1 = Bullet2(self)
|
||||
bullet2 = Bullet2(self)
|
||||
bullet1.speed_y = 36
|
||||
bullet2.speed_y = 36
|
||||
bullet1.rect.center = self.rect.midleft
|
||||
bullet2.rect.center = self.rect.midright
|
||||
# 加入到精灵组中
|
||||
self.bullets.add(bullet1, bullet2)
|
||||
self.ready_to_fire += 1
|
||||
if self.ready_to_fire > 5:
|
||||
self.ready_to_fire = 0
|
||||
|
||||
if keys[pygame.K_r]:
|
||||
if len(self.settings.aliens.sprites()):
|
||||
temp = 10
|
||||
if self.ready_to_fire == 0:
|
||||
# 创建子弹
|
||||
bullet = Bullet1(self)
|
||||
if self.grade == 4:
|
||||
temp = 1
|
||||
bullet.speed = 40
|
||||
# 加入到子弹精灵组中
|
||||
bullet.get_close_position()
|
||||
self.bullets.add(bullet)
|
||||
self.ready_to_fire += 1
|
||||
if self.ready_to_fire > temp:
|
||||
self.ready_to_fire = 0
|
||||
|
||||
def grade_up(self):
|
||||
self.grade += 1
|
||||
self.grade_up_time = pygame.time.get_ticks()
|
||||
|
||||
def effect_continue_time(self):
|
||||
now = pygame.time.get_ticks()
|
||||
if self.grade > 1 and now - self.grade_up_time > 5000:
|
||||
self.grade = 1
|
||||
self.grade_up_time = now
|
Loading…
Reference in new issue