设为首页收藏本站

Crossin的编程教室

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

【Pygame 第10课】 命中目标

[复制链接]

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

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

32#
发表于 2018-4-16 23:03:35 |只看该作者
在你的checkHit里面加上print输出调试,
看看是函数没掉用到,还是判断出错,还是重置没成功。
#==== 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, 下载次数: 260)

捕获.PNG

回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

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

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

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

使用道具 举报

174

主题

45

好友

10万

积分

管理员

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

37#
发表于 2018-6-24 22:55:47 |只看该作者
crossin先生 发表于 2018-6-24 13:30
你需要有个间隔时间,不要让子弹那么快出来,不然你一共就5颗,后面就出不来了
代码里是有 interval_b 这 ...

Interval_b我试过200, 我之前是30, 就是加了碰撞代码后,就出现异常了。我一共确实也就5颗,但是后面也会出来,5颗一组5颗一组的射出来。这是我的代码:
import pygame
from sys import exit
import random


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-=5
        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 Air:
    def __init__(self):
        self.x=0
        self.y=0
        self.image=pygame.image.load('air.png').convert_alpha()

    def Move(self):
        mousex,mousey=pygame.mouse.get_pos()
        self.x=mousex-self.image.get_width()/2
        self.y=mousey-self.image.get_height()/2


class Enemy:
    def restart(self):
        self.x=random.uniform(30,420)
        self.y=random.uniform(-200,-50)
        
    def __init__(self):
        self.restart()
        self.image=pygame.image.load('enemy.png')
        self.speed=0.08

    def move(self):
        if self.y>608:
            self.speed+=0.01
            self.restart()
            
        else:
            self.y=self.y+self.speed
            
            

pygame.init()
screen=pygame.display.set_mode((450,608))
pygame.display.set_caption('Star War')
background=pygame.image.load('background.png').convert()
interval_b=0
index_b=0
bullets=[]

for i in range(10):
    bullets.append(Bullet())

air=Air()
enemies=[]
for i in range(6):
    enemies.append(Enemy())

#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

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=30
        index_b=(1+index_b)%10
    for e in enemies:
        e.move()
        screen.blit(e.image,(e.x,e.y))
    for b in bullets:
        if b.active:
            #for e in enemies:
             #   checkHit(e,b)
            b.move()
            screen.blit(b.image,(b.x,b.y))
    air.Move()
    screen.blit(air.image,(air.x,air.y))
    pygame.display.update()
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

38#
发表于 2018-6-25 23:12:35 |只看该作者
皮一下 发表于 2018-6-24 22:55
Interval_b我试过200, 我之前是30, 就是加了碰撞代码后,就出现异常了。我一共确实也就5颗,但是后面也 ...

你5颗子弹,为什么这里改成了
index_b=(1+index_b)%10

对照之前代码再看看
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

39#
发表于 2018-6-26 03:01:33 |只看该作者
crossin先生 发表于 2018-6-25 23:12
你5颗子弹,为什么这里改成了
index_b=(1+index_b)%10

haha这里我乱改子弹就是测试下。。我找到问题所在了。。b.move()那一块的代码缩进我搞错了。现在正常了。谢谢
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

40#
发表于 2018-6-26 03:03:21 |只看该作者
crossin先生 发表于 2018-6-25 23:12
你5颗子弹,为什么这里改成了
index_b=(1+index_b)%10

我用的python3.7 现在都没有适配3.7转换成exe的文件。该咋办,找了好多教程最多也只有3.6
回复

使用道具 举报

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

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

GMT+8, 2024-4-27 03:05 , Processed in 0.032731 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部