1.游戏分为5个文件,各个文件间相互调用
五个文件:一个是怪物,一个是子弹,一个是地图,一个是Manager类,一个是声音
(声音类很简单,基本不用去管它)
2.bullets文件还远远不够完善,需要等待怪物大战2.0版本的出现
3.这次初步弄好了monster文件和maps文件
4.之后可以把Manager分成两个类--一个专门负责页面,另一个负责逻辑和碰撞
5.也可以把一些比较抽象,不会实例化的类写在一个文件里
6.如果之后代码很多,不需要time。sleep了,那时就应该用多线程了
7.之后那些方块类可以更加完善
8.争取把碰撞的代码写进类里,发生在不同类之间的碰撞甚至可以写个碰撞类吧
以下只显示怪物文件和地图文件
`
class Monster(pygame.sprite.Sprite):
def init(self,screen):
pygame.sprite.Sprite.init(self)
self.screen = screen
self.all_bullet = pygame.sprite.Group()
self.imgs = {
'w': pygame.image.load('E:/图片/player-w.png'),
's': pygame.image.load('E:/图片/player-s.png'),
'a': pygame.image.load('E:/图片/player-a.png'),
'd': pygame.image.load('E:/图片/player-d.png')
}#默认是玩家图片
self.direction = 'w'
self.img = self.imgs[self.direction]
self.rect = self.img.get_rect()
def boundary(self):
# 地图是上下左右贯通的
# up
if self.rect.top <= 0-20:
self.rect.top = SCREEN_Y + 10
# down
if self.rect.top >= SCREEN_Y + 20:
self.rect.top = 0 - 20
# left
if self.rect.left <= 0-20:
self.rect.left = SCREEN_X + 10
# right
if self.rect.left >= SCREEN_X + 20:
self.rect.left = 0 - 20
def display(self):
self.img = self.imgs[self.direction]
self.screen.blit(self.img, self.rect)
for i in self.all_bullet:
i.move_straight_forward()
self.all_bullet.draw(self.screen)
class Player(Monster):
def init(self,screen):
Monster.init(self,screen)
self.ran = [0, 4, 0]
self.weapon = 1
self.speed = 5
self.rect.topleft = [random.randrange(30, SCREEN_X, 80), random.randrange(30, SCREEN_Y, 20)]
def control(self):
super().boundary()
# 按键控制(键盘连续监听)
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w]:
self.direction = 'w'
self.rect.top -= self.speed + self.ran[2]
self.ran[2] = 0
if key_pressed[pygame.K_s]:
self.direction = 's'
self.rect.top += self.speed + self.ran[2]
self.ran[2] = 0
if key_pressed[pygame.K_a]:
self.direction = 'a'
self.rect.left -= self.speed+ self.ran[2]
self.ran[2] = 0
if key_pressed[pygame.K_d]:
self.direction = 'd'
self.rect.left += self.speed + self.ran[2]
self.ran[2] = 0
if key_pressed[pygame.K_j]:
#控制子弹发射的频率
rrr = random.randint(1, 5)
if self.weapon == 1:
if rrr == 3:
b = Bullet(self.direction, self.rect)
self.all_bullet.add(b)
elif self.weapon == 2:
if rrr == 2:
b = Biao(self.direction, self.rect)
self.all_bullet.add(b)
if key_pressed[pygame.K_h]:
# 加速
self.ran[2] += random.randint(1,10)
if self.ran[2] >= 35:self.ran[2] = 35
if key_pressed[pygame.K_1]:
self.weapon = 1
elif key_pressed[pygame.K_2]:
self.weapon = 2
elif key_pressed[pygame.K_3]:
self.weapon = 3
elif key_pressed[pygame.K_4]:
self.weapon = 4
class Common(Monster):
def init(self,screen):
Monster.init(self, screen)
self.imgs = {
'w': pygame.image.load('E:/图片/dir-w.png'),
's': pygame.image.load('E:/图片/dir-s.png'),
'a': pygame.image.load('E:/图片/dir-a.png'),
'd': pygame.image.load('E:/图片/dir-d.png')
}
self.ran = [0, 4, 0]
self.speed = random.randint(1, 5)
self.rect.topleft = [random.random() * SCREEN_X, random.random() * SCREEN_Y]
def ai(self):
super().boundary()
self.ran[0] += 1
if self.ran[0] % 30 == 0:
bb = Bullet(self.direction, self.rect)
self.all_bullet.add(bb)
if self.ran[1] % 4 == 0:
self.direction = 'w'
self.rect.top -= self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
elif self.ran[1] % 4 == 1:
self.direction = 's'
self.rect.top += self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
elif self.ran[1] % 4 == 2:
self.direction = 'a'
self.rect.left -= self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
elif self.ran[1] % 4 == 3:
self.direction = 'd'
self.rect.left += self.speed
if self.ran[0] % 20 == 0:
self.ran[1] = random.randint(4,7)
`
`
class Cube(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
self.img = pygame.image.load('E:/图片/0.png')
self.rect = self.img.get_rect()
self.rect.topleft = [random.random() * SCREEN_X, random.random() * SCREEN_Y]
class Pump_cube(Cube):
def init(self):
# 刚体不能与任何东西发生重合,坦克/刚体可以给刚体一个速度
# 坦克/刚体与刚体发生碰撞,则它们之间的边缘会重合
# 刚体可以有多个速度,遵守矢量相加的规律
Cube.init(self)
self.img = pygame.image.load('E:/图片/6.png')
self.sp1 = [] # 记录上下方向及速度,下方向为正速度
self.sp2 = [] # 记录左右方向及速度,右方向为正速度
self.speed1 = 0
self.speed2 = 0
self.dir1 = ''
self.dir2 = ''
def pump(self,dir,speed,rect):
#坦克碰刚体/刚体碰刚体
if dir == 'w':
self.rect.top = rect.top - self.rect.height
self.sp1.append(-speed)
elif dir == 's':
self.rect.top = rect.top + rect.height
self.sp1.append(speed)
elif dir == 'a':
self.rect.left = rect.left - self.rect.width
self.sp2.append(-speed)
elif dir == 'd':
self.rect.left = rect.left + rect.width
self.sp2.append(speed)
self.speed1 = sum(self.sp1)
self.speed2 = sum(self.sp2)
if self.speed1 > 0:
self.dir1 = 's'
else:
self.dir1 = 'w'
if self.speed2 > 0:
self.dir2 = 'd'
else:
self.dir2 = 'a'
class Shoot_cube(Cube):
def init(self,attack=1):
Cube.init(self)
self.img = pygame.image.load('E:/图片/1.png')
self.attack = attack
def shoot(self):
# 子弹碰brick
if self.attack >= 1:
self.attack -= 1
if self.attack == 0:
self.kill()
class Stone(Pump_cube,Shoot_cube):
def init(self,attack=1):
Pump_cube.init(self)
Shoot_cube.init(self,attack)
self.img = pygame.image.load('E:/图片/2.png')
基岩bedrock:不能消失,不能推开(class Cube)
砖块brick:可以打掉,不能推开(class Shoot_cube)
羊毛wool:不能消失,可以推开(class Pump_cube)
石头stone:可以打掉,可以推开(class Stone)
class Thing(Cube):
def init(self):
Cube.init(self)
self.img = pygame.image.load('E:/图片/h.jpg')
def life_plus(self):
pass
class Map:
def init(self,screen):
self.bedrock = pygame.sprite.Group()
self.brick = pygame.sprite.Group()
self.wool = pygame.sprite.Group()
self.stone = pygame.sprite.Group()
self.screen = screen
def clear(self):
self.bedrock.empty()
self.brick.empty()
self.wool.empty()
self.stone.empty()
def ww(self,a,style,mm):
for i in range(a):
if style == 0:#横着的方块
mm[0].rect.topleft = [random.randint(0,SCREEN_X),random.randrange(int(SCREEN_Y/4)
,int(3*SCREEN_Y/4),10)]
mm[i].rect.top = mm[i-1].rect.top
mm[i].rect.left = mm[i-1].rect.left + 55
elif style == 1:#竖着的方块
mm[0].rect.topleft = [random.randrange(int(SCREEN_X/5),int(5*SCREEN_X/6),15)
,random.randint(0,SCREEN_Y)]
mm[i].rect.top = mm[i - 1].rect.top + 55
mm[i].rect.left = mm[i - 1].rect.left
elif style == 2:#斜着的方块
mm[0].rect.topleft = [random.randrange(int(SCREEN_X/3),int(5*SCREEN_X/6),20),
random.randrange(int(SCREEN_Y/5),int(2*SCREEN_Y/5),10)]
mm[i].rect.top = mm[i - 1].rect.top + 55
mm[i].rect.left = mm[i - 1].rect.left + 55
def map1(self, n, style=-1, type='bedrock', attack=1):
if type == 'bedrock':
mm = [Cube() for _ in range(n)]
self.ww(n, style, mm)
self.bedrock.add(mm)
elif type == 'brick':
mm = [Shoot_cube(attack) for _ in range(n)]
self.ww(n, style, mm)
self.brick.add(mm)
elif type == 'wool':
mm = [Pump_cube() for _ in range(n)]
self.ww(n, style, mm)
self.wool.add(mm)
elif type == 'stone':
mm = [Stone(attack) for _ in range(n)]
self.ww(n, style, mm)
self.stone.add(mm)
def display_all(self):
for i in self.bedrock:
self.screen.blit(i.img, i.rect)
for j in self.brick:
self.screen.blit(j.img, j.rect)
for k in self.wool:
self.screen.blit(k.img,k.rect)
for l in self.stone:
self.screen.blit(l.img,l.rect)
`