def __reset_player(self, x_pos=0, y_pos=0): if not x_pos or not y_pos: self.player = Player((self.width - 30) / 2, (self.height - 30) / 2, 0, 0) else: self.player.x = x_pos self.player.y = y_pos
def __init__(self): self.player = Player() self.network = Network() self.network.send(self.player) print(f"Connected") self.screen = Screen() self.data_ready = False self.running = True self.data = [[], [], []] self.wall_to_send = None self.bullet_to_send = None
def __init__(self, width, height): pygame.init() self.width = width self.height = height self.__set_screen_up() self.ladybugs = [] self.player = Player((self.width - 30) / 2, (self.height - 30) / 2, 0, 0) self.FPS = config.fps self.clock = pygame.time.Clock() self.highscores = File(config.highscores_filename) self.mixer = Music('Objects/music/MoonlightSonata.mp3')
def buildObject(ascii_object, x_position, y_position): if ascii_object == 'P': return Player(x_position, y_position) if ascii_object == 'X': return Wall(x_position, y_position) if ascii_object == 'T': return Turret(x_position, y_position) if ascii_object == 'D': return Teleporter(x_position, y_position) if ascii_object == 'S': return LittleRobot(x_position, y_position) else: return None
class Game: def __init__(self): self.player = Player() self.network = Network() self.network.send(self.player) print(f"Connected") self.screen = Screen() self.data_ready = False self.running = True self.data = [[], [], []] self.wall_to_send = None self.bullet_to_send = None def run_game(self): clock = time.Clock() threading.Thread(target=self.data_transfer_thread, args=()).start() while self.running: while not self.data_ready: pass data_copy = self.data.copy() self.data_ready = False self.screen.update(self.player.position, data_copy[0], data_copy[1], data_copy[2]) for ev in event.get(): if ev.type == KEYDOWN: if ev.key == K_ESCAPE: self.running = False self.network.send("Disconnect") quit() if ev.key == K_w or ev.key == K_s or ev.key == K_a or ev.key == K_d: self.player.move_direction[ev.key] = True if ev.key == K_SPACE: x, y = mouse.get_pos() x_position = x - self.screen.width / 2 y_position = y - self.screen.height / 2 wall = self.player.set_wall(x_position, y_position) if wall is not None: self.wall_to_send = wall if ev.type == KEYUP: if ev.key == K_w or ev.key == K_s or ev.key == K_a or ev.key == K_d: self.player.move_direction[ev.key] = False if ev.type == MOUSEBUTTONDOWN: if ev.button == 1: x_position = ev.pos[0] - self.screen.width / 2 y_position = ev.pos[1] - self.screen.height / 2 bullet = self.player.shoot(x_position, y_position) if bullet is not None: self.bullet_to_send = bullet self.player.move() clock.tick(60) def data_transfer_thread(self): while True: while self.data_ready: pass if self.bullet_to_send is not None: self.network.send(self.bullet_to_send) self.bullet_to_send = None if self.wall_to_send is not None: self.network.send(self.wall_to_send) self.wall_to_send = None if self.running: self.network.send(self.player.position) self.data = self.network.receive() else: break if self.data[0] == "Lost": self.running = False quit() break self.data_ready = True
class Game(object): def __init__(self, width, height): pygame.init() self.width = width self.height = height self.__set_screen_up() self.ladybugs = [] self.player = Player((self.width - 30) / 2, (self.height - 30) / 2, 0, 0) self.FPS = config.fps self.clock = pygame.time.Clock() self.highscores = File(config.highscores_filename) self.mixer = Music('Objects/music/MoonlightSonata.mp3') def run(self): run = True while run: menu = self.Menu(self.width, self.height, self.__screen, self.mixer) self.mixer.play_from(-1, 360) run = menu.run() if run == 1: self.mixer.play_from(-1, 1) menu.open_highscores() elif run == 2: self.mixer.play_from(-1, 486) start = Caption("WCIŚNIJ SPACJĘ ABY ROZPOCZĄĆ", 30, config.colors.get("White")) start.x = (self.width - start.text.get_width()) / 2 start.y = int(self.height * 0.7) self.__screen_update([start]) game = self.__get_space() while game: run = True collision = False self.player.score = 0 start.y = int(self.height * 0.7) self.__set_difficulty(config.difficulty) self.__reset_player() while run and not collision: self.__screen_update() self.clock.tick(self.FPS) self.player.move(-self.player.x_speed / 10, -self.player.y_speed / 10) collision = self.__update_enemies_movements() self.player.score += config.difficulty pygame.event.get() self.__handle_keys(pygame.key.get_pressed()) if collision: result = Caption( "Twój wynik: " + str(self.player.score), 50) result.x = int( (self.width - result.text.get_width()) / 2) result.y = int( (self.height - result.text.get_height()) / 2) start.y = result.y + result.text.get_height() + 10 self.__remove_ladybugs() self.__reset_player( (self.width - self.player.width) / 2, int(self.height / 3)) self.__screen_update([result, start]) self.__update_highscores() game = self.__get_space() pygame.quit() def __set_difficulty(self, num_of_ladybugs=config.default_level): for i in range(num_of_ladybugs): x_pos = random.choice((random.randint(0, self.width / 2 - 60), random.randint(self.width / 2 + 30, self.width - 30))) y_pos = random.choice((random.randint(0, self.height / 2 - 60), random.randint(self.height / 2 + 30, self.height - 30))) self.ladybugs.append( Ladybug(x_pos, y_pos, random.randint(1, 5) / 1000, random.randint(1, 5) / 1000)) def __screen_update(self, added_captions=None): if added_captions is None: added_captions = [] self.__screen.fill((0, 0, 0)) self.__screen.blit(self.player.img, (self.player.x, self.player.y)) for ladybug in self.ladybugs: self.__screen.blit(ladybug.img, (ladybug.x, ladybug.y)) self.caption = Caption(str(self.player.score), 20) self.__screen.blit(self.caption.text, (10, 10)) if len(added_captions) > 0: for caption in added_captions: self.__screen.blit(caption.text, (caption.x, caption.y)) pygame.display.update() def __update_enemies_movements(self): for ladybug in self.ladybugs: if self.player.collides_with(ladybug): return True x_dir = random.choice((-1, 1)) y_dir = random.choice((-1, 1)) ladybug.move(x_dir * random.randint(1, 5) / 25, y_dir * random.randint(1, 5) / 25) return False def __handle_keys(self, keys): if keys[pygame.K_w] or keys[pygame.K_UP]: self.player.move(0, -config.player_movespeed) if keys[pygame.K_s] or keys[pygame.K_DOWN]: self.player.move(0, config.player_movespeed) if keys[pygame.K_a] or keys[pygame.K_LEFT]: self.player.move(-config.player_movespeed, 0) if keys[pygame.K_d] or keys[pygame.K_RIGHT]: self.player.move(config.player_movespeed, 0) def __remove_ladybugs(self): self.ladybugs.clear() def __reset_player(self, x_pos=0, y_pos=0): if not x_pos or not y_pos: self.player = Player((self.width - 30) / 2, (self.height - 30) / 2, 0, 0) else: self.player.x = x_pos self.player.y = y_pos def __get_space(self, space=True): while space: for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: space = False if event.key == pygame.K_ESCAPE: return False return True def __update_highscores(self): self.highscores.open() i = len(self.highscores.data) - 1 if self.player.score <= self.highscores.data[i].score: return self.highscores.data[i] = Score(self.player.name, self.player.score) while i > 0 and self.highscores.data[i] > self.highscores.data[i - 1]: temp = self.highscores.data[i] self.highscores.data[i] = self.highscores.data[i - 1] i -= 1 self.highscores.data[i] = temp self.highscores.save() def __set_default_highscores(self): self.highscores.open() default_points = 100000 highscores = [] for i in range(10): highscores.append(Score(config.player_name, default_points)) default_points -= 10000 self.highscores.data = highscores self.highscores.save() def __set_screen_up(self): pygame.init() pygame.display.set_icon(icon) pygame.display.set_caption('Ladybug v1.1') self.__screen = pygame.display.set_mode((self.width, self.height)) class Menu: ############# MENU HANDLER ################# def __init__(self, width, height, screen, mixer): self.__screen = screen self.logo = logo self.buttons = Buttons( [[ Caption("QUIT", 30, config.colors.get("Grey")), Caption("HIGHSCORES", 30, config.colors.get("Grey")), Caption("START", 30) ], [ Caption("PLAYER NAME: ", 30, config.colors.get("Grey")), Caption(config.player_name, 30, config.colors.get("Grey")) ], [ Caption("DIFFICULTY: ", 30, config.colors.get("Grey")), Caption(str(config.difficulty), 30, config.colors.get("Grey")) ], [Caption("MUSIC: ON", 30, config.colors.get("Grey"))]]) self.width = width self.height = height self.selection = Caption("QUIT") self.mixer = mixer def run(self): run = True while run: self.selection = self.buttons.handle_events() if self.selection is not None: if self.selection == Caption("QUIT"): return 0 elif self.selection == Caption("HIGHSCORES"): return 1 elif self.selection == Caption("START"): return 2 elif self.selection == Caption( "PLAYER NAME: " ) or self.selection == self.buttons.buttons[1][1]: self.selection = self.buttons.buttons[1][1] self.buttons.row = 1 self.buttons.col = 1 self.__get_playername() # let the user type in the name elif self.selection == Caption( "DIFFICULTY: " ) or self.selection == self.buttons.buttons[2][1]: self.selection = self.buttons.buttons[2][1] self.buttons.row = 2 self.buttons.col = 1 self.__get_difficulty() # let the user change difficulty elif self.selection == Caption("MUSIC: ON") or Caption( "MUSIC: OFF"): if self.mixer.is_on(): self.buttons.buttons[3][0] = Caption( "MUSIC: OFF", 30) else: self.buttons.buttons[3][0] = Caption( "MUSIC: ON", 30) self.mixer.change_mode() self.update() return self.selection def update(self): self.__screen.fill((0, 0, 0)) self.__screen.blit(self.logo, ((self.width - self.logo.get_width()) / 2, self.height - self.logo.get_height() - 50)) self.buttons.update(self.__screen) pygame.display.update() def open_highscores(self): run = True highscores = File(config.highscores_filename) highscores.open() title = Caption("HIGHSCORE TABLE", 30) x_pos = int((self.width - title.get_width()) / 2) while run: y_pos = 40 self.__screen.fill((0, 0, 0)) self.__screen.blit(title.text, (x_pos, y_pos)) y_pos += title.get_height() for score in highscores.data: player = (Caption(score.player_name, 20), Caption(": ", 20), Caption(score.score, 20)) y_pos += int((player[0].get_height() / 2) * 3) for caption in player: self.__screen.blit(caption.text, (x_pos, y_pos)) x_pos += caption.get_width() x_pos = int((self.width - title.get_width()) / 2) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return False pygame.display.update() def __get_playername(self): player_name = "" run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: if len(player_name): player_name = player_name[:-1] elif event.key == pygame.K_ESCAPE: player_name = config.player_name self.buttons.buttons[1][1] = Caption( player_name, 30) run = False elif event.key == pygame.K_RETURN: config.player_name = player_name run = False elif len(player_name) < 16: player_name += event.unicode self.buttons.buttons[1][1] = Caption(player_name, 30) self.update() self.selection = self.buttons.buttons[1][0] self.buttons.row = 1 self.buttons.col = 0 def __get_difficulty(self): difficulty = config.difficulty run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: if difficulty < config.max_level: difficulty += 1 elif event.key == pygame.K_s or event.key == pygame.K_DOWN: if difficulty > config.min_level: difficulty -= 1 elif event.key == pygame.K_ESCAPE: difficulty = config.difficulty self.buttons.buttons[2][1] = Caption( config.difficulty, 30) run = False elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: config.difficulty = difficulty run = False self.buttons.buttons[2][1] = Caption(difficulty, 30) self.update() self.selection = self.buttons.buttons[2][0] self.buttons.row = 2 self.buttons.col = 0
def __init__(self, screen, screen_size, mapFile = ""): _player = Player(self, (0, 0, -5)) Scene.__init__(self, screen, screen_size, mapFile, _player) self.InstanciateMap()
root = path.dirname(path.abspath(__file__)) gm.init() gv = GameVars() display = gm.display.set_mode(gv.wSize, gv.flags) clock = gm.time.Clock() gameFont = gm.font.Font(root+"//Assets//Fonts//font.otf", getSize(0.02, 0)[0]) p1 = Player( [gv.centro[0], gv.centro[1], 0, 0], gv.wSize[0] * 0.014 ) p1.setSprite(root+"//Assets//Obj//pl.png") p1.setSize(getSize(0.043, 0.13)) players = [ Player( [gv.centro[0], gv.centro[1], 0, 0], gv.wSize[0] * 0.014 ) ] players[0].setSprite(root+"//Assets//Obj//pl2.png") players[0].setSize(getSize(0.043, 0.13))
# Create a screen # Open a window on the screen screen_width = 800 screen_height = 600 screen = pygame.display.set_mode([screen_width, screen_height]) #Background background_img = pygame.image.load("images/background.png") # Title and icon pygame.display.set_caption("Space Invadors") icon = pygame.image.load("images/ufo.png") pygame.display.set_icon(icon) # Player player = Player(pygame, "images/space-invaders.png", 370, 480) # Enemy num_of_enemies = 6 enemy_position_x = list(range(num_of_enemies)) enemy_position_y = list(range(num_of_enemies)) enemies = list(range(num_of_enemies)) for i in range(num_of_enemies): enemy_position_x[i] = random.randint(0, 735) enemy_position_y[i] = random.randint(50, 150) enemies[i] = Enemy(pygame, "images/enemy.png", enemy_position_x[i], enemy_position_y[i]) #Bullets bullet = Bullets(pygame, "images/bullet.png", 0, 480)