程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人?

开发过程中遇到Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人的问题如何解决?下面主要结合日常开发的经验,给出你关于Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人的解决方法建议,希望对你解决Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人有所启发或帮助;

我在第 12 章中完成了这个练习的第一部分,没有任何问题,但现在我试图将外星人添加到组合中,我遇到了麻烦。

我无法让它们在屏幕右上角绘制,它们一直从左上角开始;他们根本不动。

其他一切似乎都运行良好,包括船舶移动和子弹碰撞。

那么,我如何让外星人从右上角开始,在列中绘制,然后让它们上下移动?

sIDeways_shooter.py

import sys
from time import sleep

import pygame

from setTings import SetTings
from game_stats import GameStats
from ship import Ship
from bullet import Bullet
from alIEn import AlIEn

class SIDewaysShooter:
    """Overall class to manage game assets and behavior"""

    def __init__(self):
        """Initialize the game and create game resources."""
        pygame.init()
        self.setTings = SetTings()

        self.screen = pygame.display.set_mode(
            (self.setTings.screen_wIDth,self.setTings.screen_height))
        pygame.display.set_caption("SIDeways Shooter")

        self.stats = GameStats(self)

        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.alIEns = pygame.sprite.Group()

        self._create_fleet()

    def run_game(self):
        """Start the main loop for the game"""
        while True:
            self._check_events()

            if self.stats.game_active:
                self.ship.update()
                self._update_bullets()
                #self._update_alIEns()

            self._update_screen()

    def _check_events(self):
        #Respond to keypresses and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def _check_keydown_events(self,event):
        #Respond to keypresses
        if event.key == pygame.K_UP:
            self.ship.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()

    def _check_keyup_events(self,event):
        #Respond to key releases
        if event.key == pygame.K_UP:
            self.ship.moving_up = false
        elif event.key == pygame.K_DOWN:
            self.ship.moving_down = false

    def _update_alIEns(self):
        #check if the fleet is at an edge,update the position of all the alIEns
        self._check_fleet_edges()
        self.alIEns.update()

        #check for alIEn-ship collisions
        if pygame.sprite.spritecollIDeany(self.ship,self.alIEns):
            self._ship_hit()

        #Look for alIEns hitTing the bottom
        self._check_alIEns_bottom()

    def _fire_bullet(self):
        #Create a new bullet and add it to the bullets group
        if len(self.bullets) < self.setTings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _update_bullets(self):
        #update position of bullets and get rID of old bullets
        #update bullet positions
        self.bullets.update()

        #Get rID of bullets that have left the screen
        for bullet in self.bullets.copy():
            if bullet.rect.left >= self.setTings.screen_wIDth:
                self.bullets.remove(bullet)

        self._check_bullet_alIEn_collisions()

    def _check_bullet_alIEn_collisions(self):
        #Respond to alIEn bullet collsions
        #Remove any bullets and alIEns that have collIDed

        collisions = pygame.sprite.groupcollIDe(
            self.bullets,self.alIEns,True,TruE)

        if not self.alIEns:
            #Destroy exisTing bullets and create new fleet
            self.bullets.empty()
            self._create_fleet()


    def _update_screen(self):
        #update images on the screen,and flip to the new screen
        self.screen.fill(self.setTings.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.Sprites():
            bullet.draw_bullet()

        self.alIEns.draw(self.screen)

        pygame.display.flip()

    def _create_fleet(self):
        #Create the fleet of alIEns
        #Make an alIEn and find the number of alIEns in a column
        #Spacing between each alIEn should be equal to one alIEn height
        alIEn = AlIEn(self)
        alIEn_wIDth,alIEn_height = alIEn.rect.size
        available_space_y = self.setTings.screen_height - (2 * alIEn_height)
        number_alIEns_y = available_space_y // (2 * alIEn_height)

        #Determine the number of columns that can fit on the screen
        ship_wIDth = self.ship.rect.wIDth
        available_space_x = (self.setTings.screen_wIDth -
                                (4 * alIEn_wIDth) - ship_wIDth)
        number_rows = available_space_x // (2 * alIEn_wIDth)

        #Create the full fleet of alIEns
        for row_number in range(number_rows):
            for alIEn_number in range(number_alIEns_y):
                self._create_alIEn(alIEn_number,row_number)

    def _create_alIEn(self,alIEn_number,row_number):
        #Create an alIEn and place it in the column
        alIEn = AlIEn(self)
        alIEn_wIDth,alIEn_height = alIEn.rect.size
        alIEn.y = alIEn_height + 2 * alIEn_height * alIEn_number
        alIEn.rect.y = alIEn.y
        alIEn.rect.x = alIEn.rect.wIDth + 2 * alIEn.rect.wIDth * row_number
        self.alIEns.add(alIEn) 

    def _check_fleet_edges(self):
        #Respond appropriately if any alIEns have reached an edge
        for alIEn in self.alIEns.Sprites():
            if alIEn.check_edges():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        #Drop the entire fleet and change direction
        for alIEn in self.alIEns.Sprites():
            alIEn.rect.x += self.setTings.fleet_drop_speed
        self.setTings.fleet_direction *= -1

    def _check_alIEns_bottom(self):
        #check if any alIEns have reached the left of the screen
        screen_rect = self.screen.get_rect()
        for alIEn in self.alIEns.Sprites():
            if alIEn.rect.left >= screen_rect.left:
                #Treat this the same as if the ship was hit
                self._ship_hit()
                break

    def _ship_hit(self):
        #Respond to the ship being hit by an alIEn
        if self.stats.ships_left > 0:
            #Decrement ships_left
            self.stats.ships_left -= 1

            #Get rID of any remaining alIEns and bullets            
            self.alIEns.empty()
            self.bullets.empty()

            #Create a new fleet and center the ship
            self._create_fleet()
            self.ship.center_ship()

            #Pause
            sleep(0.5)

        else:
            self.stats.game_active = false 


if __name__ == '__main__':
    #Make a game instance,and run the game
    ai = SIDewaysShooter()
    ai.run_game()

alIEn.py

import pygame
from pygame.sprite import Sprite

class AlIEn(SpritE):
    #A class to represent an alIEn

    def __init__(self,ai_gamE):
        #Initialize the alIEn and set its starTing position
        super().__init__()
        self.screen = ai_game.screen
        self.setTings = ai_game.setTings

        #Load the alIEn image and set its rect attribute
        self.image = pygame.image.load('images/alIEn.bR_81_11845@p')
        self.rect = self.image.get_rect()

        #Start each new alIEn near the top right of the screen
        self.rect.x = self.setTings.screen_wIDth - self.rect.wIDth
        self.rect.y = self.rect.height

        #Store the alIEn's exact vertical position
        self.y = float(self.rect.y)

    def check_edges(self):
        #Return True if alIEn is at the edge of screen
        screen_rect = self.screen.get_rect()
        if self.rect.bottom >= screen_rect.bottom or self.rect.top <= 0:
            return True

    def update(self):
        #Move the alIEn up or down
        self.y += (self.setTings.alIEn_speed *
                    self.setTings.fleet_direction)
        self.rect.y = self.y

解决方法

我相信你的外星人因为这条线出现在屏幕的左侧:

alien.rect.x = alien.rect.width + 2 * alien.rect.width * row_number

当行号为0时,这里的第二项为0,外星人的x位置将等于其宽度。

如果你想让它们开始出现在屏幕的右侧,你可以让它的初始位置等于屏幕宽度减去一定数量,就像外星人宽度的倍数。

大佬总结

以上是大佬教程为你收集整理的Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人全部内容,希望文章能够帮你解决Python 速成课程 - 13-5 Sideways Shooter Pt 2,添加外星人所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。