Skip to content

Commit

Permalink
Merge pull request #90 from cid0rz/games
Browse files Browse the repository at this point in the history
games test
  • Loading branch information
cid0rz authored Jul 26, 2023
2 parents ba15896 + 8a79bcc commit 4ec0474
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 11 deletions.
1 change: 0 additions & 1 deletion biscuit/config/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ font_size=13

[testing]
font_size=14
font_family="firacode"
tab_size=4
theme="light"
auto_save=false
Expand Down
11 changes: 5 additions & 6 deletions biscuit/core/components/editors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import os
from tkinter.constants import *

from .editor import BaseEditor
from biscuit.core.components.games.whoops import Whoops
from biscuit.core.settings.editor import SettingsEditor

from ..utils import FileType, Frame
from .breadcrumbs import BreadCrumbs

from .diffeditor import DiffEditor
from .editor import BaseEditor
from .imageviewer import ImageViewer
from .texteditor import TextEditor
from .misc import Welcome

from biscuit.core.components.games import Whoops
from biscuit.core.settings.editor import SettingsEditor
from .texteditor import TextEditor

editors = {f"::{i.name}":i for i in (Welcome, SettingsEditor)}

Expand Down
7 changes: 4 additions & 3 deletions biscuit/core/components/editors/texteditor/highlighter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os, tkinter as tk
import os
import tkinter as tk

from pygments import lex
from pygments.lexers import get_lexer_for_filename
from pygments.util import ClassNotFound


class Highlighter:
Expand All @@ -11,7 +12,7 @@ def __init__(self, master, *args, **kwargs):

try:
self.lexer = get_lexer_for_filename(os.path.basename(master.path), inencoding=master.encoding, encoding=master.encoding)
except ClassNotFound:
except:
self.lexer = None

self.tag_colors = self.base.theme.syntax
Expand Down
89 changes: 89 additions & 0 deletions biscuit/core/components/games/StackEngineer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
__author__ = "cid0rz"
version = "0.1"

import random
import tkinter as tk
from collections import Counter, deque
from tkinter import messagebox, ttk

from biscuit.core.components.editors.texteditor import TextEditor

from .game import BaseGame


class Stack(ttk.LabelFrame):
stack_n = 1

def __init__(self, parent, text=f'Stack {stack_n}', borderwidth=4, width=300,
height=200, columns=[("ID", 20, 'center'), ("Value", 50, 'center')],
selectmode='none', values=[], index=0):
super().__init__(parent, text=text, borderwidth=borderwidth, width=width, height=height)

self.twscroll = tk.Scrollbar(self)
self.twscroll.pack(side='right', fill='y')
self.tw = ttk.Treeview(self, columns=[col[0] for col in columns],
yscrollcommand=self.twscroll.set, selectmode=selectmode,
displaycolumns=[col[0] for col in columns])
self.tw.pack()
self.tw.column("#0", width=0, stretch='no')
for col in columns:
self.tw.column(col[0], width=col[1], anchor=col[2])
for col in columns:
self.tw.heading(col[0], text=col[0])

self.twscroll.config(command=self.tw.yview)

self.values = deque(values)
self.index = index

def read(self):
return self.values[self.index]
def write(self, value, index):
self.values[index] = value
self.update()
def insert(self, value, index):
self.values.insert(index, value)
self.update()
def push(self, value):
self.values.appendleft(value)
self.update()
def pop(self):
val = self.values.popleft()
self.update()
return val

def update(self):
self.tw.delete(*self.tw.get_children())
for i,val in enumerate(self.values):
self.tw.insert("", index=i, iid=str(i), values=(i,val))


class StackEngineer(BaseGame):
name = "Stack Engineer"

def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.config(**self.base.theme.editors)
self.m_registers = {'R1' : None}
s1 = Stack(self, values=[5,7])
self.m_stacks = {'S1' : s1}
self.target = []
self.cost = 0
self.score = 0

self.editor = TextEditor(self, exists=False, minimalist=True)
self.draw()

#print(se.m_stacks['S1'].pop())
self.m_stacks['S1'].insert(10, 1)

def draw(self):
for stack in self.m_stacks:
stack = self.m_stacks[stack]
stack.update()
stack.grid(row=0, column=1)
editor = self.editor
editor.grid(row=1)

# text widget functions are available under editor.text
# eg. editor.text.tag_add
5 changes: 4 additions & 1 deletion biscuit/core/components/games/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from ..utils import Frame
from .game import BaseGame
from .gameoflife import GameOfLife
from .minesweeper import Minesweeper
from .pong import Pong
from .snake import Snake
from .stackengineer import StackEngineer
from .tetris import Tetris
from .ttt import TicTacToe
from .whoops import Whoops

games = {i.name:i for i in (Tetris, GameOfLife, Pong, TicTacToe, Snake)}
games = {i.name:i for i in (Tetris, GameOfLife, Pong, TicTacToe, Snake, StackEngineer, Minesweeper)}


