当前位置:网站首页>[pyGame game] the most classic alien game in history, which fully guarantees the courage to break through (Unsolved Mystery)

[pyGame game] the most classic alien game in history, which fully guarantees the courage to break through (Unsolved Mystery)

2022-04-22 00:33:00 Hi! Chestnut classmate

   Preface

Talk about aliens ....

You think aliens ...... They all take UFO From Earth , The long hum may be more distinctive . Maybe it looks like this

  harm ! In fact, it may or may not last long .

The picture is based on the one recognized by the public . Of course, you can do it yourself ! Now let's make a simple 《 Aliens enter

Invade 》 Little game !

Complete material for all articles + The source code is in

Fans white whoring source code welfare , Please move to CSDN Community

Text

Installation environment :

download python3, Or as Anaconda3(64 bit), Import pygame Game Pack .

​​

Code implementation process :

1. Alien settings ,alien.py, Code :

import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
 """ Class representing a single alien """
 
 def __init__(self,ai_settings,screen):
  """ Initialize aliens and set other locations """
  super(Alien,self).__init__()
  self.screen = screen
  self.ai_settings = ai_settings
  
  # Loading alien images , And set its rect attribute 
  self.image = pygame.image.load('images/alien.bmp')
  self.rect = self.image.get_rect()
  
  # Each alien was initially near the top left corner of the screen 
  self.rect.x = self.rect.width
  self.rect.y = self.rect.height
  
  # Store the exact location of Aliens 
  self.x = float(self.rect.x)
  
  
 def blitme(self):
  """ Draw aliens in designated locations """
  self.screen.blit(self.image,self.rect)
    
 def check_edges(self):
  """ If the alien is on the edge of the screen , Just go back to True"""
  screen_rect = self.screen.get_rect()
  if self.rect.right >= screen_rect.right:
   return True
  elif self.rect.left <= 0:
   return True
 
 def update(self):
  """ Moving aliens to the right """
  self.x += (self.ai_settings.alien_speed_factor * self.ai_settings.fleet_direction)
  self.rect.x = self.x

2. The main program of the game ,alien_invasion.py, Code :

import pygame

from settings import Settings
from game_stats import GameStats
from button import Button
from ship import Ship
from pygame.sprite import Group
import game_functions as gf
from scoreboard import Scoreboard

def run_game():
  pygame.init()    #  Initialize background settings 
  ai_settings = Settings()    #  Global settings 

  screen = pygame.display.set_mode(      #  establish screen Display window 
    (ai_settings.screen_width,ai_settings.screen_height)
  )
  pygame.display.set_caption('Alien Invasion')  #  title 
  # newly build Play Button 
  play_button = Button(ai_settings,screen,"Play")
  # Create an instance to store game statistics , And create a scoreboard 
  stats = GameStats(ai_settings)
  sb = Scoreboard(ai_settings, screen, stats)
  #  Create a spaceship 
  ship = Ship(ai_settings,screen)
  #  Create bullet group 
  bullets = Group()
  
  # Create an alien 
  aliens = Group()
  # Creating alien groups 
  gf.create_fleet(ai_settings,screen,ship,aliens)
  
  #  Start the main cycle of the game 
  while True:
    #  Monitor keyboard and mouse events 
    gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens,bullets)
    
    if stats.game_active:
      #  Moving ships 
      gf.update_ship(ship)
      #  Update bullet position 
      gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
      # Update aliens 
      gf.update_aliens(ai_settings,stats,screen,sb,ship,aliens,bullets)
    #  Update screen 
    gf.update_screen(ai_settings,screen,stats,sb,ship,aliens,bullets,play_button)

run_game()

3. Set bullet ,bullet.py, Code :

import pygame
from pygame.sprite import Sprite
import time

class Bullet(Sprite):
  ''' The ship manages bullets '''

  def __init__(self,ai_settings,screen,ship):
    super(Bullet,self).__init__()
    self.screen = screen

    #  Create the initial position of the bullet rectangle (0,0,3,15) They correspond to each other lef,top, wide , high 
    self.rect = pygame.Rect(0,0,
    ai_settings.bullet_width, ai_settings.bullet_height)

    self.rect.centerx = ship.rect.centerx #  Set the center point x The axis coordinates are consistent with the spacecraft 
    self.rect.top = ship.rect.top     #  Set up y The top of the axis coordinates is consistent with the spacecraft 

    #  Set to decimal for calculation 
    self.top = float(self.rect.top)

    self.color = ai_settings.bullet_color
    self.speed_factor = ai_settings.bullet_speed_factor

  def update(self):
    self.top -=self.speed_factor
    self.rect.top = self.top
    print(self.rect.top)

  def draw_bullet(self):
    pygame.draw.rect(self.screen,self.color,self.rect)

# A little .....................................

In fact, there are a few pages of source code , Lots of code , If you need it, it would be better to call me directly ~

Effect display :

Okay ! The code is all on it . Need complete teaching picture materials 、 The source code has been packaged in the group .

Don't say that the code reports an error. I don't know where to modify it ! Hey , Groups can answer each other's questions !!!

Summary

Hee hee , The article is finished ~ This simple aircraft war with aliens is over here ~ The unsolved mystery is left to scientists to decipher

beep !

Complete material, etc : You can also drop me ! Or click the end of the text to get the free official account. ~

Recommended reading in the past ——

project 1.2   Pygame Little games : Playing minesweeping is blind , You're not alone .

project 1.3  Pygame Little games : screwing 《 Ball version — Greedy snake 》, You got caught ?

project 1.6 【Pygame Little games 】 I've seen a lot of fighting landlords ,BUT This open source happy landlords , Most convincing ~

project 1.7 【Pygame Little games 】 Divine reduction 【 Happy double tank battle 】 Small program game , Start playing ~

A summary of the article ——

Summary : Python Collection of articles | ( Introduction to actual combat 、 game 、Turtle、 Case etc. )

( There are more cases waiting for you to learn !)

版权声明
本文为[Hi! Chestnut classmate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220023347858.html