PYTHON Day Three (Aircraft War)

One thing to say, the book "PYTHON from entry to practice" is very messy to the chapter of PYGAME. . Really want to complain. . I didn't understand the complicated syntax of classes and classes before, so I didn't read subsequent chapters in this book. . It may also be that my own foundation is not solid. . The usage in many classes is really unclear. . Including GROUP and Spirte. . I'm bothered by this tonight. . Went to watch the video.

  • But there are still gains today... I have figured out why the plane moved up and down yesterday and went out, and the bullet was made today. . The call of the bullet is still not clear. . Wait and see other tutorials to find things in the parent category and sub category.
import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
#就是这个啥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.y = float(self.rect.y)
       self.color = ai_settings.bullet_color
       self.speed_factor = ai_settings.bullet_speed_factor
   def update(self):
       self.y -= self.speed_factor
       self.rect.y = self.y
   def draw_buller(self):
       pygame.draw.rect(self.screen,self.color,self.rect)

Then there is the calling bullet part, which is easy to understand. But after doing this, I keep firing bullets when I can't press the blank space. If I let go, I will stop firing bullets. Try it tomorrow~

import sys
import pygame
from bullet import Bullet
def check_keydown_events(event,ship,ai_settings,screen,bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
        print(event.key)
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
        print(event.key)
    elif event.key == pygame.K_UP:
        ship.moving_up = True
        print(event.key)
    elif event.key == pygame.K_DOWN:
        ship.moving_down = True
        print(event.key)
    elif event.key == pygame.K_SPACE:
        new_bullet = Bullet(ai_settings,screen,ship)
        bullets.add(new_bullet)

def check_keyup_events(event,ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False
    elif event.key == pygame.K_UP:
        ship.moving_up = False
    elif event.key == pygame.K_DOWN:
        ship.moving_down = False



def check_events(ship,ai_settings,screen,bullets):
    #控制飞船移动
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ship,ai_settings,screen,bullets)
            #Event就是for循环里的event
        elif event.type == pygame.KEYUP:
            check_keyup_events(event,ship)






def update_screen(ai_settings,screen,ship,bullets):
    # 更新屏幕,将颜色和飞船印上去,以及子弹
    screen.fill(ai_settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_buller()

    ship.blitme()
    ship.update()

    pygame.display.flip()

Guess you like

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