-
#myplane.py
-
import pygame
-
-
class MyPlane(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image1 = pygame.image.load("images/me1.png").convert_alpha()
-
self.image2 = pygame.image.load("images/me2.png").convert_alpha()
-
self.destroy_images = []
-
self.destroy_images.extend([\
-
pygame.image.load("images/me_destroy_1.png").convert_alpha(), \
-
pygame.image.load("images/me_destroy_2.png").convert_alpha(), \
-
pygame.image.load("images/me_destroy_3.png").convert_alpha(), \
-
pygame.image.load("images/me_destroy_4.png").convert_alpha() \
-
])
-
self.rect = self.image1.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.rect.left, self.rect.top = \
-
(self.width - self.rect.width) // 2, \
-
self.height - self.rect.height - 60
-
self.speed = 10
-
self.active = True
-
self.invincible = False
-
self.mask = pygame.mask.from_surface(self.image1)
-
-
def moveUp(self):
-
if self.rect.top > 0:
-
self.rect.top -= self.speed
-
else:
-
self.rect.top = 0
-
-
def moveDown(self):
-
if self.rect.bottom < self.height - 60:
-
self.rect.top += self.speed
-
else:
-
self.rect.bottom = self.height - 60
-
-
def moveLeft(self):
-
if self.rect.left > 0:
-
self.rect.left -= self.speed
-
else:
-
self.rect.left = 0
-
-
def moveRight(self):
-
if self.rect.right < self.width:
-
self.rect.left += self.speed
-
else:
-
self.rect.right = self.width
-
-
def reset(self):
-
self.rect.left, self.rect.top = \
-
(self.width - self.rect.width) // 2, \
-
self.height - self.rect.height - 60
-
self.active = True
-
self.invincible = True
-
-
#enemy.py
-
import pygame
-
from random import *
-
-
class SmallEnemy(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/enemy1.png").convert_alpha()
-
self.destroy_images = []
-
self.destroy_images.extend([\
-
pygame.image.load("images/enemy1_down1.png").convert_alpha(), \
-
pygame.image.load("images/enemy1_down2.png").convert_alpha(), \
-
pygame.image.load("images/enemy1_down3.png").convert_alpha(), \
-
pygame.image.load("images/enemy1_down4.png").convert_alpha() \
-
])
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.speed = 2
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-5 * self.height, 0)
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.reset()
-
-
def reset(self):
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-5 * self.height, 0)
-
-
-
class MidEnemy(pygame.sprite.Sprite):
-
energy = 8
-
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/enemy2.png").convert_alpha()
-
self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()
-
self.destroy_images = []
-
self.destroy_images.extend([\
-
pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
-
pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
-
pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
-
pygame.image.load("images/enemy2_down4.png").convert_alpha() \
-
])
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.speed = 1
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-10 * self.height, -self.height)
-
self.mask = pygame.mask.from_surface(self.image)
-
self.energy = MidEnemy.energy
-
self.hit = False
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.reset()
-
-
def reset(self):
-
self.active = True
-
self.energy = MidEnemy.energy
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-10 * self.height, -self.height)
-
-
-
class BigEnemy(pygame.sprite.Sprite):
-
energy = 20
-
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
-
self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
-
self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()
-
self.destroy_images = []
-
self.destroy_images.extend([\
-
pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down6.png").convert_alpha() \
-
])
-
self.rect = self.image1.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.speed = 1
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-15 * self.height, -5 * self.height)
-
self.mask = pygame.mask.from_surface(self.image1)
-
self.energy = BigEnemy.energy
-
self.hit = False
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.reset()
-
-
def reset(self):
-
self.active = True
-
self.energy = BigEnemy.energy
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-15 * self.height, -5 * self.height)
-
-
-
#bullet.py
-
import pygame
-
-
class Bullet1(pygame.sprite.Sprite):
-
def __init__(self, position):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/bullet1.png").convert_alpha()
-
self.rect = self.image.get_rect()
-
self.rect.left, self.rect.top = position
-
self.speed = 12
-
self.active = False
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
self.rect.top -= self.speed
-
-
if self.rect.top < 0:
-
self.active = False
-
-
def reset(self, position):
-
self.rect.left, self.rect.top = position
-
self.active = True
-
-
class Bullet2(pygame.sprite.Sprite):
-
def __init__(self, position):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/bullet2.png").convert_alpha()
-
self.rect = self.image.get_rect()
-
self.rect.left, self.rect.top = position
-
self.speed = 14
-
self.active = False
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
self.rect.top -= self.speed
-
-
if self.rect.top < 0:
-
self.active = False
-
-
def reset(self, position):
-
self.rect.left, self.rect.top = position
-
self.active = True
-
-
#supply.py
-
import pygame
-
from random import *
-
-
class Bullet_Supply(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/bullet_supply.png").convert_alpha()
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
-
self.speed = 5
-
self.active = False
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.active = False
-
-
def reset(self):
-
self.active = True
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
-
-
class Bomb_Supply(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/bomb_supply.png").convert_alpha()
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
-
self.speed = 5
-
self.active = False
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.active = False
-
-
def reset(self):
-
self.active = True
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
-
至此,Pygame 的內容就講解完畢了。
至此,《零基礎入門學習Python》的課程講解也完畢了。
接下來有新的更高級的課程哦。
0 留言:
發佈留言