设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
12
返回列表 发新帖
楼主: crossin先生
打印 上一主题 下一主题

【Pygame 第11课】 GAME OVER

[复制链接]

0

主题

0

好友

137

积分

注册会员

Rank: 2

11#
发表于 2017-12-26 11:33:08 |只看该作者
先生,if checkCrash(e, plane):这个是在判断什么?比如说上面那个if True是在条件为真的时候运行下一步,if后面加个函数,什么情况下运行下一步呢?
回复

使用道具 举报

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循环
回复

使用道具 举报

0

主题

0

好友

8

积分

新手上路

Rank: 1

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

使用道具 举报

0

主题

0

好友

4

积分

新手上路

Rank: 1

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

使用道具 举报

174

主题

45

好友

10万

积分

管理员

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
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

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

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

17#
发表于 2018-6-29 16:31:14 |只看该作者
在e.move 后面加个e的位置判断,触底了就
gameover = True
#==== 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

好友

10万

积分

管理员

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

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

谢谢 是缩进的问题
回复

使用道具 举报

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

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

GMT+8, 2024-4-24 09:33 , Processed in 0.027748 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部