first commit

main
夏铭 6 months ago
parent 438e0652fc
commit 4f98e3147b

@ -0,0 +1,26 @@
import pygame
from .. import tools,setup
from .. import constants as C
class Box(pygame.sprite.Sprite):
def __init__(self, x, y,box_type):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.box_type = box_type
self.frame_rects = [
(384,0,16,16),
(400,0,16,16),
(416,0,16,16),
(432,0,16,16),
]
self.frames = []
for frame_rect in self.frame_rects:
self.frames.append(tools.get_image(setup.GRAPHICS['tile_set'],*frame_rect,(0,0,0),C.BRICK_MULTI)) # 遍历加载图片
self.frame_index = 0
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y # 设置坐标与图片

@ -3,22 +3,22 @@ from .. import tools,setup
from .. import constants as C
class Brick(pygame.sprite.Sprite):
def __init__(self, x, y,brick_type,color=1):
def __init__(self, x, y,brick_type,color=None):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.type = brick_type
bright_rect_frames = [(16,0,16,16),(48,0,16,16)]
dark_rect_frames = [(16,32,16,16),(48,32,16,16)] # 抠图
bright_frame_rects = [(16,0,16,16),(48,0,16,16)]
dark_frame_rects = [(16,32,16,16),(48,32,16,16)] # 抠图
if not color:
self.frame_rects = bright_rect_frames
self.frame_rects = bright_frame_rects
else:
self.frame_rects = dark_rect_frames
self.frame_rects = dark_frame_rects
self.frames = []
for frame_rect in self.frame_rects:
self.frames.append(tools.get_image(setup.GRAPHICS['tile_set'],*frame_rect,(0,0,0),C.BG_MULTI)) # 遍历加载图片
self.frames.append(tools.get_image(setup.GRAPHICS['tile_set'],*frame_rect,(0,0,0),C.BRICK_MULTI)) # 遍历加载图片
self.frame_index = 0
self.image = self.frames[self.frame_index]

@ -0,0 +1,73 @@
import pygame
from .. import setup,tools
from .. import constants as C
def create_enemy(enemy_data):
enemy_type = enemy_data['type']
x,y_bottom,direction,color = enemy_data['x'],enemy_data['y'],enemy_data['direction'],enemy_data['color']
if enemy_type == 0: # 蘑菇类
enemy = Goomba(x,y_bottom,direction,'goomba',color)
elif enemy_type == 1:
enemy = Koopa(x,y_bottom,direction,'koopa',color)
return enemy
class Enemy(pygame.sprite.Sprite):
def __init__(self,x,y_bottom,direction,name,frame_rects):
pygame.sprite.Sprite.__init__(self)
self.direction = direction
self.name = name
self.frame_index = 0
self.left_frames = []
self.right_frames = []
self.load_frames(frame_rects)
self.frames = self.left_frames if self.direction == 0 else self.right_frames
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.bottom = y_bottom
self.timer = 0
def load_frames(self,frame_rects):
for frame_rect in frame_rects:
left_frame = tools.get_image(setup.GRAPHICS['enemies'],*frame_rect,(0,0,0),C.ENEMY_MULTI)
right_frame = pygame.transform.flip(left_frame,True,False)
self.left_frames.append(left_frame)
self.right_frames.append(right_frame)
def update(self):
self.current_time = pygame.time.get_ticks()
if self.current_time - self.timer > 125:
self.frame_index = (self.frame_index + 1) % 2
self.image = self.frames[self.frame_index]
self.timer = self.current_time
class Goomba(Enemy):
def __init__(self, x, y_bottom, direction, name, color):
bright_frame_rects = [(0, 16, 16, 16), (16, 16, 16, 16), (32, 16, 16, 16)]
dark_frame_rects = [(0, 48, 16, 16), (16, 48, 16, 16), (32, 48, 16, 16)] # 抠图
if not color:
frame_rects = bright_frame_rects
else:
frame_rects = dark_frame_rects
Enemy.__init__(self,x,y_bottom,direction,name,frame_rects)
class Koopa(Enemy):
def __init__(self, x, y_bottom, direction, name, color):
bright_frame_rects = [(96, 9, 16, 22), (112, 9, 16, 22), (160, 9, 16, 22)]
dark_frame_rects = [(96, 72, 16, 2), (112, 72, 16, 22), (160, 72, 16, 22)] # 抠图
if not color:
frame_rects = bright_frame_rects
else:
frame_rects = dark_frame_rects
Enemy.__init__(self,x, y_bottom, direction, name, frame_rects)

@ -4,6 +4,8 @@ GROUND_HEIGHT = SCREEN_H - 62
BG_MULTI = 2.68
PLAYER_MULTI = 2.9
BRICK_MULTI = 2.69
ENEMY_MULTI = 2.5
GRAVITY = 1.0
ANTI_GRAVITY = 0.3