def get_games(base):
"helper function to generate actionset items"
Expand Down
78 changes: 78 additions & 0 deletions biscuit/core/components/games/minesweeper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import random
import tkinter as tk
from tkinter import messagebox

from .game import BaseGame

# Game constants
BOARD_SIZE = 10
NUM_MINES = 10


class Minesweeper(BaseGame):
name = "Minesweeper!"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.board = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
self.mine_positions = []
self.buttons = [[None for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

self.generate_mines()
self.create_buttons()

def generate_mines(self):
self.mine_positions = random.sample(range(BOARD_SIZE * BOARD_SIZE), NUM_MINES)

for position in self.mine_positions:
row = position // BOARD_SIZE
col = position % BOARD_SIZE
self.board[row][col] = -1

def create_buttons(self):
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
button = tk.Button(self, width=2, height=1)
button.grid(row=row, column=col)
button.bind("<Button-1>", lambda e, r=row, c=col: self.button_click(e, r, c))
self.buttons[row][col] = button

def button_click(self, event, row, col):
if self.board[row][col] == -1:
self.buttons[row][col].config(text="*", bg="red")
self.game_over()
else:
count = self.count_adjacent_mines(row, col)
self.buttons[row][col].config(text=count, relief=tk.SUNKEN)
self.buttons[row][col].unbind("<Button-1>")
if count == 0:
self.reveal_empty_cells(row, col)

def count_adjacent_mines(self, row, col):
count = 0
for i in range(max(0, row-1), min(row+2, BOARD_SIZE)):
for j in range(max(0, col-1), min(col+2, BOARD_SIZE)):
if self.board[i][j] == -1:
count += 1
return count

def reveal_empty_cells(self, row, col):
for i in range(max(0, row-1), min(row+2, BOARD_SIZE)):
for j in range(max(0, col-1), min(col+2, BOARD_SIZE)):
if self.buttons[i][j]["text"] == "" and self.board[i][j] != -1:
count = self.count_adjacent_mines(i, j)
self.buttons[i][j].config(text=count, relief=tk.SUNKEN)
self.buttons[i][j].unbind("<Button-1>")
if count == 0:
self.reveal_empty_cells(i, j)

def game_over(self):
for position in self.mine_positions:
row = position // BOARD_SIZE
col = position % BOARD_SIZE
if self.buttons[row][col]["text"] != "*":
self.buttons[row][col].config(text="*", relief=tk.SUNKEN)
self.buttons[row][col].unbind("<Button-1>")

messagebox.showinfo("Game Over", "You hit a mine!")
66 changes: 66 additions & 0 deletions biscuit/core/components/games/moonlander.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import tkinter as tk

GRAVITY = 0.1
THRUST_POWER = 0.2

class MoonlanderGame:
def __init__(self):
self.window = tk.Tk()
self.window.title("Moonlander")
self.window.geometry("600x600")

self.canvas = tk.Canvas(self.window, width=600, height=600)
self.canvas.pack()

self.background_img = tk.PhotoImage(file="background.png")
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.background_img)

self.rocketship_img = tk.PhotoImage(file="rocketship.png")
self.rocketship = self.canvas.create_image(300, 100, image=self.rocketship_img)

self.fuel_gauge = self.canvas.create_rectangle(10, 10, 110, 20, fill="green")

self.fuel = 100.0
self.velocity = 0.0
self.position = 100.0
self.game_over = False

self.window.bind("<space>", self.thrust)
self.window.bind("<r>", self.restart_game)

self.update()
self.window.mainloop()

def thrust(self, event):
if self.fuel > 0 and not self.game_over:
self.velocity -= THRUST_POWER
self.fuel -= 1
self.canvas.move(self.fuel_gauge, 1, 0)

def restart_game(self, event):
self.fuel = 100.0
self.velocity = 0.0
self.position = 100.0
self.game_over = False
self.canvas.delete("all")
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.background_img)
self.rocketship = self.canvas.create_image(300, 100, image=self.rocketship_img)
self.fuel_gauge = self.canvas.create_rectangle(10, 10, 110, 20, fill="green")
self.update()

def update(self):
if not self.game_over:
self.velocity += GRAVITY
self.position += self.velocity
self.canvas.move(self.rocketship, 0, self.velocity)

if self.position > 550:
self.game_over = True
self.canvas.create_text(300, 300, text="Crashed!", fill="red", font=("Arial", 30))
elif self.fuel <= 0:
self.game_over = True
self.canvas.create_text(300, 300, text="Out of fuel!", fill="red", font=("Arial", 30))
else:
self.window.after(20, self.update)

game = MoonlanderGame()
1 change: 1 addition & 0 deletions biscuit/core/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def gen_actionset(self):
)

def setup_font(self):

self.iconfont = extra.Font(file=self.res.get_res_path("codicon.ttf"), family="codicon")
self.font = tk.font.Font(
family=self.config.font[0],
Expand Down

0 comments on commit 4ec0474

Please sign in to comment.