设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 25033|回复: 19
打印 上一主题 下一主题

【Pygame 第11课】 GAME OVER

[复制链接]

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
楼主
发表于 2013-9-5 19:18:19 |只看该作者 |正序浏览
继续我们的打飞机游戏。完成了子弹和敌机之间的碰撞检测之后,自然还要来处理敌机与本体之间的碰撞检测,这决定了游戏是否结束。

之前我们没有把plane作为一个对象来处理,现在为了能更方便地做碰撞检测,我们还是要把它封装一下。这和我们之前对bullet和enemy所做的操作类似。
  1. class Plane:
  2.     def restart(self):
  3.         self.x = 200
  4.         self.y = 600
  5.         
  6.     def __init__(self):
  7.         self.restart()
  8.         self.image = pygame.image.load('plane.png').convert_alpha()

  9.     def move(self):
  10.         x, y = pygame.mouse.get_pos()
  11.         x-= self.image.get_width() / 2
  12.         y-= self.image.get_height() / 2
  13.         self.x = x
  14.         self.y = y

  15. plane = Plane()
复制代码
在move方法中,依旧根据鼠标的位置改变飞机的位置。

然后我们增加一个checkCrash的函数,和checkHit类似,它用来处理敌机和本体之间的碰撞。
  1. def checkCrash(enemy, plane):
  2.     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_height() < enemy.y + enemy.image.get_height()):
  3.         return True
  4.     return False
复制代码
这里的判断比之前要复杂一些,因为敌机和本体都有一定的面积,不能像子弹一样忽略长宽。但如果两张图片一旦有重合就算是碰撞,会让游戏看上去有些奇怪:有时候你觉得并没有撞上,而实际已经有了重合,游戏就失败了。所以为了避免这一现象,我们要给plane的长宽打上一点折扣。这也就是代码中判断条件里“0.3”“0.7”的意义所在。

checkCrash把碰撞检测的结果用True或False返回。在游戏主循环里,我们增加一个记录游戏是否结束的变量gameover。把之前的游戏逻辑放在gameover为False的情况下。而当checkCrash为True时,就把gameover设为True。
  1. gameover = False
  2. while True:
  3.     ###
  4.     if not gameover:
  5.         ###省略部分游戏逻辑
  6.         for e in enemies:
  7.             #如果撞上敌机,设gameover为True
  8.             if checkCrash(e, plane):
  9.                 gameover = True
  10.             e.move()
  11.             screen.blit(e.image, (e.x, e.y))
  12.         #检测本体的运动
  13.         plane.move()
  14.         screen.blit(plane.image, (plane.x, plane.y))
  15.     else:
  16.         #待处理
  17.         pass
复制代码
运行代码,当你不幸被敌机撞上后,游戏陷入一片空白。然后,你只好关闭程序。下一课,我们来处理被撞后的善后工作。

#==== Crossin的编程教室 ====#
微信ID:crossincode
论坛:http://crossin.me
QQ群:156630350

面向零基础初学者的编程课
每天5分钟,轻松学编程



11.py

3.38 KB, 下载次数: 612

源码

#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

56

积分

注册会员

Rank: 2

20#
发表于 2018-8-7 09:09:26 |只看该作者
crossin先生 发表于 2018-8-6 23:45
结束是通过 gameover 来控制的
所以,去查 gameover 之后的代码,想想哪些该放在 not gameover
想不明白 ...

谢谢 是缩进的问题
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

19#
发表于 2018-8-6 23:45:59 |只看该作者
zyq 发表于 2018-8-6 16:59
请问 游戏能正常运行 但是碰撞之后 游戏没有陷入空白 只是飞机不能发子弹了 这是什么问题 代码如下:
impor ...

结束是通过 gameover 来控制的
所以,去查 gameover 之后的代码,想想哪些该放在 not gameover
想不明白的话对照示例代码,注意看下缩进
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

56

积分

注册会员

Rank: 2

