def start_screen(): # Выполняется до начала игры # создание кнопок menu_buttons = [ Sprite(general.load_image('fon.jpg'), Transform((0, 0))), Sprite(general.load_image('title.png'), Transform((560, 80))), Button(general.load_image('btn_start.png'), Transform((710, 500)), start_game, general.buttons_group), Button(general.load_image('btn_exit.png'), Transform((760, 700)), terminate, general.buttons_group) ] while is_menu: for event in pygame.event.get(): if event.type == pygame.QUIT: terminate() elif event.type == pygame.MOUSEBUTTONDOWN: general.buttons_group.update(event.pos) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: terminate() general.all_sprites.update() general.all_sprites.draw(general.screen) pygame.display.flip() general.clock.tick(general.FPS) # удаление кнопок for btn in menu_buttons: btn.kill()
def _init_buttons(self): bg_width = self.background.get_width() button_c = Button(BTNWIDTH, BTNHEIGHT, (bg_width + CLOSEX, CLOSEY), CLOSELBL, Keys.Exit.value, COLORKEY, LINECOLOR) button_q = Button(BTNWIDTH, BTNHEIGHT, (bg_width + PREVX, PREVY), PREVLBL, Keys.Prev.value, COLORKEY, LINECOLOR) button_w = Button(BTNWIDTH, BTNHEIGHT, (bg_width + NEXTX, NEXTY), NEXTLBL, Keys.Next.value, COLORKEY, LINECOLOR) self.buttons = (button_c, button_q, button_w)
def settings_screen(self): self.settings_screen_run = True self.screen.fill(LIGHTGREY) self.draw_text_mid("Настройки", 64, WHITE, WIDTH // 2, 20) # кнопки self.btn_sound_on_off = Button(self, self.str_sound_on_off, 36, BUTTON_SIZE, WHITE, LIGHTGREEN, LIGHTBLUE, WIDTH // 2, HEIGHT // 3) self.btn_music_on_off = Button(self, self.str_music_on_off, 36, BUTTON_SIZE, WHITE, LIGHTGREEN, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 100) self.btn_reset = Button(self, "Сброс данных", 40, BUTTON_SIZE, WHITE, RED, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 200) self.btn_menu = Button(self, "Назад", 48, (int(BUTTON_SIZE[0] // 1.5), BUTTON_SIZE[1]), WHITE, LIGHTBLUE, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 300) buttons = [ self.btn_sound_on_off, self.btn_music_on_off, self.btn_reset, self.btn_menu ] while self.settings_screen_run: button_pressed = self.wait_for_press(buttons, 'setting') if button_pressed == self.btn_sound_on_off: self.sound_on_off = True if not self.sound_on_off else False self.str_sound_on_off = "Включить звуки" if not self.sound_on_off else "Выключить звуки" if not self.sound_on_off: self.change_volume([0, 0, 0, 0, 0], self.music_volume) else: self.change_volume(SOUND_VOLUME, self.music_volume) self.settings_screen() elif button_pressed == self.btn_music_on_off: self.music_on_off = True if not self.music_on_off else False self.str_music_on_off = "Включить музыку" if not self.music_on_off else "Выключить музыку" if not self.music_on_off: self.change_volume(self.sound_volume, 0) else: self.change_volume(self.sound_volume, MUSIC_VOLUME) self.settings_screen() elif button_pressed == self.btn_reset: self.highscore = 0 with open(HIGHSCORE, 'w') as file: file.write(str(0)) elif button_pressed == self.btn_menu: self.settings_screen_run = False self.menu_screen() if self.running: pg.display.flip()
def gameover_screen(self): self.gameover_screen_run = True self.play_music('gameover') self.screen.fill(LIGHTGREY) self.draw_text_mid("GAME OVER", 82, RED, WIDTH // 2, 20) self.draw_text_mid("Очков: " + str(self.score), 24, WHITE, WIDTH // 2, 120) self.btn_play_again = Button(self, "Играть заново", 48, BUTTON_SIZE, WHITE, LIGHTGREEN, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 50) self.btn_menu = Button(self, "Меню", 48, BUTTON_SIZE, WHITE, LIGHTBLUE, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 150) self.btn_exit_go = Button(self, "Выйти из игры", 48, BUTTON_SIZE, WHITE, RED, LIGHTGREEN, WIDTH // 2, HEIGHT // 3 + 250) if self.score > self.highscore: self.highscore = self.score self.draw_text_mid('Ты побил рекорд, поздравляю!', 30, LIGHTBLUE, WIDTH // 2, 160) with open(HIGHSCORE, 'w') as file: file.write(str(self.highscore)) else: self.draw_text_mid("Рекорд: " + str(self.highscore), 36, LIGHTBLUE, WIDTH // 2, 160) buttons = [self.btn_play_again, self.btn_menu, self.btn_exit_go] while self.gameover_screen_run: button_pressed = self.wait_for_press(buttons, 'gameover') if button_pressed == self.btn_play_again: self.gameover_screen_run = False pg.mixer.music.fadeout(FADE_OUT) self.new_game() elif button_pressed == self.btn_menu: self.gameover_screen_run = False pg.mixer.music.fadeout(FADE_OUT) self.start() elif button_pressed == self.btn_exit_go: self.gameover_screen_run = False self.running = False pg.mixer.music.stop() pg.quit() if self.running: pg.display.flip()
def menu_screen(self): self.menu_screen_run = True self.screen.fill(LIGHTGREY) self.draw_text_mid("Stickman Jump", 64, WHITE, WIDTH // 2, 20) self.draw_text_mid("Рекорд: " + str(self.highscore), 24, WHITE, WIDTH // 2, 100) self.draw_text_mid("v.1.0", 24, WHITE, 40, HEIGHT - 40) # кнопки self.btn_play = Button(self, "Играть", 48, BUTTON_SIZE, WHITE, LIGHTGREEN, LIGHTBLUE, WIDTH // 2, HEIGHT // 3) self.btn_settings = Button(self, "Настройки", 48, BUTTON_SIZE, WHITE, LIGHTBLUE, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 100) self.btn_info = Button(self, "Информация", 48, BUTTON_SIZE, WHITE, LIGHTBLUE, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 200) self.btn_exit_menu = Button(self, "Выйти из игры", 48, BUTTON_SIZE, WHITE, RED, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 300) buttons = [ self.btn_play, self.btn_settings, self.btn_exit_menu, self.btn_info ] while self.menu_screen_run: button_pressed = self.wait_for_press(buttons, 'menu') if button_pressed == self.btn_play: self.menu_screen_run = False pg.mixer.music.fadeout(FADE_OUT) self.new_game() elif button_pressed == self.btn_settings: self.menu_screen_run = False self.settings_screen() elif button_pressed == self.btn_exit_menu: self.menu_screen_run = False self.running = False pg.quit() elif button_pressed == self.btn_info: self.menu_screen_run = False self.info_screen() if self.running: pg.display.flip()
def loop(): button_r = Button(17) while True: matrix.display("?", 1, 0.2) if not button_r.status(): break else: gesture = get_gesture() print(gesture) if gesture in ("thumb_up"): play() elif gesture in ("thumb_down"): print("end") break exit()
def info_screen(self): self.info_screen_run = True self.screen.fill(LIGHTGREY) # отрисовка полевых объектов pg.draw.rect(self.screen, RED, (WIDTH // 4 - 80, 230 + 6, 100, 20), 3) pg.draw.rect(self.screen, GREEN, (WIDTH // 4 - 80, 260 + 6, 100, 20), 3) pg.draw.rect(self.screen, BLUE, (WIDTH // 4 - 80, 290 + 6, 100, 20), 3) gfxdraw.aacircle(self.screen, WIDTH // 4 - 20, 390 + 18, 12, YELLOW) gfxdraw.filled_circle(self.screen, WIDTH // 4 - 20, 390 + 18, 12, YELLOW) gfxdraw.aacircle(self.screen, WIDTH // 4 - 20, 420 + 18, 12, VIOLET) gfxdraw.filled_circle(self.screen, WIDTH // 4 - 20, 420 + 18, 12, VIOLET) self.screen.blit( pg.transform.scale(load_image('enemy/enemy.png'), (50, 75)), (155, 500)) self.draw_text_mid("Управление", 50, WHITE, WIDTH // 2, 10) self.draw_text_mid("–> - вправо", 28, WHITE, WIDTH // 2, 70) self.draw_text_mid("<– - влево", 28, WHITE, WIDTH // 2, 100) self.draw_text_mid("Space - прыжок", 28, WHITE, WIDTH // 2, 130) self.draw_text_mid("Платформы", 50, WHITE, WIDTH // 2, 170) self.draw_text("– усиливают прыжок", 28, WHITE, WIDTH // 2 - 80, 230) self.draw_text("– двигаются", 28, WHITE, WIDTH // 2 - 80, 260) self.draw_text("– стоят на месте", 28, WHITE, WIDTH // 2 - 80, 290) self.draw_text_mid("Бонусы", 50, WHITE, WIDTH // 2, 330) self.draw_text("– ускоритель", 28, WHITE, WIDTH // 3 - 30, 390) self.draw_text("– слабая сила тяжести", 28, WHITE, WIDTH // 3 - 30, 420) self.draw_text_mid("Враги", 50, WHITE, WIDTH // 2, 460) self.draw_text("– клякса", 28, WHITE, WIDTH // 2 - 30, 520) self.draw_text_mid("© 2020, Yxngxr1, Георгий Дерганов", 25, (150, 150, 150), WIDTH // 2, HEIGHT - 40) # кнопки self.btn_menu = Button(self, "Назад", 48, (int(BUTTON_SIZE[0] // 1.5), BUTTON_SIZE[1]), WHITE, LIGHTBLUE, LIGHTBLUE, WIDTH // 2, HEIGHT // 3 + 370 - 20) buttons = [self.btn_menu] while self.info_screen_run: button_pressed = self.wait_for_press(buttons, 'info') if button_pressed == self.btn_menu: self.info_screen_run = False self.menu_screen() if self.running: pg.display.flip()
def _init_buttons(self): """ Maak de knoppen aan en zet ze in een lijst. Plaats ook de bijbehorende keys in een lijst. """ bg_width = self.background.get_width() bg_height = self.background.get_height() # todo, afhankelijk van situatie, buttons niet weergeven # button_view = sprites.Button((bg_width-200, bg_height-300), "V", pygame.K_v) button_act = Button(BTN_W, BTN_H, (bg_width + ACTX, bg_height + ACTY), ACTLBL, Keys.Action.value) button_inv = Button(BTN_W, BTN_H, (bg_width + INVX, bg_height + INVY), INVLBL, Keys.Inv.value) button_up = Button(BTN_W, BTN_H, (bg_width + UPX, bg_height + UPY), UPLBL, Keys.Up.value) button_down = Button(BTN_W, BTN_H, (bg_width + DOWNX, bg_height + DOWNY), DOWNLBL, Keys.Down.value) button_left = Button(BTN_W, BTN_H, (bg_width + LEFTX, bg_height + LEFTY), LEFTLBL, Keys.Left.value) button_right = Button(BTN_W, BTN_H, (bg_width + RIGHTX, bg_height + RIGHTY), RIGHTLBL, Keys.Right.value) # button_cancel = sprites.Button((bg_width-100, bg_height-200), "C", pygame.K_c) # self.buttons = [button_view, button_up, button_down, button_left, button_right, button_cancel] self.buttons = [ button_act, button_inv, button_up, button_down, button_left, button_right ]
def __init__(self, position, width, height, index, hero): self.surface = pygame.Surface((width, height)) self.surface.set_colorkey(COLORKEY) self.surface = self.surface.convert() self.rect = self.surface.get_rect() self.rect.topleft = position self.largefont = pygame.font.SysFont(FONT, LARGEFONTSIZE) self.normalfont = pygame.font.SysFont(FONT, NORMALFONTSIZE) self.party_index = index self.hero = hero self.face = pygame.image.load(self.hero.FAC).convert_alpha() self.name = self.largefont.render(self.hero.NAM, True, FONTCOLOR).convert_alpha() self.leave = Button(LEAVEW, LEAVEH, (self.rect.right + LEAVEX, self.rect.top + LEAVEY), LEAVELBL, True, HEROCOLOR, LINECOLOR) self.level = None self.hitpoints = None self.full_hp = None self.curr_hp = None self.color = None
class HeroBox(object): """ Alle weergegeven informatie in een hero boxje in het partyscherm. """ def __init__(self, position, width, height, index, hero): self.surface = pygame.Surface((width, height)) self.surface.set_colorkey(COLORKEY) self.surface = self.surface.convert() self.rect = self.surface.get_rect() self.rect.topleft = position self.largefont = pygame.font.SysFont(FONT, LARGEFONTSIZE) self.normalfont = pygame.font.SysFont(FONT, NORMALFONTSIZE) self.party_index = index self.hero = hero self.face = pygame.image.load(self.hero.FAC).convert_alpha() self.name = self.largefont.render(self.hero.NAM, True, FONTCOLOR).convert_alpha() self.leave = Button(LEAVEW, LEAVEH, (self.rect.right + LEAVEX, self.rect.top + LEAVEY), LEAVELBL, True, HEROCOLOR, LINECOLOR) self.level = None self.hitpoints = None self.full_hp = None self.curr_hp = None self.color = None def mouse_click(self, event, cur_hc): """ Ontvang mouse event. Kijk of het met de de surface collide. :param event: pygame.MOUSEBUTTONDOWN uit party display.py :param cur_hc: het huidige party hero nummer :return: het volgorde nummer van de hero van de party, of gewoon het oude huidige nummer, :return: ook de key van het leave knopje, wanneer die gedrukt is, anders is dat None. """ leave_press = self.leave.single_click(event) if self.rect.collidepoint(event.pos): return self.party_index, leave_press return cur_hc, leave_press def update(self, cur_hc): """ Update eerst alle data. :param cur_hc: het huidige party hero nummer uit display """ # alagos moet zo'n knop niet hebben, en hij heeft index 0. if cur_hc == self.party_index: if self.party_index: self.leave.visible = True else: self.leave.visible = False else: self.leave.visible = False self.level = self.normalfont.render(LEVEL.format(self.hero.lev.qty), True, FONTCOLOR).convert_alpha() self.hitpoints = self.normalfont.render( HITPOINTS.format(self.hero.cur_hp, self.hero.max_hp), True, FONTCOLOR).convert_alpha() # health bars # self.full_hp = HEALTHBARWIDTH self.curr_hp = (self.full_hp / self.hero.max_hp) * self.hero.cur_hp self.color = HPCOLORFULL if self.hero.lev.cur < self.hero.lev.qty: self.color = HPCOLORHIGH if self.hero.sta.cur < self.hero.sta.qty: self.color = HPCOLORNORM if self.hero.edu.cur < self.hero.edu.qty: self.color = HPCOLORCRIT if self.hero.edu.cur < self.hero.edu.qty and self.hero.sta.cur > 0: self.color = HPCOLORLOW # ----------- # def render(self, screen, cur_hc): """ En teken dan al die data op de surface en die op de screen. Als het de geselecteerde hero_box is pas dan eerst de achtergrondkleur aan. :param screen: self.screen van partyscreen :param cur_hc: het huidige party hero nummer uit display """ if cur_hc == self.party_index: self.surface.fill(HEROCOLOR) else: self.surface.fill(COLORKEY) pygame.draw.rect(self.surface, LINECOLOR, self.surface.get_rect(), 1) self.surface.blit(self.face, (FACEX, FACEY)) self.surface.blit(self.name, (NAMEX, NAMEY)) self.surface.blit(self.level, (LEVELX, LEVELY)) self.surface.blit(self.hitpoints, (HITPOINTSX, HITPOINTSY)) pygame.draw.rect(self.surface, self.color, (HEALTHBARX, HEALTHBARY, self.curr_hp, HEALTHBARHEIGHT), 0) pygame.draw.rect(self.surface, LINECOLOR, (HEALTHBARX, HEALTHBARY, self.full_hp, HEALTHBARHEIGHT), 1) screen.blit(self.surface, self.rect.topleft) self.leave.render(screen, LINECOLOR)
from machine import Pin import time from machine import I2C, UART #from bme280 import BME280 import dfplayer from components import Light, Player, Button, MQTT_client, Sensor import uasyncio as asyncio mqtt = MQTT_client('frida') bme = Sensor(mqtt=mqtt, topic='sensor') led = Light(13, mqtt=mqtt, topic='lights', id=1) light_switch1 = Button(Pin(12, pull=Pin.PULL_UP)) light_switch1.release_func(led.toggle) light_switch2 = Button(Pin(27, pull=Pin.PULL_UP)) light_switch2.release_func(led.toggle) uart = UART(2, 9600, tx=17, rx=16) uart.init(9600, bits=8, parity=None, stop=1) player = Player(uart=uart, busy_pin=Pin(15), volume=0.5, mqtt=mqtt, topic='audio') door_bell = Button(Pin(14, pull=Pin.PULL_UP)) door_bell.press_func(player.ring)