-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from cid0rz/games
games test
- Loading branch information
Showing
8 changed files
with
247 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ font_size=13 | |
|
||
[testing] | ||
font_size=14 | ||
font_family="firacode" | ||
tab_size=4 | ||
theme="light" | ||
auto_save=false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters