ship.py
This commit is contained in:
parent
a59b913cb5
commit
6e786c1ebe
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
60
alien.py
60
alien.py
|
@ -1,38 +1,40 @@
|
|||
import pygame
|
||||
from pygame.sprite import Sprite
|
||||
class Alien(Sprite):
|
||||
#'''表示单个外星人的类'''
|
||||
|
||||
def __init__(self, ai_settings, screen):
|
||||
#'''初始化外星人并设置其起始位置'''
|
||||
super(Alien,self).__init__()
|
||||
self.screen = screen
|
||||
class Alien(Sprite):
|
||||
"""A class to represent a single alien in the fleet."""
|
||||
|
||||
def __init__(self, ai_settings, screen):
|
||||
"""Initialize the alien, and set its starting position."""
|
||||
super(Alien, self).__init__()
|
||||
self.screen = screen
|
||||
self.ai_settings = ai_settings
|
||||
|
||||
#加载外星人图像,并设置其rect属性
|
||||
self.image = pygame.image.load('images/alien.jpg')
|
||||
|
||||
# Load the alien image, and set its rect attribute.
|
||||
self.image = pygame.image.load(r'alien.bmp')
|
||||
self.rect = self.image.get_rect()
|
||||
|
||||
#每个外星人最初都在屏幕左上角附近
|
||||
self.rect.x = self.rect.width
|
||||
|
||||
# Start each new alien near the top left of the screen.
|
||||
self.rect.x = self.rect.width
|
||||
self.rect.y = self.rect.height
|
||||
|
||||
#存储外星人的准确位置
|
||||
|
||||
# Store the alien's exact position.
|
||||
self.x = float(self.rect.x)
|
||||
|
||||
def blitme(self):
|
||||
# '''在指定位置绘制外星人'''
|
||||
self.screen.blit(self.image, self.rect)
|
||||
|
||||
def update(self):
|
||||
#'''向右移动外星人'''
|
||||
self.x += (self.ai_settings.alien_speed_factor * self.ai_settings.fleet_direction)
|
||||
self.rect.x = self.x
|
||||
|
||||
def check_edges(self):
|
||||
#"""如果外星人位于屏幕边缘, 就返回True"""
|
||||
screen_rect = self.screen.get_rect()
|
||||
if self.rect.right >= screen_rect.right:
|
||||
return True
|
||||
elif self.rect.left <= 0:
|
||||
def check_edges(self):
|
||||
"""Return True if alien is at edge of screen."""
|
||||
screen_rect = self.screen.get_rect()
|
||||
if self.rect.right >= screen_rect.right:
|
||||
return True
|
||||
elif self.rect.left <= 0:
|
||||
return True
|
||||
|
||||
def update(self):
|
||||
"""Move the alien right or left."""
|
||||
self.x += (self.ai_settings.alien_speed_factor *
|
||||
self.ai_settings.fleet_direction)
|
||||
self.rect.x = self.x
|
||||
|
||||
def blitme(self):
|
||||
"""Draw the alien at its current location."""
|
||||
self.screen.blit(self.image, self.rect)
|
||||
|
|
43
bullet.py
43
bullet.py
|
@ -1,30 +1,33 @@
|
|||
import pygame
|
||||
from pygame.sprite import Sprite
|
||||
|
||||
class Bullet(Sprite):
|
||||
#'''一个对飞船发射的子弹进行管理的类'''
|
||||
def __init__(self, ai_settings, screen, ship):
|
||||
#'''在飞船所处的位置创建一个子弹对象'''
|
||||
class Bullet(Sprite):
|
||||
"""A class to manage bullets fired from the ship."""
|
||||
|
||||
def __init__(self, ai_settings, screen, ship):
|
||||
"""Create a bullet object, at the ship's current position."""
|
||||
super(Bullet, self).__init__()
|
||||
self.screen = screen
|
||||
|
||||
#在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
|
||||
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
|
||||
self.rect.centerx = ship.rect.centerx
|
||||
|
||||
# Create bullet rect at (0, 0), then set correct position.
|
||||
self.rect = pygame.Rect(0, 0, ai_settings.bullet_width,
|
||||
ai_settings.bullet_height)
|
||||
self.rect.centerx = ship.rect.centerx
|
||||
self.rect.top = ship.rect.top
|
||||
|
||||
#存储用小数表示的子弹位置
|
||||
# Store a decimal value for the bullet's position.
|
||||
self.y = float(self.rect.y)
|
||||
self.color = ai_settings.bullet_color
|
||||
self.speed_factor = ai_settings.bullet_speed_factor
|
||||
|
||||
def update(self):
|
||||
#'''向上移动子弹'''
|
||||
#更新表示子弹位置的小数值
|
||||
self.y -= self.speed_factor
|
||||
#跟新表示子弹的rect的位置
|
||||
self.rect.y = self.y
|
||||
|
||||
def draw_bullet(self):
|
||||
#'''在屏幕上绘制子弹'''
|
||||
self.color = ai_settings.bullet_color
|
||||
self.speed_factor = ai_settings.bullet_speed_factor
|
||||
|
||||
def update(self):
|
||||
"""Move the bullet up the screen."""
|
||||
# Update the decimal position of the bullet.
|
||||
self.y -= self.speed_factor
|
||||
# Update the rect position.
|
||||
self.rect.y = self.y
|
||||
|
||||
def draw_bullet(self):
|
||||
"""Draw the bullet to the screen."""
|
||||
pygame.draw.rect(self.screen, self.color, self.rect)
|
||||
|
|
46
button.py
46
button.py
|
@ -1,31 +1,33 @@
|
|||
import pygame.font
|
||||
|
||||
class Button():
|
||||
def __init__(self, ai_settings, screen, msg):
|
||||
# """初始化按钮的属性"""
|
||||
self.screen = screen
|
||||
class Button():
|
||||
|
||||
def __init__(self, ai_settings, screen, msg):
|
||||
"""Initialize button attributes."""
|
||||
self.screen = screen
|
||||
self.screen_rect = screen.get_rect()
|
||||
|
||||
# 设置按钮的尺寸和其他属性
|
||||
self.width, self.height = 200, 50
|
||||
self.button_color = (0, 255, 0)
|
||||
self.text_color = (255, 255, 255)
|
||||
# Set the dimensions and properties of the button.
|
||||
self.width, self.height = 200, 50
|
||||
self.button_color = (0, 255, 0)
|
||||
self.text_color = (255, 255, 255)
|
||||
self.font = pygame.font.SysFont(None, 48)
|
||||
|
||||
# 创建按钮的rect对象, 并使其居中
|
||||
self.rect = pygame.Rect(0, 0, self.width, self.height)
|
||||
# Build the button's rect object, and center it.
|
||||
self.rect = pygame.Rect(0, 0, self.width, self.height)
|
||||
self.rect.center = self.screen_rect.center
|
||||
|
||||
# 按钮的标签只需创建一次
|
||||
self.prep_msg(msg)
|
||||
|
||||
def prep_msg(self, msg):
|
||||
# """将msg渲染为图像, 并使其在按钮上居中"""
|
||||
self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)
|
||||
self.msg_image_rect = self.msg_image.get_rect()
|
||||
self.msg_image_rect.center = self.rect.center
|
||||
# The button message only needs to be prepped once.
|
||||
self.prep_msg(msg)
|
||||
|
||||
def draw_button(self):
|
||||
# 绘制一个用颜色填充的按钮, 再绘制文本
|
||||
self.screen.fill(self.button_color, self.rect)
|
||||
self.screen.blit(self.msg_image, self.msg_image_rect)
|
||||
def prep_msg(self, msg):
|
||||
"""Turn msg into a rendered image, and center text on the button."""
|
||||
self.msg_image = self.font.render(msg, True, self.text_color,
|
||||
self.button_color)
|
||||
self.msg_image_rect = self.msg_image.get_rect()
|
||||
self.msg_image_rect.center = self.rect.center
|
||||
|
||||
def draw_button(self):
|
||||
# Draw blank button, then draw message.
|
||||
self.screen.fill(self.button_color, self.rect)
|
||||
self.screen.blit(self.msg_image, self.msg_image_rect)
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
import pygame
|
||||
from pygame.sprite import Sprite
|
||||
|
||||
class Ship(Sprite):
|
||||
|
||||
def __init__(self, ai_settings, screen):
|
||||
"""Initialize the ship, and set its starting position."""
|
||||
super(Ship, self).__init__()
|
||||
self.screen = screen
|
||||
self.ai_settings = ai_settings
|
||||
|
||||
# Load the ship image, and get its rect.
|
||||
self.image = pygame.image.load(r'ship.bmp')
|
||||
self.rect = self.image.get_rect()
|
||||
self.screen_rect = screen.get_rect()
|
||||
|
||||
# Start each new ship at the bottom center of the screen.
|
||||
self.rect.centerx = self.screen_rect.centerx
|
||||
self.rect.bottom = self.screen_rect.bottom
|
||||
|
||||
# Store a decimal value for the ship's center.
|
||||
self.center = float(self.rect.centerx)
|
||||
|
||||
# Movement flags.
|
||||
self.moving_right = False
|
||||
self.moving_left = False
|
||||
|
||||
def center_ship(self):
|
||||
"""Center the ship on the screen."""
|
||||
self.center = self.screen_rect.centerx
|
||||
|
||||
def update(self):
|
||||
"""Update the ship's position, based on movement flags."""
|
||||
# Update the ship's center value, not the rect.
|
||||
if self.moving_right and self.rect.right < self.screen_rect.right:
|
||||
self.center += self.ai_settings.ship_speed_factor
|
||||
if self.moving_left and self.rect.left > 0:
|
||||
self.center -= self.ai_settings.ship_speed_factor
|
||||
|
||||
# Update rect object from self.center.
|
||||
self.rect.centerx = self.center
|
||||
|
||||
def blitme(self):
|
||||
"""Draw the ship at its current location."""
|
||||
self.screen.blit(self.image, self.rect)
|
Loading…
Reference in New Issue