@ -154,7 +154,7 @@
{"x":4330, "y":538, "direction":0, "type":0, "color":0}
]},
{"5":[
{"x":4700, "y":538, "direction":0, "type":1, "color":1}
{"x":4700, "y":538, "direction":0, "type":1, "color":0}
]},
{"6":[
{"x":4900, "y":538, "direction":0, "type":0, "color":0},

@ -2,7 +2,7 @@ from ..components import info
import pygame
from .. import tools, setup
from .. import constants as C
from .. components import player,stuff,brick
from .. components import player,stuff,brick,box,enemy
import os
import json
@ -17,7 +17,8 @@ class Level:
self.setup_start_positions()
self.setup_player()
self.setup_ground_items()
self.setup_bricks()
self.setup_bricks_and_boxes()
self.setup_enemies()
def load_map_data(self):
file_name = 'level_1.json'
@ -56,8 +57,9 @@ class Level:
self.ground_items_group.add(stuff.Item(item['x'],item['y'],item['width'],item['height'],name))
def setup_bricks(self):
def setup_bricks_and_boxes(self):
self.brick_group = pygame.sprite.Group()
self.box_group = pygame.sprite.Group()
if'brick' in self.map_data:
for brick_data in self.map_data['brick']: # 遍历得到xy坐标以及type
@ -69,6 +71,22 @@ class Level:
else:
self.brick_group.add(brick.Brick(x,y,brick_type))
if 'box' in self.map_data:
for box_data in self.map_data['box']: # 遍历得到xy坐标以及type
x, y = box_data['x'], box_data['y']
box_type = box_data['type']
self.brick_group.add(box.Box(x, y, box_type))
def setup_enemies(self):
self.enemy_group_dict = {}
for enemy_group_data in self.map_data['enemy']: # 字典存放
group = pygame.sprite.Group()
for enemy_group_id,enemy_list in enemy_group_data.items():
for enemy_data in enemy_list:
group.add(enemy.create_enemy(enemy_data)) # 调用 ce
self.enemy_group_dict[enemy_group_id] = group
def update(self, surface, keys):
@ -86,6 +104,9 @@ class Level:
self.update_game_window()
self.info.update()
self.brick_group.update()
self.box_group.update()
for enemy_group in self.enemy_group_dict.values():
enemy_group.update()
self.draw(surface)
@ -105,15 +126,17 @@ class Level:
self.check_y_collisions()
def check_x_collisions(self): # x方向
ground_item = pygame.sprite.spritecollideany(self.player, self.ground_items_group) # 检测碰撞
if ground_item:
self.adjust_player_x(ground_item)
check_group = pygame.sprite.Group(self.ground_items_group,self.brick_group,self.box_group)
collided_sprite = pygame.sprite.spritecollideany(self.player,check_group) # 检测碰撞
if collided_sprite:
self.adjust_player_x(collided_sprite)
def check_y_collisions(self): # y方向
ground_item = pygame.sprite.spritecollideany(self.player, self.ground_items_group) # 检测碰撞
if ground_item:
self.adjust_player_y(ground_item)
def check_y_collisions(self): # y方向
check_group = pygame.sprite.Group(self.ground_items_group, self.brick_group,self.box_group)
collided_sprite = pygame.sprite.spritecollideany(self.player, check_group) # 检测碰撞
if collided_sprite:
self.adjust_player_y( collided_sprite)
self.check_will_fall(self.player) # 坠落检测
def adjust_player_x(self,sprite):
@ -135,9 +158,9 @@ class Level:
def check_will_fall(self,sprite):
sprite.rect.y += 1 # 下落一个像素
check_ground = pygame.sprite.Group(self.ground_items_group) # 检测是否碰撞
collided = pygame.sprite.spritecollideany(sprite, check_ground)
if not collided and sprite.state != 'jump' : # 如果没有或者处于跳起则不坠落
check_group = pygame.sprite.Group(self.ground_items_group,self.brick_group,self.box_group) # 检测是否碰撞
collided_sprite = pygame.sprite.spritecollideany(sprite,check_group)
if not collided_sprite and sprite.state != 'jump' : # 如果没有或者处于跳起则不坠落
sprite.state = 'fall'
sprite.rect.y -= 1
@ -151,6 +174,9 @@ class Level:
self.game_ground.blit(self.background,self.game_window,self.game_window)
self.game_ground.blit(self.player.image,self.player.rect) # 将背景与人物化境
self.brick_group.draw(self.game_ground)
self.box_group.draw(self.game_ground)
for enemy_group in self.enemy_group_dict.values():
enemy_group.draw(self.game_ground)
surface.blit(self.game_ground,(0,0), self.game_window) # 渲染
self.info.draw(surface)

Loading…
Cancel
Save