pygame经典游戏space invader太空入侵核心原理程序

"""pygame经典游戏space invader太空入侵核心原理程序,本程序给作品加了封面,做了结尾设计,读者可自行完善."""

__author__ = "李兴球"
__date__ = "2018年11月"

import pygame
from pygame.locals import *

class Block(pygame.sprite.Sprite):
    def __init__(self,width,height,color,x,y,screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.width = width
        self.height = height
        "敌人的图像,可自行更换成漂亮的图像用pygame.image.load命令"
        self.image = pygame.Surface((width,height))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.centerx,self.rect.centery = x,y
        self.xspeed = 0
        self.yspeed = height
        
    def update(self):
        self.rect.move_ip(self.xspeed,self.yspeed)

    def draw(self):
        self.screen.blit(self.image,self.rect)
        
def make_enemis():
    
    cyan = (0,255,255)
    enemy_group = pygame.sprite.Group()          # 创建组
    startx,starty = 50,50                        # 起始x,y坐标
    for x in range(startx,screenwidth,100):  
        for y in range(starty,screenheight - 2*100,50):
           enemy_group.add(Block(50,25,cyan,x,y,screen))
           
    return enemy_group

def make_fighter():
    
    fighter = Block(50,25,(255,0,0),screenwidth//2,screenheight - 50,screen)
    return fighter

def shoot(posx,posy):
    
    red = (255,0,0)
    bullete = Block(15,25,red,posx,posy,screen)
    bullete.yspeed = -5
    bullete_group.add(bullete)
    
def display_shell(image):
    """显示封面"""
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == MOUSEBUTTONDOWN:running = False
        screen.blit(image,(0,0))
        pygame.display.update()
    

You must log in to read the rest of this article. Please log in or register as a user.