设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Pygame 第10课】 命中目标

[复制链接]

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

36#
发表于 2018-6-24 13:30:20 |只看该作者
皮一下 发表于 2018-6-24 11:16
我加了命中目标这段代码后,比如说我有5颗子弹,这五颗子弹会5颗一条5颗一条的射出来,和之前的不一样了。 ...

你需要有个间隔时间,不要让子弹那么快出来,不然你一共就5颗,后面就出不来了
代码里是有 interval_b 这个参数的,调大一点试试
这不是你电脑不行,可能正相反,是计算太快了
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

35#
发表于 2018-6-24 11:16:59 |只看该作者
我加了命中目标这段代码后,比如说我有5颗子弹,这五颗子弹会5颗一条5颗一条的射出来,和之前的不一样了。是不是电脑处理不行了
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

34#
发表于 2018-6-6 17:15:43 |只看该作者
koalaoycx 发表于 2018-6-6 15:53
#定义命中函数
def checkHit(enemy,bullet):
    if (bullet.x > enemy.x and bullet.x < enemy.x+enemy.im ...

代码没看出问题
再调试下吧,输出看看那句blit有没有被执行到,传给blit的参数是多少,还有把你的图片换下试试
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

35

积分

新手上路

Rank: 1

33#
发表于 2018-6-6 15:53:23 |只看该作者
#定义命中函数
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()):
        screen.blit(boom,(enemy.x,enemy.y))
        enemy.restart()
        bullet.active=False


先生您好,我想给被击中的敌机加一个boom的特效,但不知为什么boom图片好像是随机出现的,请问应该怎么修改呢?

捕获.PNG (10.15 KB, 下载次数: 297)

捕获.PNG

回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

32#
发表于 2018-4-16 23:03:35 |只看该作者
在你的checkHit里面加上print输出调试,
看看是函数没掉用到,还是判断出错,还是重置没成功。
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

2

主题

0

好友

66

积分

注册会员

Rank: 2

31#
发表于 2018-4-16 11:13:17 |只看该作者
我的飞机打中之后没有被重置是为什么?
# -*- coding: utf-8 -*-
import pygame, random
from sys import exit





class Bullet:
    def __init__(self):
        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, 800)
        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()

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


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()
# 创建子弹的list
bullets = []
# 向list添加5发子弹
for i in range(5):
    bullets.append(Bullet())
# 子弹总个数
count_b = len(bullets)
# 即将激发的子弹序号
index_b = 0
# 发射子弹的间隔
interval_b = 0
# 创建敌机的list
enemies = []
# 向list中添加5架敌机
for i in range(5):
    enemies.append(Enemy())
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    screen.blit(background, (0, 0))
    interval_b -= 1
    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:
            b.move()
            screen.blit(b.image, (b.x, b.y))
    for b in bullets:
        if b.active:
            for e in enemies:
                checkHit(e, b)
    # 处理每架敌机的运动
    for e in enemies:
        e.move()
        screen.blit(e.image, (e.x, e.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()
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

30#
发表于 2018-3-15 17:15:12 |只看该作者
skybeak 发表于 2018-3-14 16:18
请问判断飞机是否被打中的那段代码
            for e in enemies:
                checkHit(e, b)

可以,一个功能可以有不同的实现
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

148

积分

注册会员

Rank: 2

29#
发表于 2018-3-14 16:18:22 |只看该作者
请问判断飞机是否被打中的那段代码
            for e in enemies:
                checkHit(e, b)
是不是也可以放在别的位置,比如单独出去,或者是处理每个飞机运动的for循环里,比如
    for e in enemies:
        checkHit(e, b)
        e.move()
        screen.blit(e.image, (e.x, e.y))
两处有什么不同么?只是响应时间会有所不同,还是会有很多其他的不一样,谢谢老师
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

28#
发表于 2018-1-12 16:32:15 |只看该作者
Youngkuso 发表于 2018-1-12 13:14
不仅延迟,还能感觉到打到敌机的子弹会明显变慢

你的代码里不是击中敌机就 actice False ,然后不移动了吗
你这个要自己调试代码来看的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

35

积分

新手上路

Rank: 1

27#
发表于 2018-1-12 13:14:33 |只看该作者
本帖最后由 Youngkuso 于 2018-1-12 15:10 编辑

不仅延迟,还能感觉到打到敌机的子弹会明显变慢
回复

使用道具 举报

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

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

GMT+8, 2024-5-17 19:34 , Processed in 0.030686 second(s), 25 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部