python 打飞机项目 (实战二) 创建敌机

HeroPlane.py 文件

#-*- coding:utf-8 -*-
import pygame

class HeroPlane(object):
    direction = 'right'

    def __init__ (self, screen_temp, x, y):
        self.screen = screen_temp
        self.x = x
        self.y = y
        self.img_load()

    def img_load (self):
        self.image = pygame.image.load("./resource/enemy-1.gif")

    def display (self):
        self.screen.blit(self.image, (self.x, self.y))

    def move (self):
        if self.direction == 'right':
            self.x += 5
        elif self.direction == 'left':
            self.x -= 5

        if self.x > 480 - 50:
            self.direction = 'left'
        elif self.x <= 0:
            self.direction = 'right'

main.py 文件

#-*- coding:utf-8 -*-
import pygame, time
from Plane import Plane
from HeroPlane import HeroPlane
from pygame.locals import *

def key_control (plane_temp):
    # 获取事件,比如按键等
    for event in pygame.event.get():

        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                plane_temp.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                plane_temp.move_right()
            # 检测按键是否是空格键
            elif event.key == K_SPACE:
                print('space')
                plane_temp.fire()

def main ():
    screen = pygame.display.set_mode((480, 852), 0, 32)
    background = pygame.image.load("./resource/background.png")

    # 创建一个飞机对象
    plane = Plane(screen, 210, 700)

    # 创建敌机对象
    heroplane = HeroPlane(screen,0,0)

    while True:
        screen.blit(background, (0, 0))
        plane.display()
        heroplane.display()
        heroplane.move()
        key_control(plane)
        pygame.display.update()
        time.sleep(0.01)

if __name__ == '__main__':
    main()

file

本作品采用《CC 协议》,转载必须注明作者和本文链接
by JeffreyBool blog :point_right: link
JeffreyBool
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!