博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
打飞机游戏【来源于Crossin的编程教室 http://chuansong.me/account/crossincode 】
阅读量:5261 次
发布时间:2019-06-14

本文共 6803 字,大约阅读时间需要 22 分钟。

 
# -*- coding: utf-8 -*-import pygamefrom sys import exitclass 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 -= 5pygame.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 pygamefrom sys import exitpygame.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 = 0bullet_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 pygameimport randomfrom sys import exitclass 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 = Trueclass 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 = ydef 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 Falsedef 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 Falsepygame.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=0interval_b=0gameover = Falsescore = 0font = 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()

 

转载于:https://www.cnblogs.com/zhuyaguang/p/4612757.html

你可能感兴趣的文章
Linux服务器在外地,如何用eclipse连接hdfs
查看>>
react双组件传值和传参
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
vue route 跳转
查看>>