• python 打飞机项目 (实战一)


    第一步定义 main 函数:

    # -*- coding=utf-8 -*-
    import pygame,time
    from Plane import Plane
    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)
    
        while True:
            screen.blit(background, (0, 0))
            plane.display()
            key_control(plane)
            pygame.display.update()
            time.sleep(0.01)
    
    if __name__ == '__main__':
        main()

    创建飞机类


    import pygame
    from Bullet import Bullet
    
    class Plane(object):
    
        image = None
    
        bullet_list = []
    
        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/hero1.png")
    
        def display(self):
            self.screen.blit(self.image, (self.x, self.y))
            for bullet in self.bullet_list:
                print(bullet)
                bullet.display()
                bullet.move()
    
        def move_left(self):
            self.x -= 10
    
        def move_right(self):
            self.x += 10
    
        def fire(self):
            self.bullet_list.append(Bullet(self.screen, self.x, self.y))

    创建子弹类:

    import pygame
    
    class Bullet(object):
        def __init__ (self, screen_temp, x, y):
            self.x = x + 40
            self.y = y - 20
            self.screen = screen_temp
            self.img_load()
    
        def img_load(self):
            self.image = pygame.image.load("./resource/bullet.png")
    
        def display(self):
            self.screen.blit(self.image, (self.x, self.y))
    
        def move(self):
            self.y -= 20
    如果有问题可加群:887934385  可分享相关资料
  • 相关阅读:
    什么是重名问题,树型目录结构如何解决这一问题。
    什么是当前目录,什么是相对路径名。
    什么是文件路径名?
    Centos6.7安装chrome
    关于Java开发过程中质量提升-2自动化
    关于Java开发过程中质量提升-1代码格式配置
    java质量提升相关
    完成wamp安装后Mysql配置记录
    树莓派raspi2-ubuntu meta安装配置指导
    mysql数据库批量操作
  • 原文地址:https://www.cnblogs.com/pypypy/p/12026081.html
Copyright © 2020-2023  润新知