zoukankan      html  css  js  c++  java
  • 打飞机游戏【来源于Crossin的编程教室 http://chuansong.me/account/crossincode 】

    
    
    # -*- coding: utf-8 -*-
    import pygame
    from sys import exit
    class Bullet:
        def __init__(self):
            #初始化成员变量,x,y,image
            self.x = 0
            self.y = -1
            self.image = pygame.image.load('bullet.png').convert_alpha()
    
        def move(self):
            #处理子弹的运动
            if self.y < 0:
                mouseX, mouseY = pygame.mouse.get_pos()
                self.x = mouseX - self.image.get_width() / 2
                self.y = mouseY - self.image.get_height() / 2
            else:
                self.y -= 5
    pygame.init()
    screen = pygame.display.set_mode((450, 800), 0, 32)
    pygame.display.set_caption("Hello, World!")
    background = pygame.image.load('back.jpg').convert()
    plane = pygame.image.load('plane.png').convert_alpha()
    bullet = Bullet()
    #加载子弹图像
    #初始化子弹位置
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
        screen.blit(background, (0,0))
        bullet.move()
        screen.blit(bullet.image, (bullet.x, bullet.y))
        #把子弹画到屏幕上
        x, y = pygame.mouse.get_pos()
        x-= plane.get_width() / 2
        y-= plane.get_height() / 2
        screen.blit(plane, (x, y))
        pygame.display.update()
    
    
    
    # -*- coding: utf-8 -*-
    import pygame
    #导入pygame库
    from sys import exit
    #向sys模块借一个exit函数用来退出程序
    pygame.init()
    #初始化pygame,为使用硬件做准备
    screen = pygame.display.set_mode((600, 170), 0, 32)
    #创建了一个窗口,窗口大小和背景图片大小一样
    pygame.display.set_caption("Hello, World!")
    #设置窗口标题
    background = pygame.image.load('0.jpg').convert()
    #加载并转换图像
    plane = pygame.image.load('2.jpg').convert()
    while True:
    #游戏主循环
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                #接收到退出事件后退出程序
                pygame.quit()
                exit()
     
        screen.blit(background, (0,0))
        x, y = pygame.mouse.get_pos()
        #获取鼠标位置
        x-= plane.get_width() / 2
        y-= plane.get_height() / 2
        #计算飞机的左上角位置
        screen.blit(plane, (x,y))
        #把飞机画到屏幕上
        pygame.display.update()
        #刷新一下画面
    # -*- coding: utf-8 -*-
    import pygame
    from sys import exit
    pygame.init()
    screen = pygame.display.set_mode((450, 800), 0, 32)
    pygame.display.set_caption("Hello, World!")
    background = pygame.image.load('back.jpg').convert()
    plane = pygame.image.load('plane.png').convert_alpha()
    bullet = pygame.image.load('bullet.png').convert_alpha()
    #加载子弹图像
    bullet_x = 0
    bullet_y = -1
    #初始化子弹位置
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
        screen.blit(background, (0,0))
        x, y = pygame.mouse.get_pos()
        if bullet_y < 0:
            #如果子弹位置超出了屏幕上端
            bullet_x = x - bullet.get_width() / 2
            bullet_y = y - bullet.get_height() / 2
            #把子弹的中心位置设为鼠标坐标
        else:
            bullet_y -= 5
            #子弹的位置往上移
        screen.blit(bullet, (bullet_x, bullet_y))
        #把子弹画到屏幕上
        x-= plane.get_width() / 2
        y-= plane.get_height() / 2
        screen.blit(plane, (x, y))
        pygame.display.update()
    # -*- coding: utf-8 -*-
    import pygame
    import random
    from sys import exit
    class Bullet:
        def __init__(self):
            #初始化成员变量,x,y,image
            self.x = 0
            self.y = -1
            self.image = pygame.image.load('bullet.png').convert_alpha()
            self.active = False
    
        def move(self):
            if self.active:
                self.y-=3
            if self.y < 0:
                self.active =False
            #处理子弹的运动
        def restart(self):
                mouseX, mouseY = pygame.mouse.get_pos()
                self.x = mouseX - self.image.get_width() / 2
                self.y = mouseY - self.image.get_height() / 2
                self.active = True
    class Enemy:
        def restart(self):
            self.x = random.randint(50, 400)
            self.y = random.randint(-200, -50)
            self.speed = random.random() + 0.1
        def __init__(self):
            self.restart()
            self.image = pygame.image.load('enemy.png').convert_alpha()
    
        def move(self):
            if self.y < 800:
                self.y += self.speed
            else:
                self.restart()
    class Plane:
        def restart(self):
            self.x = 200
            self.y = 600
    
        def __init__(self):
            self.restart()
            self.image = pygame.image.load('plane.png').convert_alpha()
    
        def move(self):
            x, y = pygame.mouse.get_pos()
            x-= self.image.get_width() / 2
            y-= self.image.get_height() / 2
            self.x = x
            self.y = y
    def checkCrash(enemy, plane):
        if (plane.x + 0.7*plane.image.get_width() > enemy.x) and (plane.x + 0.3*plane.image.get_width() < enemy.x + enemy.image.get_width()) and (plane.y + 0.7*plane.image.get_height() > enemy.y) and (plane.y + 0.3*plane.image.get_width() < enemy.y + enemy.image.get_height()):
            return True
        return False
    def checkHit(enemy, bullet):
        if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (
            bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()
        ):
            enemy.restart()
            bullet.active = False
            #增加返回值
            return True
        return False
    pygame.init()
    screen = pygame.display.set_mode((450, 800), 0, 32)
    pygame.display.set_caption("Hello, World!")
    background = pygame.image.load('back.jpg').convert()
    plane = Plane()
    bullets = []
    for i in range(10):
        bullets.append(Bullet())
    count_b =len(bullets)
    index_b=0
    interval_b=0
    gameover = False
    score = 0
    font = pygame.font.Font(None, 32)
    enemies = []
    for i in range(5):
        enemies.append(Enemy())
    #加载子弹图像
    #初始化子弹位置
    while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        exit()
                    if gameover and event.type == pygame.MOUSEBUTTONUP:
                        plane.restart()
                        for e in enemies:
                            e.restart()
                        for b in bullets:
                            b.active = False
                        score = 0
                        gameover = False
                interval_b-=1
                screen.blit(background, (0,0))
                if  not gameover:
                    if interval_b<0:
                        bullets[index_b].restart()
                        interval_b=100
                        index_b=(index_b+1)%count_b
                    for b in bullets:
                        if b.active:
                            for e in enemies:
                                if checkHit(e, b):
                                    score+=100
                                b.move()
                            screen.blit(b.image,(b.x,b.y))
                    for e in enemies:
                        if checkCrash(e, plane):
                            gameover = True
                        e.move()
                        screen.blit(e.image, (e.x, e.y))
                    screen.blit(plane.image, (plane.x, plane.y))
                    plane.move()
                    text = font.render("Socre: %d" % score, 1, (0, 0, 0))
                    screen.blit(text, (0, 50))
                else:
                    text = font.render("Socre: %d" % score, 1, (0, 0, 0))
                    screen.blit(text, (190, 400))
                pygame.display.update()
  • 相关阅读:
    SSH免密码登陆备忘
    WeiBo官网oauth2开发文档理解
    TOP
    使用定位,逆地理编码,经纬度《=转=》地址信息、逆地理编码,地址《=转=》经纬度,贼方便!!!!
    计算机病毒分类之感染目标
    预处理
    指针与引用
    printf问题参数顺序
    神奇的求平均数
    C和C++的关系
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4612757.html
Copyright © 2011-2022 走看看