commit 84b930a628311afe6dc58c51350d396607a22e51 Author: Bruno Rybársky Date: Sat Oct 30 19:33:11 2021 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b74b6b0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/main.py", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/assets/bum.ogg b/assets/bum.ogg new file mode 100644 index 0000000..af97350 Binary files /dev/null and b/assets/bum.ogg differ diff --git a/assets/button.png b/assets/button.png new file mode 100644 index 0000000..273a5ea Binary files /dev/null and b/assets/button.png differ diff --git a/assets/case.png b/assets/case.png new file mode 100644 index 0000000..a3587f2 Binary files /dev/null and b/assets/case.png differ diff --git a/assets/dynamite.png b/assets/dynamite.png new file mode 100644 index 0000000..fd954bb Binary files /dev/null and b/assets/dynamite.png differ diff --git a/assets/lcd.png b/assets/lcd.png new file mode 100644 index 0000000..bb322bd Binary files /dev/null and b/assets/lcd.png differ diff --git a/assets/music.ogg b/assets/music.ogg new file mode 100644 index 0000000..8ddb74e Binary files /dev/null and b/assets/music.ogg differ diff --git a/assets/room.png b/assets/room.png new file mode 100644 index 0000000..72886e5 Binary files /dev/null and b/assets/room.png differ diff --git a/bomb.py b/bomb.py new file mode 100644 index 0000000..e915765 --- /dev/null +++ b/bomb.py @@ -0,0 +1,68 @@ +from tonegen import ToneGen +from imageobj import Obj +import textwrap +class Bomb: + def __init__(self, pygame, screen): + self.pygame = pygame + self.screen = screen + self.ticktimer = 1 + pygame.mixer.music.load('assets/music.ogg') + pygame.mixer.music.play(-1, 0.0) + self.lcdtext = "" + self.explosion = self.pygame.mixer.Sound('assets/bum.ogg') + self.sprites = [] + self.sprites.append(Obj(pygame.image.load('assets/room.png'))) + self.sprites.append(Obj(pygame.image.load('assets/case.png'))) + self.sprites.append(Obj(pygame.image.load('assets/dynamite.png'), 200, 400)) + xbut = 598 + ybut = 100 + self.sprites.append(Obj(pygame.image.load('assets/lcd.png'), 260, 90)) + button_img = pygame.image.load('assets/button.png') + buttons = [ + [Obj(button_img, xbut, ybut, "1"), Obj(button_img, xbut + 68, ybut, "2"), Obj(button_img, xbut + 136, ybut, "3")], + [Obj(button_img, xbut, ybut + 68, "4"), Obj(button_img, xbut + 68, ybut + 68, "5"), Obj(button_img, xbut + 136, ybut + 68, "6")], + [Obj(button_img, xbut, ybut + 136, "7"), Obj(button_img, xbut + 68, ybut + 136, "8"), Obj(button_img, xbut + 136, ybut + 136, "9")], + [Obj(button_img, xbut, ybut + 204, "X"), Obj(button_img, xbut + 136, ybut + 204, ">")] + ] + for buttonx in buttons: + for button in buttonx: + self.sprites.append(button) + + def getevents(self): + for event in self.pygame.event.get(): # User did something + if event.type == self.pygame.QUIT: # If user clicked close + return False + elif event.type == self.pygame.MOUSEBUTTONUP: + pos = self.pygame.mouse.get_pos() + for sprite in self.sprites: + if sprite.rect.collidepoint(pos): + if sprite.button: + self.buttonpress_handler(sprite.button) + + def buttonpress_handler(self, button): + print(f"The button {button} was pressed.") + if button == "X": + self.lcdtext = "" + elif button == ">": + self.lcdtext = str(len(self.lcdtext)) + else: + self.lcdtext += button + + def beep(self): + if self.ticktimer >= 1000: + self.ticktimer = 1000 - self.ticktimer + ToneGen(self.pygame, 1000, 1000, 0.2) + + def drawstuff(self): + self.screen.fill((0,0,0)) + for sprite in self.sprites: + sprite.draw(self.screen) + myfont = self.pygame.font.SysFont('Liberation Mono, Bold', 16) + lines = textwrap.wrap(self.lcdtext, 23)[-9:] + ybase = 170 + i = 0 + for line in lines: + renderedtext = myfont.render(line, False, (0, 0, 0)) + self.screen.blit(renderedtext, (310, ybase + (i * 16))) + i += 1 + self.pygame.display.flip() \ No newline at end of file diff --git a/imageobj.py b/imageobj.py new file mode 100644 index 0000000..0df0d51 --- /dev/null +++ b/imageobj.py @@ -0,0 +1,15 @@ +import pygame +class Obj(pygame.sprite.Sprite): + def __init__(self, img, x = 0, y = 0, button=""): + pygame.sprite.Sprite.__init__(self) + self.image = img.convert_alpha() + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.y = y + self.button = button + if button: + myfont = pygame.font.SysFont('Liberation Mono, Bold', 16) + textsurface = myfont.render(button, False, (0, 0, 0)) + self.image.blit(textsurface, (18, 16)) + def draw(self, screen): + screen.blit(self.image, self.rect) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..7a40e87 --- /dev/null +++ b/main.py @@ -0,0 +1,25 @@ +import pygame +from bomb import Bomb +pygame.init() +pygame.mixer.pre_init(44100, 16, 2) +pygame.font.init() +size = (824, 674) +screen = pygame.display.set_mode(size) + +pygame.display.set_caption("BOH(Brunova Osobna Hra)") +running = True +clock = pygame.time.Clock() + +bomb = Bomb(pygame, screen) + +while True: + + bomb.drawstuff() + + bomb.ticktimer += clock.tick(60) + bomb.beep() + if bomb.getevents() == False: + break + +#Once we have exited the main program loop we can stop the game engine: +pygame.quit() \ No newline at end of file diff --git a/tonegen.py b/tonegen.py new file mode 100644 index 0000000..5b91699 --- /dev/null +++ b/tonegen.py @@ -0,0 +1,19 @@ +from math import sin, pi +import numpy as np +class ToneGen(): + + def __init__(self, pygame, freq_l, freq_r, duration=1): + + n_samples = int(round(duration*44100)) + #setup our numpy array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above + buf = np.zeros((n_samples, 2), dtype = np.int16) + max_sample = 2**(16 - 1) - 1 + + for s in range(n_samples): + t = float(s)/44100 # time in seconds + + #grab the x-coordinate of the sine wave at a given time, while constraining the sample to what our mixer is set to with "bits" + buf[s][0] = int(round(max_sample*sin(2*pi*freq_l*t))) # left + buf[s][1] = int(round(max_sample*0.5*sin(2*pi*freq_r*t))) # right + pygame.sndarray.make_sound(buf).play() + \ No newline at end of file