PYTHON Day Four (Aircraft War)

I can almost understand the general meaning of Sprite in PYGAME... The meaning of setting up Sprite is to turn each bullet into a small element (the meaning of an elf) on the one hand, it is easy to store, and on the other hand, it can be called.

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

The significance of this is to set the attributes of each bullet to be unified and define it on the screen in pygame. One definition is to define the position in the direction of the ship's head (ship.rect.top), and the other is to define the position in the middle of the ship. Part (ship.rect.centerx) The meaning of each bullet is to be emitted from the head of the spacecraft. Now we return to the main program.

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, although the writing is bad, I will continue to cheer. The study time today is too short. Come on tomorrow. I still haven't written out how to proceed. Press the space to fire the bullets. I will try again tomorrow.

Guess you like

Origin blog.csdn.net/weixin_49712647/article/details/112598428