def init_elements(self): self.view_port = ViewPort() self.controller = UserController() self.resource = Resources() self.sprite_group = IGroup() self.explosion_group = IGroup() self.map = Map() main_sprite = Qie(self) main_sprite.pos = np.random.randint(-100, 100, size=(1, 2)) self.sprite_group.add(main_sprite) main_sprite.attack_range = 2000 insect = Insect(self) insect.pos = np.random.randint(-100, 100, size=(1, 2)) insect.speed = 1 insect.target = main_sprite self.sprite_group.add(insect) exp = BombExplosion(self) exp.pos = np.random.randint(-100, 100, size=(1, 2)) self.explosion_group.add(exp) exp = MissalBullet(self) exp.pos = np.random.randint(-100, 100, size=(1, 2)) exp.speed = 1 exp.target = main_sprite self.sprite_group.add(exp) self.framerate.tick(30) ticks = pygame.time.get_ticks() exp.explode(ticks) self.controller.main_actor = main_sprite
def __init__(self, parent=None): self._list = [] self.partical_type = None self.parent = parent self.n_rows = 10 self.destination = np.array([[1, 2]]) self.width = 16 self.height = 16 self.image = None self._switcher = False self.last_time = 0 self.view_port = ViewPort() self.init()
def __init__(self, scene): self.image = None self.rect = None super(Bullet, self).__init__(self) self.scene = scene self.screen = scene.screen if scene.screen else None self.pos = np.array([0, 0]) self.explosion = None self.view_port = ViewPort()
def __init__(self, scene): self.image = None self.rect = None super(Explosion, self).__init__(self) self.scene = scene self.screen = scene.screen if scene.screen else None self.be_lelf_right = None self.pos = np.array([0, 0]) self.view_port = ViewPort() self.last_time = 0
def __init__(self, scene): self.image = None self.rect = None super(SpriteActor, self).__init__() self.scene = scene self.screen = scene.screen if scene.screen else None self.can_selected = True self.last_attack_time = 0 self.attack_rate = 1000 self.type = 0 self.target = None self.target_pos = None self.width_height = np.array([32, 32]) self.be_lelf_right = True self.be_attacked = False self.be_attacked_last_time = 0 self.be_attacked_rate = 20000 self.view_port = ViewPort()
width_height = np.array([32, 32]) pos = np.array([0, 0]) random_pos = np.array([2, 2]) print("在范围内", is_in_rect(random_pos, pos, width_height)) random_pos = np.array([-2, 2]) print("不在范围内", is_in_rect(random_pos, pos, width_height)) random_pos = np.array([40, 2]) print("不在范围内", is_in_rect(random_pos, pos, width_height)) for actor in actor_list: print(is_in_rect(random_pos, actor.pos, width_height)) pos = np.array([10, 0]) direction_pos = np.array([0, 5]) # left_or_right() print("left", left_or_right(direction_pos, pos)) direction_pos = np.array([20, 5]) print("right", left_or_right(direction_pos, pos)) width_height = np.array([32, 32]) pos = np.array([0, 0]) random_pos = np.array([2, 2]) view_port = ViewPort() world_pos = view_port.to_world(random_pos) print("不在范围内", is_in_rect(world_pos, pos, width_height)) if direction_pos is None: print("T")
framerate.tick(30) ticks = pygame.time.get_ticks() group.update(ticks) framerate.tick(30) ticks = pygame.time.get_ticks() group.update(ticks) framerate.tick(30) ticks = pygame.time.get_ticks() group.update(ticks) del attacked_actor framerate.tick(30) ticks = pygame.time.get_ticks() group.update(ticks) print(len("")) view_port = ViewPort() controller = UserController() pygame.display.set_caption("精灵类测试") framerate = pygame.time.Clock() for sprite in group.sprites(): print(sprite.pos) import json print(actor.map_editor_serialize())
import pygame import numpy as np from zhak_projects.agame.elements.Ghost import Ghost, Yaorao, Xiaohua, Tiaotiao, Qie, Kuaile, Phoenix from zhak_projects.agame.elements.Resources import Resources from zhak_projects.agame.elements.SpriteActor import SpriteActor from zhak_projects.agame.map import ViewPort, VIEW_PORT_W, VIEW_PORT_H, Map # from zhak_projects.agame.test_game.test_run_map_bilt import tilesize from zhak_projects.agame.zhgame import UserController, LIFE_STATE_GHOST, is_in_rect from zhak_projects.groups import IGroup pygame.init() print(len("")) view_port = ViewPort() screen = pygame.display.set_mode((VIEW_PORT_W, VIEW_PORT_H), 0, 32) controller = UserController() pygame.display.set_caption("精灵类测试") framerate = pygame.time.Clock() resource = Resources() sprite_group = IGroup() # sa=Ghost(screen) # sa.pos=np.random.randint(-100,100, size=(1,2)) # group.add(sa) # sa = Phoenix(screen) sa.pos = np.random.randint(-100, 100, size=(1, 2))
from zhak_projects.agame.map import ViewPort import numpy as np from zhak_projects.agame.zhgame import sprite_shake view_port = ViewPort() view_port.pos = np.array([50, 50]) print("world point", view_port.to_world(np.array([44.3, 50]))) print("world point", view_port.to_world(np.array([44.3, 50]))) view_port.pos = np.array([-10, 50]) print("world point", view_port.to_world(np.array([-44.3, -150]))) print(view_port.pos) sprite_shake(view_port) print(view_port.pos) for i in range(100): print(np.random.randint(-1, 2, size=(1, 2)) * 2)
class ParticalSystem(): def __init__(self, parent=None): self._list = [] self.partical_type = None self.parent = parent self.n_rows = 10 self.destination = np.array([[1, 2]]) self.width = 16 self.height = 16 self.image = None self._switcher = False self.last_time = 0 self.view_port = ViewPort() self.init() def init(self): self.start_point = self.particals = np.random.randint( 4, 9, (self.n_rows, 3)) self.particals[:, 2] = 0 def set_pos(self, destination): self.destination = destination def _start_point(self): return np.array([self.parent.head_offset.x, self.parent.head_offset.y]) def trigger(self): self._switcher = True def intrigger(self): self._switcher = False def _deactivated(self, particals): return particals[:, 2] == 0 def _activated(self, particals): return particals[:, 2] != 0 def update(self, current_time=None, rate=60): if self._switcher == True: particals = self.particals if current_time > self.last_time + rate: deactivated = np.argwhere( self._deactivated(particals)).reshape(1, -1)[0] if deactivated.size != 0: choice_id = np.random.choice(deactivated) # choice_id=np.random.choice(self.n_rows) particals[choice_id][2] = 1 particals[choice_id][:2] = self._start_point() self.last_time = current_time activated = np.argwhere(self._activated(particals)).reshape(1, -1)[0] if activated.size != 0: vector = self.destination - particals[:, :2] # direction=self.destination - self.start_point[:, :2] particals_speed = vector / np.linalg.norm(vector) * 15 self.particals = particals + np.column_stack( (particals_speed, np.zeros(self.n_rows))) self.destination = self.parent.parent.target.pos destination_distance = np.linalg.norm( (self.destination.reshape(-1, 2) - particals[:, :2]), axis=1, keepdims=True) condition = (destination_distance < 4).reshape(self.n_rows) if condition.any(): self.particals[np.where(condition), 2] = 0 # moving_distance=np.linalg.norm(( particals[:, :2])-self._start_point(), axis=1, # keepdims=True) # condition = (moving_distance >300 ).reshape(self.n_rows) # if condition.any(): # self.particals[np.where(condition), 2] = 0 else: activated = np.argwhere(self._activated(self.particals)).reshape( 1, -1)[0] if activated.size != 0: vector = self.destination - self.particals[:, :2] # direction=self.destination - self.start_point[:, :2] particals_speed = vector / np.linalg.norm(vector) * 15 self.particals = self.particals + np.column_stack( (particals_speed, np.zeros(self.n_rows))) # self.destination = self.parent.parent.target.pos destination_distance = np.linalg.norm( (self.destination.reshape(-1, 2) - self.particals[:, :2]), axis=1, keepdims=True) condition = (destination_distance < 4).reshape(self.n_rows) if condition.any(): self.particals[np.where(condition), 2] = 0 def draw(self, screen=None): particals = self.particals particals_show = particals[np.argwhere( (self._activated(particals)))[:, 0]] particals_show = self.view_port.to_screen(particals_show[:, :2]) rect = np.column_stack((particals_show, np.repeat(np.array([[self.width, self.height]]), particals_show.shape[0], axis=0))) surface = self.image all_par = [(surface, tuple(t)) for t in rect.tolist()] # print(all_par) screen.blits(all_par)
import pygame import numpy as np from zhak_projects.agame.elements.Ghost import Ghost, Yaorao, Xiaohua, Tiaotiao, Qie, Kuaile, Phoenix from zhak_projects.agame.elements.Panels import Panel from zhak_projects.agame.elements.Resources import Resources from zhak_projects.agame.elements.SpriteActor import SpriteActor from zhak_projects.agame.map import ViewPort, VIEW_PORT_W, VIEW_PORT_H, Map # from zhak_projects.agame.test_game.test_run_map_bilt import tilesize from zhak_projects.agame.zhgame import UserController, LIFE_STATE_GHOST, is_in_rect pygame.init() print(len("")) view_port = ViewPort() screen = pygame.display.set_mode((VIEW_PORT_W, VIEW_PORT_H), 0, 32) controller = UserController() pygame.display.set_caption("界面类测试") framerate = pygame.time.Clock() resource = Resources() sprite_group = pygame.sprite.Group() sa = Phoenix(screen) sa.pos = np.random.randint(-100, 100, size=(1, 2)) sa.name = "Phoenix" sprite_group.add(sa) sa = Kuaile(screen)
def init_elements_mapEditor(self): self.view_port = ViewPort() self.controller = UserController() self.resource = Resources() self.sprite_group = IGroup() self.map = Map()
class Scene(): map_list = [] current_map = None sprite_group = None screen = None controller = None view_port = None def __init__(self, screen=None, mapEditor=None): if not screen: pygame.init() pygame.display.set_caption("场景测试") screen = pygame.display.set_mode((VIEW_PORT_W, VIEW_PORT_H), 0, 32) self.screen = screen self.map_list = [] self.sprite_group = None self.controller = None self.view_port = None self.map_file = None self.framerate = pygame.time.Clock() if mapEditor: self.init_elements_mapEditor() else: self.init_elements() def init_elements(self): self.view_port = ViewPort() self.controller = UserController() self.resource = Resources() self.sprite_group = IGroup() self.explosion_group = IGroup() self.map = Map() main_sprite = Qie(self) main_sprite.pos = np.random.randint(-100, 100, size=(1, 2)) self.sprite_group.add(main_sprite) main_sprite.attack_range = 2000 insect = Insect(self) insect.pos = np.random.randint(-100, 100, size=(1, 2)) insect.speed = 1 insect.target = main_sprite self.sprite_group.add(insect) exp = BombExplosion(self) exp.pos = np.random.randint(-100, 100, size=(1, 2)) self.explosion_group.add(exp) exp = MissalBullet(self) exp.pos = np.random.randint(-100, 100, size=(1, 2)) exp.speed = 1 exp.target = main_sprite self.sprite_group.add(exp) self.framerate.tick(30) ticks = pygame.time.get_ticks() exp.explode(ticks) self.controller.main_actor = main_sprite def init_elements_mapEditor(self): self.view_port = ViewPort() self.controller = UserController() self.resource = Resources() self.sprite_group = IGroup() self.map = Map() # def init_from_read(self): def event(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos world_click_pos = self.view_port.to_world(np.array([x, y])) self.controller.control_actor(world_click_pos, self.sprite_group) # controller.main_actor.be_attacked=True # self.controller.main_actor.weapon.bullet.destination = world_click_pos if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.view_port.pos += np.array([-20, 0]) elif event.key == pygame.K_RIGHT: self.view_port.pos += np.array([20, 0]) if event.key == pygame.K_UP: self.view_port.pos += np.array([0, -20]) elif event.key == pygame.K_DOWN: self.view_port.pos += np.array([0, 20]) def update(self, ticks): self.sprite_group.update(ticks) self.explosion_group.update(ticks) # self.view_port.update(ticks) self.map.update(self.sprite_group) def draw(self, screen): screen.fill((0, 0, 100)) self.map.draw(screen) self.sprite_group.draw(self.screen) self.explosion_group.draw(self.screen) self.controller.main_actor.weapon.bullet.draw(screen) def run(self): while True: self.framerate.tick(30) ticks = pygame.time.get_ticks() self.event() self.update(ticks) self.draw(self.screen) # screen.fill((0, 0, 100)) # sprite_group.update(ticks) # view_port.update(ticks) # low_map, hight_map = map.update(sprite_group) # # screen.blits(low_map) # sprite_group.draw(screen) # screen.blits(hight_map) pygame.display.update()