After learning python The foundation is to write a plane battle training , This game has been written by many people , I wrote a comparison dish . Because it's practice , So many things that should be sealed are not sealed , No joy, no spray. , Blogging is about recording your progress , So as not to waste a summer vacation .( There is a perversion pattern in it , Those who are interested can play with it ). There is a resource link at the end of the article .
import pygame.sprite
import enemy
import myplane
import bullet
import supply
from random import *
from pygame.locals import *
from background import *
pygame.init()
bg_size = width, height = 480, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption(" Aircraft battle -----design by Wu Zechen ")
# Load sound
pygame.mixer.music.load("./sound/ Aircraft battle .mp3")
pygame.mixer.music.set_volume(0.6)
bullet_sound = pygame.mixer.Sound("./sound/bullet.wav")
bullet_sound.set_volume(0.1)
supply_sound = pygame.mixer.Sound("./sound/supply.wav")
supply_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("./sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("./sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("./sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("./sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("./sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("./sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("./sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("./sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("./sound/me_down.wav")
me_down_sound.set_volume(0.2)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Create a scrolling background image
bg1 = Background()
bg2 = Background(True)
bg_group = pygame.sprite.Group(bg1, bg2)
# Create an event
SUPPLY_TIME = pygame.USEREVENT
DOUBLE_BULLET_TIME = pygame.USEREVENT + 1
INVINCIBLE_TIME = pygame.USEREVENT + 2
# The number of bullets
BULLET1_NUM = 4
BULLET2_NUM = 12
def inc_speed(target, inc):
for e in target:
e.speed += inc
def main():
# Loop back the background music
pygame.mixer.music.play(-1)
# Set the clock to control the screen refresh rate
clock = pygame.time.Clock()
# Make me release the fighter
me = myplane.MyPlane(bg_size)
# Generate enemy spirit group , When detecting, you only need to connect the sprite group with me Compare
enemy_group = pygame.sprite.Group()
# Generate small enemy units
small_enemies = pygame.sprite.Group()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 50)
# Generate medium enemy units
mid_enemies = pygame.sprite.Group()
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 6)
# Generate large enemy units
big_enemies = pygame.sprite.Group()
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 4)
# Generate ordinary bullets
bullet1 = []
bullet1_index = 0
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1((me.rect.centerx - 20, me.rect.centery)))
# Generate super bullets
bullet2 = []
bullet2_index = 0
for i in range(BULLET2_NUM // 3):
bullet2.append(bullet.Bullet2((me.rect.centerx - 30, me.rect.centery)))
bullet2.append(bullet.Bullet2((me.rect.centerx, me.rect.centery)))
bullet2.append(bullet.Bullet2((me.rect.centerx + 30, me.rect.centery)))
# Whether to pause the game
paused = False
paused_nor_image = pygame.image.load("./images/pause_nor.png")
paused_pressed_image = pygame.image.load("./images/pause_pressed.png")
resume_nor_image = pygame.image.load("./images/resume_nor.png")
resume_pressed_image = pygame.image.load("./images/resume_pressed.png")
paused_image = paused_nor_image
paused_rect = paused_nor_image.get_rect()
paused_rect.left = width - paused_rect.width - 10
paused_rect.top = 10
# Statistics score
score = 0
score_font = pygame.font.Font('./font/rough.ttf', 36)
# Set the number of lives
life_image = pygame.image.load("./images/life.png")
life_rect = life_image.get_rect()
life_num = 3
# Set game level
level = 1
# Set the game running variables
running = True
# Set image conversion variables
switch_image = False
# Set the delay variable
delay = 100
# Set the full screen bomb
bomb_image = pygame.image.load("./images/bomb.png")
bomb_image_rect = bomb_image.get_rect()
bomb_font = pygame.font.Font("./font/font.ttf", 48)
bomb_num = 3
# 30 Seconds to generate a supply package
bullet_supply = supply.BulletSupply(bg_size)
bomb_supply = supply.BombSupply(bg_size)
pygame.time.set_timer(SUPPLY_TIME, 30 * 1000) # 30 * 1000 Millisecond is equal to 30 second
# Whether to use super bullets
is_double_bullet = False
# Index of shot pictures
enemy1_destroy_index = 0
enemy2_destroy_index = 0
enemy3_destroy_index = 0
me_destroy_index = 0
# Used to prevent duplicate opening of log files
recorded = False
# Game start screen
game_begin_font = pygame.font.Font("./font/font.ttf", 48)
begin_image = pygame.image.load("./images/begin.png").convert_alpha()
invincible_image = pygame.image.load("./images/invincible.png").convert_alpha()
begin_image_rect = begin_image.get_rect()
invincible_image_rect = invincible_image.get_rect()
# Game ending screen
game_over_font = pygame.font.Font("./font/font.ttf", 48)
over_image = pygame.image.load("./images/game_over.png")
again_image = pygame.image.load("./images/again.png")
again_image_rect = again_image.get_rect()
over_image_rect = over_image.get_rect()
# Draw the background image
bg_group.update()
bg_group.draw(screen)
# Whether to enter invincible mode
invincible = False
# Whether to enter Super Fighter mode
super_me = True
super_bullet = False
# Whether to exit the selection page
out = False
# Choice mode
while True:
bg_group.update()
bg_group.draw(screen)
# Draw mode button
begin_image_rect.left = (width - begin_image_rect.width) // 2
begin_image_rect.top = (height - begin_image_rect.height) // 2
invincible_image_rect.left = (width - invincible_image_rect.width) // 2
invincible_image_rect.top = (height - invincible_image_rect.height) // 2 + 50
screen.blit(begin_image, begin_image_rect)
screen.blit(invincible_image, invincible_image_rect)
clock.tick(60)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
# Normal mode
if begin_image_rect.left < event.pos[0] < begin_image_rect.right and \
begin_image_rect.top < event.pos[1] < begin_image_rect.bottom:
invincible = False
out = True
pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
break
# Invincible mode , In this mode, the fighter is invincible , supply 10 One second , Infinite explosive bag
if invincible_image_rect.left < event.pos[0] < invincible_image_rect.right and \
invincible_image_rect.top < event.pos[1] < invincible_image_rect.bottom:
invincible = True
out = True
pygame.time.set_timer(SUPPLY_TIME, 10 * 1000)
life_num = 1
break
if out:
break
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1 and paused_rect.collidepoint(event.pos):
paused = not paused
if paused:
pygame.time.set_timer(SUPPLY_TIME, 0)
pygame.mixer.music.pause()
pygame.mixer.pause()
else:
pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
pygame.mixer.music.unpause()
pygame.mixer.unpause()
elif event.type == MOUSEMOTION:
if paused_rect.collidepoint(event.pos):
if paused:
paused_image = resume_pressed_image
else:
paused_image = paused_pressed_image
else:
if paused:
paused_image = resume_nor_image
else:
paused_image = paused_nor_image
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if bomb_num > 0:
bomb_sound.play()
if not invincible:
bomb_num -= 1
for e in enemy_group:
if e.rect.bottom > 0:
e.active = False
elif event.type == SUPPLY_TIME:
supply_sound.play()
if choice([True, False]):
bomb_supply.reset()
else:
bullet_supply.reset()
elif event.type == DOUBLE_BULLET_TIME:
is_double_bullet = False
pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)
elif event.type == INVINCIBLE_TIME:
me.invincible = False
pygame.time.set_timer(INVINCIBLE_TIME, 0)
if level == 1 and score > 180000:
level = 2
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 2)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 1)
if level == 2 and score > 360000:
level = 3
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.5)
inc_speed(mid_enemies, 0.5)
# Fighter upgrade , Four times the damage of bullets , Increase movement speed by twice
if level == 3 and score > 800000 and super_me:
level = 4
upgrade_sound.play()
super_me = False
super_bullet = True
upgrade_sound.play()
me.change_face(bg_size)
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
inc_speed(small_enemies, 0.5)
inc_speed(mid_enemies, 0.5)
if level == 4 and score > 1200000:
level = 5
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 2)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
# Increase the speed of small enemy aircraft
inc_speed(small_enemies, 0.1)
# Increase the speed of medium enemy aircraft
inc_speed(mid_enemies, 0.1)
# Increase the speed of large enemy aircraft
inc_speed(big_enemies, 0.5)
if level == 5 and score > 2000000:
level = 6
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
# Increase the speed of small enemy aircraft
inc_speed(small_enemies, 0.1)
# Increase the speed of medium enemy aircraft
inc_speed(mid_enemies, 0.1)
# Increase the speed of large enemy aircraft
inc_speed(big_enemies, 0.1)
if level == 6 and score > 3000000:
level = 7
upgrade_sound.play()
enemy.SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
enemy.MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
enemy.BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
# Increase the speed of small enemy aircraft
inc_speed(small_enemies, 0.1)
# Increase the speed of medium enemy aircraft
inc_speed(mid_enemies, 0.1)
# Increase the speed of large enemy aircraft
inc_speed(big_enemies, 0.1)
# Draw the background image
bg_group.update()
bg_group.draw(screen)
# There is no pause and the fighter's life is greater than 0
if life_num and not paused:
# Check whether the user's keyboard is pressed
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
me.move_up()
if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
me.move_down()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
me.move_left()
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
me.move_right()
# Draw a full screen bomb
if bomb_supply.active:
bomb_supply.move()
screen.blit(bomb_supply.image, bomb_supply.rect)
if pygame.sprite.collide_mask(bomb_supply, me):
get_bomb_sound.play()
if bomb_num < 3:
bomb_num + 1
bomb_supply.active = False
# Draw super bullets
if bullet_supply.active:
bullet_supply.move()
screen.blit(bullet_supply.image, bullet_supply.rect)
# Check whether the fighter gets super bullets
if pygame.sprite.collide_mask(bullet_supply, me):
get_bullet_sound.play()
is_double_bullet = True
pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
bullet_supply.active = False
# bullets
if not (delay % 10):
bullet_sound.play()
if is_double_bullet:
bullets = bullet2
bullets[bullet2_index].reset((me.rect.centerx - 20, me.rect.centery))
bullets[bullet2_index + 1].reset((me.rect.centerx - 60, me.rect.centery))
bullets[bullet2_index + 2].reset((me.rect.centerx + 20, me.rect.centery))
bullet2_index = (bullet2_index + 3) % BULLET2_NUM
else:
bullets = bullet1
bullets[bullet1_index].reset((me.rect.centerx - 17.5, me.rect.centery))
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
# Check whether the bullet collided with the enemy plane
for b in bullets:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemies_hit = pygame.sprite.spritecollide(b, enemy_group, False, pygame.sprite.collide_mask)
if enemies_hit:
b.active = False
for e in enemies_hit:
if e in mid_enemies or e in big_enemies:
if super_bullet:
e.energy -= 4
else:
e.energy -= 1
e.hit = True
if e.energy == 0:
e.active = False
else:
e.active = False
# Draw a small enemy plane
for each in small_enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
if not (delay % 3):
if enemy1_destroy_index == 0:
enemy1_down_sound.play()
screen.blit(each.destroy_images[enemy1_destroy_index], each.rect)
enemy1_destroy_index = (enemy1_destroy_index + 1) % 4
if enemy1_destroy_index == 0:
score += 1000
each.reset()
# Draw medium enemy aircraft
for each in mid_enemies:
if each.active:
each.move()
if each.hit: # Trigger the strike effect when being hit
screen.blit(each.hit_image, each.rect)
each.hit = False
else:
screen.blit(each.image, each.rect)
# Drawing blood tanks
pygame.draw.line(screen,
BLACK,
(each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5),
2)
energy_remain = each.energy / enemy.MidEnemy.energy
if energy_remain > 0.3:
line_color = GREEN
else:
line_color = RED
pygame.draw.line(screen,
line_color,
(each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
2)
else:
if not (delay % 3):
if enemy2_destroy_index == 0:
enemy2_down_sound.play()
screen.blit(each.destroy_images[enemy2_destroy_index], each.rect)
enemy2_destroy_index = (enemy2_destroy_index + 1) % 4
if enemy2_destroy_index == 0:
score += 4000
each.reset()
# Draw a large enemy plane
for each in big_enemies:
if each.active:
each.move()
if each.hit: # Trigger the strike effect when being hit
screen.blit(each.hit_image, each.rect)
each.hit = False
else:
if switch_image:
screen.blit(each.image1, each.rect)
else:
screen.blit(each.image2, each.rect)
# Drawing blood tanks
pygame.draw.line(screen,
BLACK,
(each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5),
2)
energy_remain = each.energy / enemy.BigEnemy.energy
if energy_remain > 0.4:
line_color = GREEN
else:
line_color = RED
pygame.draw.line(screen,
line_color,
(each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
2)
if each.rect.bottom - 50 > 0:
enemy3_fly_sound.play()
else: # Ruin
if not (delay % 3):
if enemy3_destroy_index == 0:
enemy3_down_sound.play()
screen.blit(each.destroy_images[enemy3_destroy_index], each.rect)
enemy3_destroy_index = (enemy3_destroy_index + 1) % 6
if enemy3_destroy_index == 0:
enemy3_fly_sound.stop()
score += 10000
each.reset()
# Check whether my fighter plane collides with the enemy fighter plane , Return to the list in case of collision
enemies_down = pygame.sprite.spritecollide(me, enemy_group, False, pygame.sprite.collide_mask)
if enemies_down and not me.invincible:
if not invincible:
me.active = False
for e in enemies_down:
e.active = False
# Draw dynamic fighter
if me.active:
if switch_image:
screen.blit(me.image1, me.rect)
else:
screen.blit(me.image2, me.rect)
else:
if not (delay % 3):
if me_destroy_index == 0:
me_down_sound.play()
screen.blit(me.destroy_images[me_destroy_index], me.rect)
me_destroy_index = (me_destroy_index + 1) % 4
if me_destroy_index == 0:
life_num -= 1
me.reset()
pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)
# Draw the number of fighter life
if life_num:
for life in range(life_num):
screen.blit(life_image, (width - 10 - (life + 1) * life_rect.width, height - 10 - life_rect.height))
# Draw a full screen bomb
bomb_text = bomb_font.render("x %d" % bomb_num, True, WHITE)
text_rect = bomb_text.get_rect()
screen.blit(bomb_image, (10, height - 10 - bomb_image_rect.height))
screen.blit(bomb_text, (20 + bomb_image_rect.width, height - 10 - bomb_image_rect.height))
# Draw fractions
score_text = score_font.render("Score : %s" % str(score), True, WHITE)
screen.blit(score_text, (10, 5))
# Game ending screen
elif life_num == 0:
# End the background music
pygame.mixer.music.stop()
# Stop all sound effects
pygame.mixer.stop()
# Stop supply distribution
pygame.time.set_timer(SUPPLY_TIME, 0)
if not recorded:
recorded = True
# Read the highest score in history
with open('record.txt', 'r') as f:
record_score = int(f.read())
# If the player's score is higher than the highest score, it will be archived
if score > record_score:
with open('record.txt', 'w') as f:
f.write(str(score))
# Draw the highest score in history
record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
screen.blit(record_score_text, (50, 50))
# Draw the score of this game
game_over_text1 = game_over_font.render("Your Score", True, (255, 255, 255))
game_over_text1_rect = game_over_text1.get_rect()
game_over_text1_rect.left = (width - game_over_text1_rect.width) // 2
game_over_text1_rect.top = height // 3
screen.blit(game_over_text1, game_over_text1_rect)
game_over_text2 = game_over_font.render(str(score), True, (255, 255, 255))
game_over_text2_rect = game_over_text2.get_rect()
game_over_text2_rect.left = (width - game_over_text2_rect.width) // 2
game_over_text2_rect.top = game_over_text1_rect.top + 70
screen.blit(game_over_text2, game_over_text2_rect)
# Drawing starts all over again
again_image_rect.left = (width - again_image_rect.width) // 2
again_image_rect.top = game_over_text2_rect.bottom + 50
screen.blit(again_image, again_image_rect)
# Draw the end of the game
over_image_rect.left = (width - over_image_rect.width) // 2
over_image_rect.top = again_image_rect.bottom + 50
screen.blit(over_image, over_image_rect)
# Check whether the left mouse button is pressed
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if again_image_rect.left < pos[0] < again_image_rect.right and \
again_image_rect.top < pos[1] < again_image_rect.bottom:
# Restart the game
main()
if over_image_rect.left < pos[0] < over_image_rect.right and \
over_image_rect.top < pos[1] < over_image_rect.bottom:
# Quit the game
pygame.quit()
# pygame.quit()
# Draw a pause image
screen.blit(paused_image, paused_rect)
# Limit drawing time , Optimize the effect of bumps
if not (delay % 3):
switch_image = not switch_image
if delay:
delay -= 1
else:
delay = 100
# Refresh image
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
# try:
# main()
# except SystemExit: # If the exception is system exit, skip
# pass
# except:
# traceback.print_exc()
main()
pygame.quit()
input() # You can only go down after receiving user input here
import pygame
from random import *
class BulletSupply(pygame.sprite.Sprite):
def __init__(self, bg_size):
super().__init__()
self.image = pygame.image.load("./images/bullet_supply.png")
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.speed = 5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.y < self.height:
self.rect.y += self.speed
else:
self.active = False
def reset(self):
self.rect.x = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.active = True
class BombSupply(pygame.sprite.Sprite):
def __init__(self, bg_size):
super().__init__()
self.image = pygame.image.load("./images/bomb_supply.png")
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.speed = 4
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.y < self.height:
self.rect.y += self.speed
else:
self.active = False
def reset(self):
self.rect.x = randint(0, self.width - self.rect.width)
self.rect.top = -100
self.active = True
import pygame
from random import *
class SmallEnemy(pygame.sprite.Sprite):
def __init__(self, bg_size):
super(SmallEnemy, self).__init__()
self.image = pygame.image.load("./images/enemy1.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/enemy1_down1.png"),
pygame.image.load("./images/enemy1_down2.png"),
pygame.image.load("./images/enemy1_down3.png"),
pygame.image.load("./images/enemy1_down4.png")])
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-5 * self.height, 0)
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-5 * self.height, 0)
@staticmethod
def add_small_enemies(group1, group2, bg_size, num):
for i in range(num):
e1 = SmallEnemy(bg_size)
group1.add(e1)
group2.add(e1)
class MidEnemy(pygame.sprite.Sprite):
energy = 8
def __init__(self, bg_size):
super().__init__()
self.image = pygame.image.load("./images/enemy2.png")
self.hit_image = pygame.image.load("./images/enemy2_hit.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/enemy2_down1.png"),
pygame.image.load("./images/enemy2_down2.png"),
pygame.image.load("./images/enemy2_down3.png"),
pygame.image.load("./images/enemy2_down4.png")])
self.rect = self.image.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-10 * self.height, -self.height)
self.active = True
self.hit = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = MidEnemy.energy
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-10 * self.height, -self.height)
@staticmethod
def add_mid_enemies(group1, group2, bg_size, num):
for i in range(num):
e2 = MidEnemy(bg_size)
group1.add(e2)
group2.add(e2)
class BigEnemy(pygame.sprite.Sprite):
energy = 20
def __init__(self, bg_size):
super().__init__()
self.image1 = pygame.image.load("./images/enemy3_n1.png")
self.image2 = pygame.image.load("./images/enemy3_n2.png")
self.hit_image = pygame.image.load("./images/enemy3_hit.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/enemy3_down1.png"),
pygame.image.load("./images/enemy3_down2.png"),
pygame.image.load("./images/enemy3_down3.png"),
pygame.image.load("./images/enemy3_down4.png"),
pygame.image.load("./images/enemy3_down5.png"),
pygame.image.load("./images/enemy3_down6.png")])
self.rect = self.image1.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 2
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-15 * self.height, -5 * self.height)
self.active = True
self.hit = False
self.mask = pygame.mask.from_surface(self.image1)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = BigEnemy.energy
self.rect.left = randint(0, self.width - self.rect.width)
self.rect.top = randint(-15 * self.height, -5 * self.height)
@staticmethod
def add_big_enemies(group1, group2, bg_size, num):
for i in range(num):
e3 = BigEnemy(bg_size)
group1.add(e3)
group2.add(e3)
import webbrowser
import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self, bg_size):
self.image1 = pygame.image.load("./images/me1.png")
self.image2 = pygame.image.load("./images/me2.png")
self.destroy_images = []
self.destroy_images.extend([pygame.image.load("./images/me_destroy_1.png"),
pygame.image.load("./images/me_destroy_2.png"),
pygame.image.load("./images/me_destroy_3.png"),
pygame.image.load("./images/me_destroy_4.png")])
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)
self.width = bg_size[0]
self.height = bg_size[1]
self.rect = self.image1.get_rect()
self.rect.bottom = bg_size[1] - 60
self.rect.left = (bg_size[0] - self.rect.width) // 2
self.speed = 8
def move_up(self):
if self.rect.top > 0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def move_down(self):
if self.rect.bottom < self.height - 60:
self.rect.bottom += self.speed
else:
self.rect.bottom = self.height - 60
def move_left(self):
if self.rect.left > 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def move_right(self):
if self.rect.right < self.width:
self.rect.right += self.speed
else:
self.rect.right = self.width
def reset(self):
self.rect.left = (self.width - self.rect.width) // 2
self.rect.bottom = self.height - 60
self.active = True
self.invincible = True
def change_face(self, bg_size):
self.image1 = pygame.image.load("./images/me5.png")
self.image2 = pygame.image.load("./images/me6.png")
self.mask = pygame.mask.from_surface(self.image1)
self.rect = self.image1.get_rect()
self.rect.bottom = bg_size[1] - 60
self.rect.left = (bg_size[0] - self.rect.width) // 2
self.speed = 10
import pygame
class Background(pygame.sprite.Sprite):
def __init__(self, is_other=False): # The default is the first image , if True Is the second chapter of the image
super().__init__()
self.image = pygame.image.load("./images/background.png") # Directly pass in the image location in the initialization function
self.rect = self.image.get_rect()
print(self.rect.size)
if is_other:
self.rect.y = -self.rect.height
def update(self):
self.rect.y += 1
if self.rect.y >= 700:
self.rect.y = -self.rect.height
from pygame import image, sprite, mask
class Bullet1(sprite.Sprite):
def __init__(self, position):
super().__init__()
self.image = image.load("./images/bullet3.png")
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 15
self.active = False
self.mask = mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
class Bullet2(sprite.Sprite):
def __init__(self, position):
super().__init__()
self.image = image.load("./images/bullet4.png")
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 15
self.active = False
self.mask = mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
link :https://pan.baidu.com/s/1ujLif8qLgznucOMDNb3pMQ
Extraction code :wzc6
I use it when coding import py
save pandas plan (20)—— Count