-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aircraft.py
71 lines (62 loc) · 2.56 KB
/
Aircraft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pygame
import json
class Aircraft(pygame.sprite.Sprite):
""" 玩家控制的飞行器 """
def __init__(self, screen, position):
super(Aircraft, self).__init__()
""" 显示屏幕 """
self.screen = screen
""" 火箭种类 """
self.AircraftNumber = self.LoadAircraftChoose()
""" 火箭位置 """
self.position = position
""" 火箭速度 """
self.speed = [2, 2]
""" 加载图片 """
self.AllImage = []
self.AllImage.append(
pygame.image.load('pictures/AirCraft/0.png').convert_alpha())
self.AllImage.append(
pygame.image.load('pictures/AirCraft/1.png').convert_alpha())
self.AllImage.append(
pygame.image.load('pictures/AirCraft/2.png').convert_alpha())
self.AllImage.append(
pygame.image.load('pictures/AirCraft/3.png').convert_alpha())
self.AllImage.append(
pygame.image.load('pictures/AirCraft/4.png').convert_alpha())
self.image = pygame.transform.smoothscale(self.AllImage[self.AircraftNumber], (50, 40))
self.rect = self.image.get_rect()
self.rect.centerx = self.position[0]
self.rect.centery = self.position[1]
def draw(self):
""" 绘制火箭 """
self.screen.blit(self.image, self.rect)
def move(self, width, height):
""" 火箭移动 """
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and self.rect.top >= 0:
self.rect.centery -= self.speed[1]
if keys[pygame.K_s] and self.rect.bottom <= height:
self.rect.centery += self.speed[1]
if keys[pygame.K_a] and self.rect.left >= 0:
self.rect.centerx -= self.speed[0]
if keys[pygame.K_d] and self.rect.right <= width:
self.rect.centerx += self.speed[0]
def OptionDraw(self,width,height,choose):
""" 设置界面绘制火箭 """
self.OptionImage = self.AllImage[choose]
self.OptionRect = self.AllImage[choose].get_rect()
self.OptionRect.centerx=400
self.OptionRect.centery=height/2
self.screen.blit(self.OptionImage,self.OptionRect)
def LoadAircraftChoose(self):
""" 读取火箭选择 """
try:
with open('AircraftChoose.json', 'r') as f:
HistoryChoose = json.load(f)
return HistoryChoose
except:
DefaultChoose = 1
with open('AircraftChoose.json', 'w') as f:
HistoryChoose = json.dump(DefaultChoose, f)
return DefaultChoose