当前位置:网站首页>[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.7 【Pygame Little games 】 Divine reduction 【 Happy double tank battle 】 Small program game , Start playing ~
A summary of the article ——
( There are more cases waiting for you to learn !)

版权声明
本文为[Hi! Chestnut classmate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220023347858.html
边栏推荐
- Redis (II): advanced redis - redis basic operation, persistence, transaction, deletion strategy and server basic configuration under Linux Conf, advanced data type
- 如何测试 Flutter 应用? ー 单元测试
- [Niuke] you must brush the top101-01 linked list for the interview
- 刷题整理(一)
- How to introduce Fuyue festival in any web page to destroy small aircraft?
- Busybox overview
- Main parameters and structure of LED
- 浅谈摸鱼的快乐 —— 4月20日
- Redis(二):redis高级——linux下redis基本操作、持久化、事务、删除策略、服务器基础配置redis.conf、高级数据类型
- What is the process of complete data analysis
猜你喜欢

响应结果定义具体错误码

物联网专业未来没有前途了吗?

Thousands of people have collected and "finished" this 300 page 1000 interview questions together

Redis(三):redis集群——主从复制、哨兵、集群

Distributed transactions and Seata

Detailed explanation of TCP's three handshakes

08. Install MySQL for raspberry pie

Blender MMD export FBX models and bake animations

ES6 new syntax parsing

Redis (I): basics of redis - Introduction to redis, basic data types, general instructions, jedis
随机推荐
完整的数据分析是怎样的流程
L1-020 帅到没朋友
简单了解Oracle+JPA+Hibernate
[sctf2019] flag shop Erb template injection
深度解析ERP和MES的六大不同之处,务必耐心看完
What has changed since Huawei Routing & Switching switched to datacom
非常强大的时间日期插件 --- JeDate.js
Detailed explanation of thread pool
等待wait(),wait(long),wait(long,int)/通知机制notify(),notifyAll()
dp 优化
kubernetes集群之Pod说能不能让我体面的消亡呀?
SEO keyword expansion - Automatic Keyword expansion software free download
MDC 管理多线程日志
物联网专业未来没有前途了吗?
Detailed explanation of TCP's three handshakes
Redis(一):redis基础——redis入门、基础数据类型、通用指令、jedis
Unity determines whether the file (under the local absolute directory) exists
C语言 结构体
面试题8——捕获的雨水
微服务简介,Euraka,Ribbon,openFeign