• 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  可分享相关资料
  • 相关阅读:
    Microsoft Azure 的负载平衡器的Session Sticky
    MySQL Performance tuning
    集装箱码头智能理货方案
    Iphone 英语语言下通讯录排序问题
    mac安装Aws cli失败
    vbs操作excel
    Sharepoint 2010 工作流启动时处理出错
    2、easyUI-创建 CRUD可编辑dataGrid(表格)
    1、easyUI-创建 CRUD普通dataGrid(表格)
    0327定时执行--存储过程--dbms_job--dbms_scheduler.create_job
  • 原文地址:https://www.cnblogs.com/pypypy/p/12026081.html
Copyright © 2020-2023  润新知