This commit is contained in:
Bruno Rybársky 2021-10-30 19:33:11 +02:00
commit 84b930a628
13 changed files with 143 additions and 0 deletions

1
.gitignore vendored Normal file

@ -0,0 +1 @@
__pycache__

15
.vscode/launch.json vendored Normal file

@ -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"
}
]
}

BIN
assets/bum.ogg Normal file

Binary file not shown.

BIN
assets/button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

BIN
assets/case.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
assets/dynamite.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
assets/lcd.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
assets/music.ogg Normal file

Binary file not shown.

BIN
assets/room.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 429 KiB

68
bomb.py Normal file

@ -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()

15
imageobj.py Normal file

@ -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)

25
main.py Normal file

@ -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()

19
tonegen.py Normal file

@ -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()