18#
发表于 2018-8-6 16:59:03 |只看该作者
请问 游戏能正常运行 但是碰撞之后 游戏没有陷入空白 只是飞机不能发子弹了 这是什么问题 代码如下:
import pygame
import random
from sys import exit
#定义一个bullet类,封装子弹相关的数据和方法
class Bullet:
    def __init__(self):
        self.x = 0
        self.y = -1
        self.image = pygame.image.load('bullet.png').convert_alpha()
        #初始化成员变量,x,y,image
        self.active = False

    def move(self):
    #激活状态下,向上移动
        if self.active:
            self.y -= 2
        #当飞出屏幕,就设为不激活
        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.jpg').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 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

#检测敌机与本地是否相撞
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_height() < enemy.y + enemy.image.get_height()
    ):
        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('z.jpg').convert()
#创建Plane对象
plane = Plane()
bullets =[]
#向list中添加5发子弹
for i in range(5):
    bullets.append(Bullet())
#子弹总数
count_b = len(bullets)
#即将激活子弹序号
index_b = 0
#发射子弹的间隔
interval_b = 0

#创建敌机list
enemies = []
#添加5个敌机
for i in range(5):
    enemies.append(Enemy())
#增加记录游戏是否结束变量
gameover = False
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    screen.blit(background, (0, 0))
    #当gameover为False时,游戏逻辑正常运行
    if not gameover:
    #发射间隔递减
        interval_b -= 1
    #当间隔小于0时,激活已发子弹
        if interval_b < 0:
             bullets[index_b].restart()
             #重置间隔时间
             interval_b = 200
             #子弹序号周期性递减
             index_b = (index_b + 1) % count_b
            #判断每个子弹的状态
    for b in bullets:
        #处于激活状态的子弹,移动位置并绘制
        if b.active:
            #检查每一刻active的子弹是否与敌机碰撞
            for e in enemies:
                checkHit(e,b)
            b.move()
            screen.blit(b.image,(b.x,b.y))

    for e in enemies:
    #如果撞上敌机,设gameover为True
        if checkCrash(e, plane):
            gameover = True
        e.move()
        screen.blit(e.image,(e.x,e.y))
        #检查本体的运动
        plane.move()
        screen.blit(plane.image,(plane.x,plane.y))
    else:
        #待处理
        pass
    pygame.display.update()
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

17#
发表于 2018-6-29 16:31:14 |只看该作者
在e.move 后面加个e的位置判断,触底了就
gameover = True
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

16#
发表于 2018-6-29 00:05:48 |只看该作者
那我如果想敌机碰到屏幕底部也算作GAMEOVER,要如何改写代码呢?因为之前的代码是重复利用碰到超出屏幕的敌机的。
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

15#
发表于 2018-3-31 22:14:33 |只看该作者
bmwh123 发表于 2018-3-31 18:52
plane.x 是飞机正中心的位置吗 那加上了0.7倍的宽度 不是就已经在飞机的图片外面了 ...

左上角
这个你在代码里试一试就清楚了
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

4

积分

新手上路

Rank: 1

14#
发表于 2018-3-31 18:52:39 |只看该作者
plane.x 是飞机正中心的位置吗 那加上了0.7倍的宽度 不是就已经在飞机的图片外面了
回复

使用道具 举报

0

主题

0

好友

8

积分

新手上路

Rank: 1

13#
发表于 2018-2-7 15:35:32 |只看该作者
跟着正确的慢慢敲几遍再说
回复

使用道具 举报

0

主题

0

好友

137

积分

注册会员

Rank: 2

12#
发表于 2017-12-26 15:44:35 |只看该作者
先生,我懂了,函数的返回值作为判断依据,if checkCrash(e, plane):如果撞上了,return True,下一个循环if not gameover 的bool值为Flase ,然后后面的程序不运行了,只有一个主循环while Ture循环
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-4 23:25 , Processed in 0.030318 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部