PYTHON第四天(飞机大战)

算是差不多能搞懂PYGAME里的Sprite大致意思了…设置Sprite的意义在于将每个子弹变成一个小元素(小精灵的意思)一方面便于存储,一方面可以进行调用。

class Bullet(Sprite):
    def __init__(self,ai_settings,screen,ship):
        super(Bullet,self).__init__()
        self.screen = screen
        #开始制作子弹的形状
        self.rect = pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        self.x = float(self.rect.x)
        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

这个意义在于设置每个子弹的属性都统一,并且将其定义到pygame里的屏幕上,一个定义是定义位置在飞船的头部方向(ship.rect.top),另一个是定义在飞船的中间部分(ship.rect.centerx)每个子弹的意义在于从飞船头部发出,现在我们再返回到主程序。

import pygame
import sys

from settings1 import plant_game
from ship1 import Ship
import checkcheck as gf
from pygame.sprite import Group
~~这个Group是我昨天没弄懂的

def run_game():
    pygame.display.set_caption("Alien Invasion")
    ai_setting = plant_game()
    ship = Ship(ai_setting,ai_setting.screen)
    bullets = Group()
**就是这里的,bullets =Group,现在我明白了,应该是将其定义为一个完整的子弹组。**
    # ship看做是一个矩形
    while True:
        gf.check_events(ship,ai_setting,ai_setting.screen,bullets)
        gf.update_screen(ai_setting,ai_setting.screen,ship,bullets)
        #def update_screen(ai_settings,screen,ship,bullets)
	    #screen.fill(ai_settings.bg_color)
	    #for bullet in bullets.sprites():
	        #bullet.draw_bullets()
	        #这里的意义在于,将Group填充子弹,Draw_bullets()进行子弹填充,然后建立一个完整的子弹Group。
        gf.update_bullets(bullets)
        #当然,不能让你子弹无限的发射吧。这里我们设置了一个判定子弹是否飞跃出的屏幕的函数。
        def update_bullets(bullets):
    		bullets.update()
		    for bullet in bullets.copy():
		        if bullet.rect.bottom <= 0:
		            bullets.remove(bullet)
		            print(len(bullets))
		            #这里的bullets就是Group,我们进行判断是否每个子弹的位置是不是超出去了,超出去我们就remove它。
       	


        gf.update_screen(ai_setting,ai_setting.screen,ship,bullets)
run_game()




run_game()

OK,虽然写的很烂我会继续加油的。今天学习时间太短了,明天加油,仍然没有写出如何进行一直按空格发射子弹,明天接着试试。

猜你喜欢

转载自blog.csdn.net/weixin_49712647/article/details/112598428