设为首页收藏本站

Crossin的编程教室

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

【Pygame 第10课】 命中目标

[复制链接]

5

主题

1

好友

87

积分

注册会员

Rank: 2

楼主
发表于 2018-6-24 11:16:59 |显示全部楼层
我加了命中目标这段代码后,比如说我有5颗子弹,这五颗子弹会5颗一条5颗一条的射出来,和之前的不一样了。是不是电脑处理不行了
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

沙发
发表于 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()
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

板凳
发表于 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

地板
发表于 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
回复

使用道具 举报

5

主题

1

好友

87

积分

注册会员

Rank: 2

5#
发表于 2018-6-26 22:48:12 |显示全部楼层
crossin先生 发表于 2018-6-26 15:30
这个我也没办法,exe支持没那么快,你试试3.6的行不行。
不行的话,如果一定要打包,你可以建一个3.6的虚 ...

我用了pyinstaller,3.5,3.6都试过没用。
回复

使用道具 举报

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

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

GMT+8, 2024-5-3 22:45 , Processed in 0.019995 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部