正在做的飞机大战(没有音效)
AirplaneGame.py
import pygame
import random
import time
import find
import sys
import img_size
import fire
system = find.File()
data = system.read()
bullet_py = fire.Fire()
pygame.init()
screen = pygame.display.set_mode((400, 650))
pygame.display.set_caption(' Airplane Game')
icon = pygame.image.load('something/icon.ico')
bg = pygame.image.load('bg/bg.png')
gems = pygame.image.load('something/gems.png')
SomeGems = pygame.image.load('something/money.png')
start_button = pygame.Rect((125, 200), (150, 80))
start_button2 = pygame.Rect((130, 205), (140, 70))
strengthen_button = pygame.Rect((125, 295), (150, 80))
strengthen_button2 = pygame.Rect((130, 300), (140, 70))
exit_button = pygame.Rect((125, 390), (150, 80))
exit_button2 = pygame.Rect((130, 395), (140, 70))
over_return = pygame.Rect((125, 490), (150, 80))
over_return2 = pygame.Rect((130, 495), (140, 70))
continue_Button = pygame.Rect((125, 390), (150, 80))
continue_Button2 = pygame.Rect((130, 395), (140, 70))
win_return = pygame.Rect((125, 490), (150, 80))
win_return2 = pygame.Rect((130, 495), (140, 70))
shop_return = pygame.Rect((125, 490), (150, 80))
shop_return2 = pygame.Rect((130, 495), (140, 70))
buy_sr = pygame.Rect((270, 215.5), (120, 64))
buy_sr2 = pygame.Rect((275, 220.5), (110, 54))
buy_ssr = pygame.Rect((270, 335), (120, 64))
buy_ssr2 = pygame.Rect((275, 340), (110, 54))
Enter1 = pygame.Rect((25, 200), (350, 80))
Enter2 = pygame.Rect((25, 325), (350, 80))
OK = pygame.Rect((125, 490), (150, 80))
OK2 = pygame.Rect((130, 495), (140, 70))
bullets = []
enemies = []
pygame.display.set_icon(icon)
player_x = 160
player_y = 470
Clock = pygame.time.Clock()
fps = 60
score = 0
level = data['level']
bg_number = 1
if len(str(level)) == 1:
pass
else:
bg_number = int(str(level)[0]) + 1
bg_Img = 'bg' + str(bg_number) + '.png'
bg_y1 = 0
bg_y2 = -650
wars_bg1 = pygame.image.load('bg/' + bg_Img)
if bg_number > 5:
wars_bg2 = wars_bg1.copy()
elif bg_number == 5:
bg_y1 = -550
dead = 'Game Over!'
task = data['task']
km = 0
coin = data['coin']
game_over = False
start = False
win = False
shop = False
none = False
shop_thing = ['yellow_airplane_r.png', 'blue_airplane_r.png', 'pink_airplane_r.png', 'pixel_airplane_r.png']
if len(data['airport']) == 4:
none = True
else:
for data_images in data['airport']:
for thing in shop_thing:
if data_images == thing:
shop_thing.remove(thing)
shop_count = len(shop_thing)
sr_Img = shop_thing[random.randint(0, shop_count - 1)]
shop_sr_plane = pygame.image.load('player/' + sr_Img)
ssr_img = shop_thing[random.randint(0, shop_count - 1)]
while ssr_img == sr_Img:
ssr_img = shop_thing[random.randint(0, shop_count - 1)]
shop_ssr_plane = pygame.image.load('player/' + ssr_img)
font_name1 = pygame.font.match_font('comicsansms')
font_name2 = pygame.font.match_font('segoeprint')
font1 = pygame.font.Font(font_name1, 36)
font2 = pygame.font.Font(font_name2, 60)
font3 = pygame.font.Font(font_name2, 40)
class Actor:
x = 0
y = 0
image = None
def __init__(self, image):
self.image = pygame.image.load(image)
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Password:
def write(self, name, password):
data['name'] = name
data['password'] = password
def into(self, name, password):
if name == data['name'] and data['password'] == password:
return True
else:
return False
class Gems_Get:
x = 185.5
y = 309.5
speed_x = 1
speed_y = -2
wait_ago = time.time()
def init(self):
screen.blit(SomeGems, (self.x, self.y))
def __move__(self):
self.x += self.speed_x
self.y += self.speed_y
def move(self):
wait_now = time.time()
if (wait_now - self.wait_ago) >= 1 and self.speed_y != 2:
self.speed_y += 0.1
if self.speed_y == 2:
self.wait_ago = time.time()
if (wait_now - self.wait_ago) <= 3 and self.speed_y != 2:
self.__move__()
password_use = Password()
color1 = (0, 0, 0)
color2 = (0, 0, 0)
OK_IF = False
name = ''
password = ''
first = False
def Init():
global color1, color2, OK_IF, name, password, first
while True:
screen.blit(bg, (0, 0))
word = 'Enter your ID'
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if color1 == (100, 100, 100) and len(name) < 18:
if event.key == pygame.K_a:
name += 'a'
elif event.key == pygame.K_b:
name += 'b'
elif event.key == pygame.K_c:
name += 'c'
elif event.key == pygame.K_d:
name += 'd'
elif event.key == pygame.K_e:
name += 'e'
elif event.key == pygame.K_f:
name += 'f'
elif event.key == pygame.K_g:
name += 'g'
elif event.key == pygame.K_h:
name += 'h'
elif event.key == pygame.K_i:
name += 'i'
elif event.key == pygame.K_j:
name += 'j'
elif event.key == pygame.K_k:
name += 'k'
elif event.key == pygame.K_l:
name += 'l'
elif event.key == pygame.K_m:
name += 'm'
elif event.key == pygame.K_n:
name += 'n'
elif event.key == pygame.K_o:
name += 'o'
elif event.key == pygame.K_p:
name += 'p'
elif event.key == pygame.K_q:
name += 'q'
elif event.key == pygame.K_r:
name += 'r'
elif event.key == pygame.K_s:
name += 's'
elif event.key == pygame.K_t:
name += 't'
elif event.key == pygame.K_u:
name += 'u'
elif event.key == pygame.K_v:
name += 'v'
elif event.key == pygame.K_w:
name += 'w'
elif event.key == pygame.K_x:
name += 'x'
elif event.key == pygame.K_y:
name += 'y'
elif event.key == pygame.K_z:
name += 'z'
elif event.key == pygame.K_SPACE:
name += ' '
elif event.key == pygame.K_1:
name += '1'
elif event.key == pygame.K_2:
name += '2'
elif event.key == pygame.K_3:
name += '3'
elif event.key == pygame.K_4:
name += '4'
elif event.key == pygame.K_5:
name += '5'
elif event.key == pygame.K_6:
name += '6'
elif event.key == pygame.K_7:
name += '7'
elif event.key == pygame.K_8:
name += '8'
elif event.key == pygame.K_9:
name += '9'
elif event.key == pygame.K_0:
name += '0'
elif color2 == (100, 100, 100) and len(password) < 18:
if event.key == pygame.K_a:
password += 'a'
elif event.key == pygame.K_b:
password += 'b'
elif event.key == pygame.K_c:
password += 'c'
elif event.key == pygame.K_d:
password += 'd'
elif event.key == pygame.K_e:
password += 'e'
elif event.key == pygame.K_f:
password += 'f'
elif event.key == pygame.K_g:
password += 'g'
elif event.key == pygame.K_h:
password += 'h'
elif event.key == pygame.K_i:
password += 'i'
elif event.key == pygame.K_j:
password += 'j'
elif event.key == pygame.K_k:
password += 'k'
elif event.key == pygame.K_l:
password += 'l'
elif event.key == pygame.K_m:
password += 'm'
elif event.key == pygame.K_n:
password += 'n'
elif event.key == pygame.K_o:
password += 'o'
elif event.key == pygame.K_p:
password += 'p'
elif event.key == pygame.K_q:
password += 'q'
elif event.key == pygame.K_r:
password += 'r'
elif event.key == pygame.K_s:
password += 's'
elif event.key == pygame.K_t:
password += 't'
elif event.key == pygame.K_u:
password += 'u'
elif event.key == pygame.K_v:
password += 'v'
elif event.key == pygame.K_w:
password += 'w'
elif event.key == pygame.K_x:
password += 'x'
elif event.key == pygame.K_y:
password += 'y'
elif event.key == pygame.K_z:
password += 'z'
elif event.key == pygame.K_SPACE:
password += ' '
elif event.key == pygame.K_1:
password += '1'
elif event.key == pygame.K_2:
password += '2'
elif event.key == pygame.K_3:
password += '3'
elif event.key == pygame.K_4:
password += '4'
elif event.key == pygame.K_5:
password += '5'
elif event.key == pygame.K_6:
password += '6'
elif event.key == pygame.K_7:
password += '7'
elif event.key == pygame.K_8:
password += '8'
elif event.key == pygame.K_9:
password += '9'
elif event.key == pygame.K_0:
password += '0'
if color1 == (100, 100, 100):
if event.key == pygame.K_BACKSPACE:
name = name[:-1]
elif color2 == (100, 100, 100):
if event.key == pygame.K_BACKSPACE:
password = password[:-1]
font_label = font1.render(word, True, 'black')
label_w, label_h = font1.size(word)
screen.blit(font_label, (200 - label_w / 2, 100))
pygame.draw.rect(screen, color1, Enter1)
pygame.draw.rect(screen, color2, Enter2)
pygame.draw.rect(screen, 'black', OK)
pygame.draw.rect(screen, (20, 20, 20), OK2)
OK_surface = font1.render('into-->', True, (240, 190, 0))
width, height = font1.size('into-->')
screen.blit(OK_surface, (200 - width / 2, 490 + (80 - height) / 2))
name_surface = font1.render('name:', True, 'black')
nw, nh = font1.size('name:')
password_surface = font1.render('password:', True, 'black')
pw, ph = font1.size('password:')
name_answer_surface = font1.render(name, True, 'white')
password_answer_surface = font1.render(password, True, 'white')
name_width, name_height = font1.size(name)
password_width, password_height = font1.size(password)
screen.blit(name_answer_surface, (200 - name_width / 2, 200 + (80 - name_height) / 2))
screen.blit(password_answer_surface, (200 - password_width / 2, 325 + (80 - password_height) / 2))
screen.blit(name_surface, (200 - nw / 2, 150))
screen.blit(password_surface, (200 - pw / 2, 270))
if OK.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not OK_IF:
OK.width += 10
OK.height += 10
OK.x -= 5
OK.y -= 5
OK2.width += 10
OK2.height += 10
OK2.x -= 5
OK2.y -= 5
OK_IF = True
elif not OK.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and OK_IF:
OK.width -= 10
OK.height -= 10
OK.x += 5
OK.y += 5
OK2.width -= 10
OK2.height -= 10
OK2.x += 5
OK2.y += 5
OK_IF = False
if pygame.mouse.get_pressed()[0]:
if Enter1.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
color1 = (100, 100, 100)
color2 = (0, 0, 0)
elif Enter2.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
color2 = (100, 100, 100)
color1 = (0, 0, 0)
else:
color1 = (0, 0, 0)
color2 = (0, 0, 0)
if OK.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and password != '' and name != '' and data['name'] == None:
password_use.write(name, password)
first = True
time.sleep(1)
return
elif OK.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and data['name'] is not None:
if password_use.into(name, password):
return
else:
word = 'Name or password wrong!'
Clock.tick(fps)
pygame.display.update()
Init()
def First():
global data
while True:
screen.blit(people1, (0, 0))
screen.blit(people2, (0, 216))
screen.blit(people3, (0, 433))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pygame.mouse.get_pressed()[0]:
if pygame.mouse.get_pos()[1] < 216:
data['people'] = 'enemypic1.png'
data['player'] = 'yellow_airplane_r.png'
data['airport'].append('yellow_airplane_r.png')
return
elif 216 < pygame.mouse.get_pos()[1] < 433:
data['people'] = 'enemypic2.png'
data['player'] = 'pink_airplane_r.png'
data['airport'].append('pink_airplane_r.png')
return
elif 433 < pygame.mouse.get_pos()[1]:
data['people'] = 'enemypic3.png'
data['player'] = 'red_airplane_r.png'
data['airport'].append('red_airplane_r.png')
return
Clock.tick(fps)
pygame.display.update()
def Again():
while True:
screen.blit(bg, (0, 0))
font_surface = font1.render('Restart the game', True, 'black')
w, h = font1.size('Restart the game')
screen.blit(font_surface, (200 - w / 2, 325 - h / 2))
for event in pygame.event.get():
if event.type == pygame.QUIT:
system.into(data)
sys.exit()
Clock.tick(fps)
pygame.display.update()
if first:
people1 = pygame.image.load('something/1.jpg')
people2 = pygame.image.load('something/2.jpg')
people3 = pygame.image.load('something/3.jpg')
First()
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect((0, 0), (400, 650)))
Again()
people_Img = pygame.image.load('people/' + data['people'])
player = pygame.image.load('player/' + data['player'])
Lv = data['Lv']
need_exp = Lv * 100
exp = data['now_exp']
size = img_size.Size()
player_x = 200 - size.width() / 2
def PeoPle_Draw():
screen.blit(people_Img, (10, 540))
font_Lv = font1.render('Lv ' + str(Lv), True, 'black')
w, h = font1.size('Lv ' + str(Lv))
screen.blit(font_Lv, (110, 640 - h))
def Start():
font_surface1 = font3.render('Airplane Game', True, (240, 190, 0))
width1, height1 = font3.size('Airplane Game')
screen.blit(font_surface1, (200 - width1/2, 100))
pygame.draw.rect(screen, 'white', start_button)
pygame.draw.rect(screen, (230, 230, 230), start_button2)
font_surface2 = font3.render('start', True, (240, 190, 0))
width2, height2 = font3.size('start')
H1 = (70 - height2)/2 + 205
screen.blit(font_surface2, (200 - width2/2, H1))
pygame.draw.rect(screen, 'white', exit_button)
pygame.draw.rect(screen, (230, 230, 230), exit_button2)
font_surface3 = font3.render('exit', True, (240, 190, 0))
width3, height3 = font3.size('exit')
H2 = (70 - height3)/2 + 395
screen.blit(font_surface3, (200 - width3/2, H2))
pygame.draw.rect(screen, 'white', strengthen_button)
pygame.draw.rect(screen, (230, 230, 230), strengthen_button2)
font_surface4 = font3.render('shop', True, (240, 190, 0))
width4, height4 = font3.size('shop')
H3 = (70 - height4)/2 + 300
screen.blit(font_surface4, (200 - width4/2, H3))
screen.blit(gems, (0, 0))
font_surface5 = font1.render(' : ' + str(coin), True, (240, 190, 0))
screen.blit(font_surface5, (0, 0))
PeoPle_Draw()
Plus_1 = False
Plus_2 = False
Plus_3 = False
def Rect_Mouse():
global Plus_1, Plus_2, Plus_3
if start_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not Plus_1:
start_button.width += 10
start_button.height += 10
start_button.x -= 5
start_button.y -= 5
start_button2.width += 10
start_button2.height += 10
start_button2.x -= 5
start_button2.y -= 5
Plus_1 = True
elif not start_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and Plus_1:
start_button.width -= 10
start_button.height -= 10
start_button.x += 5
start_button.y += 5
start_button2.width -= 10
start_button2.height -= 10
start_button2.x += 5
start_button2.y += 5
Plus_1 = False
if strengthen_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not Plus_2:
strengthen_button.width += 10
strengthen_button.height += 10
strengthen_button.x -= 5
strengthen_button.y -= 5
strengthen_button2.width += 10
strengthen_button2.height += 10
strengthen_button2.x -= 5
strengthen_button2.y -= 5
Plus_2 = True
elif not strengthen_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and Plus_2:
strengthen_button.width -= 10
strengthen_button.height -= 10
strengthen_button.x += 5
strengthen_button.y += 5
strengthen_button2.width -= 10
strengthen_button2.height -= 10
strengthen_button2.x += 5
strengthen_button2.y += 5
Plus_2 = False
if exit_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not Plus_3:
exit_button.width += 10
exit_button.height += 10
exit_button.x -= 5
exit_button.y -= 5
exit_button2.width += 10
exit_button2.height += 10
exit_button2.x -= 5
exit_button2.y -= 5
Plus_3 = True
elif not exit_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and Plus_3:
exit_button.width -= 10
exit_button.height -= 10
exit_button.x += 5
exit_button.y += 5
exit_button2.width -= 10
exit_button2.height -= 10
exit_button2.x += 5
exit_button2.y += 5
Plus_3 = False
def Ready():
global start
if pygame.mouse.get_pressed()[0]:
if start_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
start = True
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect((0, 0), (400, 650)))
def Exit():
global running
if pygame.mouse.get_pressed()[0]:
if exit_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
running = False
def Mall():
global shop
if pygame.mouse.get_pressed()[0]:
if strengthen_button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
shop = True
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect((0, 0), (400, 650)))
def Shop_draw():
font_surface = font3.render('shop', True, (240, 190, 0))
width, height = font3.size('shop')
screen.blit(font_surface, (200 - width/2, 100))
pygame.draw.rect(screen, 'white', shop_return)
pygame.draw.rect(screen, (230, 230, 230), shop_return2)
font_R = font3.render('return', True, (240, 190, 0))
R_W, R_H = font3.size('return')
screen.blit(font_R, (200 - R_W/2, (70 - R_H)/2 + 490))
screen.blit(gems, (0, 0))
font_surface5 = font1.render(' : ' + str(coin), True, (240, 190, 0))
screen.blit(font_surface5, (0, 0))
word_sr_buy = 'buy'
word_ssr_buy = 'buy'
def Shop_sr():
screen.blit(shop_sr_plane, (30, 200))
screen.blit(shop_ssr_plane, (30, 320))
price_sr = font1.render('2000 $', True, (240, 190, 0))
price_w, price_h = font1.size('2000 $')
screen.blit(price_sr, (200 - price_w/2, 245.5 - price_h/2))
price_ssr = font1.render('2000 $', True, (240, 190, 0))
price_ww, price_hh = font1.size('2000 $')
screen.blit(price_ssr, (200 - price_ww / 2, 385 - price_hh / 2))
pygame.draw.rect(screen, 'white', buy_sr)
pygame.draw.rect(screen, (230, 230, 230), buy_sr2)
pygame.draw.rect(screen, 'white', buy_ssr)
pygame.draw.rect(screen, (230, 230, 230), buy_ssr2)
font_buy1 = font3.render(word_sr_buy, True, (240, 190, 0))
font_buy2 = font3.render(word_ssr_buy, True, (240, 190, 0))
width1, height1 = font3.size(word_sr_buy)
width2, height2 = font3.size(word_ssr_buy)
screen.blit(font_buy1, (270 + (120 - width1) / 2, 215.5 + (64 - height1) / 2)) # 215.5, 335
screen.blit(font_buy2, (270 + (120 - width2) / 2, 335 + (64 - height2) / 2))
coin_surface = font1.render(' : ' + str(coin), True, (240, 190, 0))
screen.blit(coin_surface, (0, 0))
sr_stop = False
ssr_stop = False
def Buy_stop():
global sr_stop, ssr_stop
if buy_sr.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not sr_stop:
buy_sr.width += 10
buy_sr.height += 10
buy_sr.x -= 5
buy_sr.y -= 5
buy_sr2.width += 10
buy_sr2.height += 10
buy_sr2.x -= 5
buy_sr2.y -= 5
sr_stop = True
elif not buy_sr.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and sr_stop:
buy_sr.width -= 10
buy_sr.height -= 10
buy_sr.x += 5
buy_sr.y += 5
buy_sr2.width -= 10
buy_sr2.height -= 10
buy_sr2.x += 5
buy_sr2.y += 5
sr_stop = False
if buy_ssr.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not ssr_stop:
buy_ssr.width += 10
buy_ssr.height += 10
buy_ssr.x -= 5
buy_ssr.y -= 5
buy_ssr2.width += 10
buy_ssr2.height += 10
buy_ssr2.x -= 5
buy_ssr2.y -= 5
ssr_stop = True
elif not buy_ssr.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and ssr_stop:
buy_ssr.width -= 10
buy_ssr.height -= 10
buy_ssr.x += 5
buy_ssr.y += 5
buy_ssr2.width -= 10
buy_ssr2.height -= 10
buy_ssr2.x += 5
buy_ssr2.y += 5
ssr_stop = False
def Buy():
global word_sr_buy, word_ssr_buy, data, coin
if pygame.mouse.get_pressed()[0]:
if buy_sr.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and coin >= 2000 and word_sr_buy == 'buy':
word_sr_buy = 'None'
data['player'] = 'pixel_airplane_sr.png'
data['buy_sr'] = True
coin -= 2000
if buy_ssr.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and coin >= 2000 and word_ssr_buy == 'buy' and word_sr_buy == 'None':
word_ssr_buy = 'None'
data['player'] = 'pixel_airplane_ssr.png'
data['buy_ssr'] = True
coin -= 2000
shop_IF = False
def Shop_r():
global shop_IF
if shop_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not shop_IF:
shop_return.width += 10
shop_return.height += 10
shop_return.x -= 5
shop_return.y -= 5
shop_return2.width += 10
shop_return2.height += 10
shop_return2.x -= 5
shop_return2.y -= 5
shop_IF = True
elif not shop_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and shop_IF:
shop_return.width -= 10
shop_return.height -= 10
shop_return.x += 5
shop_return.y += 5
shop_return2.width -= 10
shop_return2.height -= 10
shop_return2.x += 5
shop_return2.y += 5
shop_IF = False
def UP_SHOP_RETURN():
global shop
if pygame.mouse.get_pressed()[0]:
if shop_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
shop = False
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect((0, 0), (400, 650)))
def Bg_Move():
global bg_y1, bg_y2, win
if bg_number == 5:
bg_y1 += 0.1
if bg_y1 == 0:
bg_y1 -= 0.1
win = True
elif bg_number > 5:
bg_y1 += 1
bg_y2 += 1
if bg_y1 > 650:
bg_y1 = -650
elif bg_y2 > 650:
bg_y2 = -650
def Player_Move():
global player_x
if not pygame.mouse.get_pos()[0] <= 0 and not pygame.mouse.get_pos()[0] >= (400 - size.width() / 2):
player_x = pygame.mouse.get_pos()[0] - size.width() / 2
def Bullet_Mode():
global bullets
bullet = Actor('bullets/bullet_pixel_r.png')
bullets.append(bullet)
bullet.x = player_x + (size.width() / 2 - 17.5)
bullet.y = player_y
def Bullet_Move():
global bullets
for b in bullets:
b.y -= 5
if b.y < -50:
bullets.remove(b)
def Enemy_Mode():
global enemies
enemy = Actor('enemy/enemy.png')
enemies.append(enemy)
enemy.x = random.randint(0, 312)
enemy.y = 0
def Enemy_Move():
global enemies, score
for e in enemies:
e.y += 2
if e.y > 600:
enemies.remove(e)
score -= 1
def Bomb():
global bullets, enemies, score, coin
for b in bullets:
for e in enemies:
b_mask = pygame.mask.from_surface(b.image)
e_mask = pygame.mask.from_surface(e.image)
if b_mask.overlap(e_mask, (e.x - b.x, e.y - b.y)) is not None:
bullets.remove(b)
enemies.remove(e)
score += 1
coin += 10
def Over():
global enemies, player, game_over
player_mask = pygame.mask.from_surface(player)
for e in enemies:
e_mask = pygame.mask.from_surface(e.image)
if e_mask.overlap(player_mask, (player_x - e.x, player_y - e.y)) is not None:
game_over = True
def Level_draw():
global level
font_surface = font1.render('Level: ' + str(level), True, (132, 132, 255))
width, height = font1.size('Level: ' + str(level))
screen.blit(font_surface, (200 - width/2, 0))
def Distance():
global km, win
km += 1
font_km = font1.render(str(km) + ' km', True, (0, 0, 0))
screen.blit(font_km, (0, 0))
if km > task:
win = True
def Win():
font_surface = font2.render('You winning!', True, (240, 190, 0))
width, height = font2.size('You winning!')
screen.blit(font_surface, (200 - width/2, 100))
score_font = font1.render('Score: ' + str(score), True, (19, 98, 43))
score_width, score_height = font1.size('Score: ' + str(score))
screen.blit(score_font, (200 - score_width / 2, 200))
km_font = font1.render('Distance: ' + str(km) + 'km', True, (31, 37, 116))
km_width, km_height = font1.size('Distance: ' + str(km) + 'km')
screen.blit(km_font, (200 - km_width / 2, 300))
pygame.draw.rect(screen, 'white', continue_Button)
pygame.draw.rect(screen, (230, 230, 230), continue_Button2)
font_C = font3.render('next->', True, (240, 190, 0))
C_W, C_H = font3.size('next->')
screen.blit(font_C, (200 - C_W/2, (70 - C_H)/2 + 390))
pygame.draw.rect(screen, 'white', win_return)
pygame.draw.rect(screen, (230, 230, 230), win_return2)
font_R = font3.render('return', True, (240, 190, 0))
R_W, R_H = font3.size('return')
screen.blit(font_R, (200 - R_W/2, (70 - R_H)/2 + 490))
IF_WIN = False
IF_WIN2 = False
def Win_Button():
global IF_WIN, IF_WIN2
if continue_Button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not IF_WIN:
continue_Button.width += 10
continue_Button.height += 10
continue_Button.x -= 5
continue_Button.y -= 5
continue_Button2.width += 10
continue_Button2.height += 10
continue_Button2.x -= 5
continue_Button2.y -= 5
IF_WIN = True
elif not continue_Button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and IF_WIN:
continue_Button.width -= 10
continue_Button.height -= 10
continue_Button.x += 5
continue_Button.y += 5
continue_Button2.width -= 10
continue_Button2.height -= 10
continue_Button2.x += 5
continue_Button2.y += 5
IF_WIN = False
if win_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not IF_WIN2:
win_return.width += 10
win_return.height += 10
win_return.x -= 5
win_return.y -= 5
win_return2.width += 10
win_return2.height += 10
win_return2.x -= 5
win_return2.y -= 5
IF_WIN2 = True
elif not win_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and IF_WIN2:
win_return.width -= 10
win_return.height -= 10
win_return.x += 5
win_return.y += 5
win_return2.width -= 10
win_return2.height -= 10
win_return2.x += 5
win_return2.y += 5
IF_WIN2 = False
def Win_UP():
global win, start, coin
if pygame.mouse.get_pressed()[0]:
if continue_Button.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
win = False
Level()
elif win_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
win = False
start = False
Level()
def Level():
global level, player_x, player_y, score, enemies, bullets, bg_number, bg_Img, wars_bg1, km, task
level += 1
if len(str(level)) == 1:
pass
else:
bg_number = int(str(level)[0]) + 1
bg_Img = 'bg' + str(bg_number) + '.png'
wars_bg1 = pygame.image.load('bg/' + bg_Img)
player_x = 160
player_y = 470
score = 0
enemies = []
bullets = []
data['task'] += 100
km = 0
task += 100
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect((0, 0), (400, 650)))
def Over_draw():
font_surface = font2.render(dead, True, 'red')
width, height = font2.size(dead)
screen.blit(font_surface, (200 - width/2, 100))
def Over_rect():
pygame.draw.rect(screen, 'white', over_return)
pygame.draw.rect(screen, (230, 230, 230), over_return2)
font_surface = font3.render('return', True, (240, 190, 0))
width, height = font3.size('return')
H = (70 - height)/2 + 495
screen.blit(font_surface, (200 - width/2, H))
r = False
def Over_Mouse_rect():
global r
if over_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and not r:
over_return.width += 10
over_return.height += 10
over_return.x -= 5
over_return.y -= 5
over_return2.width += 10
over_return2.height += 10
over_return2.x -= 5
over_return2.y -= 5
r = True
elif not over_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) and r:
over_return.width -= 10
over_return.height -= 10
over_return.x += 5
over_return.y += 5
over_return2.width -= 10
over_return2.height -= 10
over_return2.x += 5
over_return2.y += 5
r = False
def UP_rect():
global game_over, start, player_x, player_y, score, enemies, bullets
if pygame.mouse.get_pressed()[0]:
if over_return.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
game_over = False
start = False
pygame.draw.rect(screen, (0, 0, 0), pygame.Rect((0, 0), (400, 650)))
player_x = 160
player_y = 470
score = 0
enemies = []
bullets = []
stop = False
stop_Img = pygame.image.load('something/stop.png')
running = True
ago_time = time.time()
bullet_ago_time = time.time()
Gems = Gems_Get()
def main():
global running, player_x, player_y, ago_time, game_over, win, bullet_ago_time, Lv, exp
while running:
if not game_over and start and not win and not shop:
bullet_now_time = time.time()
now_time = time.time()
screen.blit(wars_bg1, (0, bg_y1))
screen.blit(stop_Img, (347, 0))
if bg_number > 5:
screen.blit(wars_bg2, (0, bg_y2))
Bg_Move()
screen.blit(player, (player_x, player_y))
for B in bullets:
B.draw()
for E in enemies:
E.draw()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
pass
elif event.type == pygame.KEYUP:
pass
if pygame.mouse.get_pressed()[0] and (bullet_now_time - bullet_ago_time) > 0.1:
Bullet_Mode()
bullet_ago_time = time.time()
if (now_time - ago_time) > 1:
Enemy_Mode()
ago_time = time.time()
Distance()
Player_Move()
Bullet_Move()
Enemy_Move()
Bomb()
Level_draw()
Over()
if player_x < 0:
player_x = 0
elif player_x > 320:
player_x = 320
Clock.tick(fps)
pygame.display.update()
elif not start and not shop:
screen.blit(bg, (0, 0))
screen.blit(player, (player_x, player_y))
Start()
Ready()
Exit()
Mall()
Rect_Mouse()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Clock.tick(fps)
pygame.display.update()
elif shop:
screen.blit(bg, (0, 0))
Shop_draw()
Shop_r()
Shop_sr()
Buy_stop()
Buy()
UP_SHOP_RETURN()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Clock.tick(fps)
pygame.display.update()
elif win:
Gems.init()
Gems.move()
screen.blit(bg, (0, 0))
screen.blit(player, (player_x, player_y))
Win()
Win_Button()
Win_UP()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Clock.tick(fps)
pygame.display.update()
elif game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Over_draw()
Over_rect()
Over_Mouse_rect()
UP_rect()
Clock.tick(fps)
pygame.display.update()
if exp > need_exp:
Lv += 1
exp -= need_exp
if __name__ == '__main__':
main()
system.into(data)
建立一个data目录创建data.dat文件
写入初始的游戏信息
from cryptography.fernet import Fernet
import json
# 生成密钥
key = b'dNcf9UYoAaqUcyrPS0CLIdEQbGzMRyniE9RUMlgGKfg=' # 可以自己定哦!
# 初始化加密器
cipher = Fernet(key)
data = {
'name': None,
'password': None,
'people': None,
'player': 'pixel_airplane_r.png',
'level': 1,
'coin': 0,
'Lv': 1,
'task': 3000,
'now_exp': 0,
'airport': []
}
json_data = json.dumps(data).encode()
encrypted_data = cipher.encrypt(json_data)
# 将加密后的数据写入文件
with open('data/data.dat', 'wb') as f:
f.write(encrypted_data)
print(key)
一定要记住密钥
不然无法解密
find.py
from cryptography.fernet import Fernet
import json
# 加载密钥
key = b'dNcf9UYoAaqUcyrPS0CLIdEQbGzMRyniE9RUMlgGKfg='
# 初始化加密器
cipher = Fernet(key)
class File:
def into(self, data):
json_data = json.dumps(data).encode()
encrypted_data = cipher.encrypt(json_data)
# 将加密后的数据写入文件
with open('data/data.dat', 'wb') as f:
f.write(encrypted_data)
def read(self):
with open('data/data.dat', 'rb') as f:
encrypted_data = f.read()
# 解密数据
decrypted_data = cipher.decrypt(encrypted_data)
json_data = decrypted_data.decode()
data = json.loads(json_data)
return data
img_size.py # 防止图片错位(暂时没有弄商店图片的位置)
from PIL import Image
import find
import os
system = find.File()
data = system.read()
class Size:
if data['player'] is not None:
__image_name__ = 'player/' + data['player']
__image_path__ = os.path.abspath(__image_name__)
__image__ = Image.open(__image_path__)
def __init__(self, image=None):
if image is None:
pass
elif '.' in image:
self.__image_name__ = image
self.__image_path__ = os.path.abspath(self.__image_name__)
self.__image__ = Image.open(self.__image_path__)
def size(self):
return self.__image__.size
def width(self):
return self.__image__.width
def height(self):
return self.__image__.height
def format(self):
return self.__image__.format
fire是空的, 可以在AirplaneGame.py中删掉
Bug1: 失败后km不会重置(懒的搞)
Bug2:失败的返回rect有问题(不知道怎么回事)
Bug3:金币和关卡丢失(
在main()后加上
data['level'] = level data['coin'] = coin
)

这是一篇关于Pygame开发飞机大战游戏的文章,详细介绍了AirplaneGame.py的实现过程,包括创建data目录和data.dat文件来存储游戏信息。文章提到了几个已知的Bug,如游戏失败后km不重置、返回界面的rect问题以及金币和关卡数据丢失等,但并未提供解决方案。
1541





