Skip to content

Commit

Permalink
Welcome screen added
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulShagri committed Aug 6, 2021
1 parent 8e6f83c commit 68fb446
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 100 deletions.
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"statistics_window": dpg.generate_uuid(),
},
"displays": {
"enter_level": dpg.generate_uuid(),
"level_text": dpg.generate_uuid(),
"full_line_text": dpg.generate_uuid(),
"score_text": dpg.generate_uuid(),
Expand All @@ -25,6 +26,11 @@
},
"registries": {
"texture_registry": dpg.generate_uuid(),
"key_release_handler": dpg.generate_uuid(),
"mouse_release_handler": dpg.generate_uuid(),
},
"buttons": {
"play_button": dpg.generate_uuid(),
},
"block_texture": {
"I_block": dpg.generate_uuid(),
Expand Down
200 changes: 119 additions & 81 deletions tetris_game.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import config
from theme_settings import *
from config import *
import tetrominos_handler

dpg.setup_registries() # Registries for mouse and keyboard press events

dpg.setup_viewport()
dpg.set_viewport_title("Tetris Game")
dpg.configure_viewport(0, x_pos=0, y_pos=0, width=1000, height=735)
Expand All @@ -12,109 +11,148 @@
dpg.set_viewport_min_height(735)
dpg.set_viewport_min_width(100)

with dpg.window(pos=[0, 0], autosize=True, no_collapse=True, no_resize=True, no_close=True, no_move=True,
no_title_bar=True) as main_window:

with dpg.group(horizontal=True):
with dpg.child(width=320, id=item_id["windows"]["score_window"]):
dpg.add_dummy(height=10)
def set_main_window():
config.level = dpg.get_value(item=item_id["displays"]["enter_level"])

dpg.add_text(default_value=" Your level : ")
dpg.add_same_line()
dpg.add_text(default_value="0", id=item_id["displays"]["level_text"])
with dpg.window(pos=[0, 0], autosize=True, no_collapse=True, no_resize=True, no_close=True, no_move=True,
no_title_bar=True, id=item_id["windows"]["main_window"]):

dpg.add_dummy()
dpg.add_text(default_value=" Full lines : ")
dpg.add_same_line()
dpg.add_text(default_value="0", id=item_id["displays"]["full_line_text"])
with dpg.group(horizontal=True):
with dpg.child(width=320, id=item_id["windows"]["score_window"]):
dpg.add_dummy(height=10)

dpg.add_dummy(height=10)
dpg.add_text(default_value=" SCORE : ")
dpg.add_same_line()
dpg.add_text(default_value="0", color=(161, 94, 33), id=item_id["displays"]["score_text"])
dpg.add_text(default_value=" Your level : ")
dpg.add_same_line()
dpg.add_text(default_value=config.level, id=item_id["displays"]["level_text"])

dpg.add_dummy(height=50)
help_text = dpg.add_button(label="H E L P", width=-1)
dpg.set_item_theme(item=help_text, theme=dummy_button_theme)
dpg.add_dummy()
dpg.add_text(default_value=" Full lines : ")
dpg.add_same_line()
dpg.add_text(default_value="0", id=item_id["displays"]["full_line_text"])

dpg.add_dummy(height=20)
dpg.add_text(default_value=" LEFT KEY : Left")
dpg.add_text(default_value=" RIGHT KEY : Right")
dpg.add_text(default_value=" UP KEY : Rotate")
dpg.add_text(default_value=" DOWN KEY : Speed up")
dpg.add_text(default_value=" SPACE : Drop")
dpg.add_dummy(height=10)
dpg.add_text(default_value=" SCORE : ")
dpg.add_same_line()
dpg.add_text(default_value="0", color=(161, 94, 33), id=item_id["displays"]["score_text"])

dpg.add_dummy(height=50)
next_text = dpg.add_button(label="Next :", width=-1)
dpg.set_item_theme(item=next_text, theme=dummy_button_theme)
dpg.add_dummy(height=50)
help_text = dpg.add_button(label="H E L P", width=-1)
dpg.set_item_theme(item=help_text, theme=dummy_button_theme)

with dpg.plot(no_menus=False, no_title=True, no_box_select=True, no_mouse_pos=True, width=315,
height=160, equal_aspects=True, id=item_id["windows"]["next_block_board"]):
dpg.add_dummy(height=20)
dpg.add_text(default_value=" LEFT KEY : Left")
dpg.add_text(default_value=" RIGHT KEY : Right")
dpg.add_text(default_value=" UP KEY : Rotate")
dpg.add_text(default_value=" DOWN KEY : Speed up")
dpg.add_text(default_value=" SPACE : Drop")

dpg.set_item_theme(item=item_id["windows"]["next_block_board"], theme=no_border_board_theme)
dpg.add_dummy(height=50)
next_text = dpg.add_button(label="Next :", width=-1)
dpg.set_item_theme(item=next_text, theme=dummy_button_theme)

x = dpg.add_plot_axis(axis=0, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
y = dpg.add_plot_axis(axis=1, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
with dpg.plot(no_menus=False, no_title=True, no_box_select=True, no_mouse_pos=True, width=315,
height=160, equal_aspects=True, id=item_id["windows"]["next_block_board"]):

dpg.set_axis_limits(axis=x, ymin=0, ymax=8)
dpg.set_axis_limits(axis=y, ymin=0, ymax=4)
dpg.set_item_theme(item=item_id["windows"]["next_block_board"], theme=no_border_board_theme)

with dpg.group():
with dpg.plot(no_menus=False, no_title=True, no_box_select=True, no_mouse_pos=True, width=325,
height=650, equal_aspects=True, id=item_id["windows"]["tetris_board"]):
default_x = dpg.add_plot_axis(axis=0, no_gridlines=False, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
default_y = dpg.add_plot_axis(axis=1, no_gridlines=False, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
x = dpg.add_plot_axis(axis=0, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
y = dpg.add_plot_axis(axis=1, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)

dpg.set_axis_limits(axis=x, ymin=0, ymax=8)
dpg.set_axis_limits(axis=y, ymin=0, ymax=4)

with dpg.group():
with dpg.plot(no_menus=False, no_title=True, no_box_select=True, no_mouse_pos=True, width=325,
height=650, equal_aspects=True, id=item_id["windows"]["tetris_board"]):
default_x = dpg.add_plot_axis(axis=0, no_gridlines=False, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
default_y = dpg.add_plot_axis(axis=1, no_gridlines=False, no_tick_marks=True, no_tick_labels=True,
lock_min=True)

dpg.set_axis_limits(axis=default_x, ymin=0, ymax=10)
dpg.set_axis_limits(axis=default_y, ymin=0, ymax=20)

dpg.add_vline_series(x=[n for n in range(10)], parent=default_x)
dpg.add_hline_series(x=[n for n in range(120)], parent=default_y)

dpg.add_button(label="Play TETRIS !", width=325, callback=tetrominos_handler.create_blocksDispatcher,
id=item_id["buttons"]["play_button"])
dpg.set_item_font(item=item_id["buttons"]["play_button"], font=play_font)
dpg.set_item_theme(item=item_id["buttons"]["play_button"], theme=play_button_theme)

dpg.set_axis_limits(axis=default_x, ymin=0, ymax=10)
dpg.set_axis_limits(axis=default_y, ymin=0, ymax=20)
with dpg.child(autosize_x=True):
dpg.add_dummy(height=10)

dpg.add_vline_series(x=[n for n in range(10)], parent=default_x)
dpg.add_hline_series(x=[n for n in range(120)], parent=default_y)
statistics_text = dpg.add_button(label="STATISTICS", width=-1)
dpg.set_item_theme(item=statistics_text, theme=dummy_button_theme)

play_button = dpg.add_button(label="Play TETRIS !", width=325,
callback=tetrominos_handler.create_blocksDispatcher)
dpg.set_item_font(item=play_button, font=play_font)
dpg.set_item_theme(item=play_button, theme=play_button_theme)
with dpg.plot(no_menus=False, no_title=True, no_box_select=True, no_mouse_pos=True, width=315,
height=560, equal_aspects=True, id=item_id["windows"]["statistics_window"]):

with dpg.child(autosize_x=True):
dpg.add_dummy(height=10)
dpg.set_item_theme(item=item_id["windows"]["statistics_window"], theme=no_border_board_theme)

statistics_text = dpg.add_button(label="STATISTICS", width=-1)
dpg.set_item_theme(item=statistics_text, theme=dummy_button_theme)
x = dpg.add_plot_axis(axis=0, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
y = dpg.add_plot_axis(axis=1, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)

with dpg.plot(no_menus=False, no_title=True, no_box_select=True, no_mouse_pos=True, width=315,
height=560, equal_aspects=True, id=item_id["windows"]["statistics_window"]):
dpg.set_axis_limits(axis=x, ymin=0, ymax=10)
dpg.set_axis_limits(axis=y, ymin=0, ymax=19)

dpg.set_item_theme(item=item_id["windows"]["statistics_window"], theme=no_border_board_theme)
tetrominos_handler.draw_statistics_LBlock()
tetrominos_handler.draw_statistics_IBlock()
tetrominos_handler.draw_statistics_TBlock()
tetrominos_handler.draw_statistics_ZBlock()
tetrominos_handler.draw_statistics_SBlock()
tetrominos_handler.draw_statistics_OBlock()
tetrominos_handler.draw_statistics_JBlock()

x = dpg.add_plot_axis(axis=0, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
y = dpg.add_plot_axis(axis=1, no_gridlines=True, no_tick_marks=True, no_tick_labels=True,
lock_min=True)
dashed_line_text = dpg.add_button(label="-------------------", width=-1)
dpg.add_text(default_value=" Total")
dpg.add_same_line(spacing=170)
dpg.add_text(default_value=0, id=item_id["displays"]["Total_block_stat"])
dpg.set_item_theme(item=dashed_line_text, theme=dummy_button_theme)

dpg.set_axis_limits(axis=x, ymin=0, ymax=10)
dpg.set_axis_limits(axis=y, ymin=0, ymax=19)
dpg.delete_item(item=enter_level_screen)
dpg.set_primary_window(window=item_id["windows"]["main_window"], value=True)

tetrominos_handler.draw_statistics_LBlock()
tetrominos_handler.draw_statistics_IBlock()
tetrominos_handler.draw_statistics_TBlock()
tetrominos_handler.draw_statistics_ZBlock()
tetrominos_handler.draw_statistics_SBlock()
tetrominos_handler.draw_statistics_OBlock()
tetrominos_handler.draw_statistics_JBlock()

dashed_line_text = dpg.add_button(label="-------------------", width=-1)
dpg.add_text(default_value=" Total")
dpg.add_same_line(spacing=170)
dpg.add_text(default_value=0, id=item_id["displays"]["Total_block_stat"])
dpg.set_item_theme(item=dashed_line_text, theme=dummy_button_theme)
def press_any_key_to_start():
dpg.delete_item(item=item_id["registries"]["key_release_handler"])
dpg.delete_item(item=item_id["registries"]["mouse_release_handler"])
dpg.delete_item(item=welcome_screen)
dpg.configure_item(item=enter_level_screen, show=True, modal=True)
dpg.set_primary_window(window=enter_level_screen, value=True)


with dpg.window(modal=True, autosize=True, no_collapse=True, no_resize=True, no_close=True, no_move=True,
no_title_bar=True) as welcome_screen:
width, height, channels, data = dpg.load_image("textures/welcome_screen.jpg")

welcome_screen_image = dpg.add_static_texture(width, height, data, parent=item_id["registries"]["texture_registry"])
dpg.add_image(texture_id=welcome_screen_image, width=984, height=688)

dpg.add_key_release_handler(callback=press_any_key_to_start, id=item_id["registries"]["key_release_handler"])
dpg.add_mouse_release_handler(callback=press_any_key_to_start, id=item_id["registries"]["mouse_release_handler"])

with dpg.window(autosize=True, no_collapse=True, no_resize=True, no_close=True, no_move=True,
no_title_bar=True,show=False) as enter_level_screen:
dpg.add_dummy(height=300)

with dpg.group(horizontal=True):
dpg.add_child(width=250)

with dpg.group():
dpg.add_text(default_value="Enter your level (0-9) > ")
dpg.add_same_line()
dpg.add_input_int(label="", step=0, min_value=0, max_value=9, width=30, id=item_id["displays"]["enter_level"])

continue_button = dpg.add_button(label="Continue", callback=set_main_window)
dpg.set_item_theme(item=continue_button, theme=play_button_theme)

dpg.add_key_press_handler(callback=tetrominos_handler.key_release_handler)

dpg.set_primary_window(window=main_window, value=True)
dpg.set_primary_window(window=welcome_screen, value=True)
dpg.start_dearpygui()
29 changes: 10 additions & 19 deletions tetrominos_handler/tetrominosAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import random
import pandas as pd
import config
from theme_settings import *
from config import *
import tetrominos_handler

dpg.setup_registries() # Registries for mouse and keyboard press events

# Load and add all block textures
with dpg.texture_registry(id=item_id["registries"]["texture_registry"]):
# Start a texture registry. Textures will be added later
Expand Down Expand Up @@ -71,26 +74,12 @@ def rotate_block(cells: int, rotation_point: int):
pmax=[config.cells_occupied[-1 - n][0] + 1, config.cells_occupied[-1 - n][1]])


# def start_game():
# # Function initiates the main game
# config.level = int(dpg.get_value(item=item_id["displays"]["level_text"]))
#
# block_speeds_data = pd.read_csv("block_speeds_data.csv")
#
# while config.level < 30:
# config.speed = (block_speeds_data.values[config.level][1])/20
#
# create_blocksDispatcher()
#
# print(f"level {config.level} over")
# time.sleep(2)
# dpg.delete_item(item=item_id["windows"]["tetris_board"], children_only=True)
# dpg.delete_item(item=item_id["windows"]["next_block_board"], children_only=True)
# config.level += 1


def create_blocksDispatcher():
# Function creates a new thread that controls the continuous movement of the new blocks
dpg.add_key_press_handler(callback=tetrominos_handler.key_release_handler)
dpg.configure_item(item=item_id["buttons"]["play_button"], enabled=False)
dpg.set_item_disabled_theme(item=item_id["buttons"]["play_button"], theme=play_button_theme)

create_blocks_thread = threading.Thread(name="create blocks", target=create_blocks, args=(), daemon=True)
create_blocks_thread.start()

Expand Down Expand Up @@ -131,7 +120,9 @@ def create_blocks():
time.sleep(config.speed)
temp_block.move_blockDispatcher()

print("Game over!")
dpg.draw_rectangle(pmin=[0,0], pmax=[10, 20], color=[0, 0, 0, 150], thickness=0,
fill=[0, 0, 0, 150], parent=item_id["windows"]["tetris_board"])
dpg.draw_text(pos=[0.5, 11], text="GAME OVER", size=1, parent=item_id["windows"]["tetris_board"])


def check_complete_line():
Expand Down
2 changes: 2 additions & 0 deletions theme_settings/theme_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

# Colors
dpg.add_theme_color(dpg.mvThemeCol_WindowBg, (0, 0, 0), category=dpg.mvThemeCat_Core)
dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (0, 0, 0), category=dpg.mvThemeCat_Core)
dpg.add_theme_color(dpg.mvThemeCol_PopupBg, (0, 0, 0), category=dpg.mvThemeCat_Core)
dpg.add_theme_color(dpg.mvThemeCol_ChildBg, (0, 0, 0), category=dpg.mvThemeCat_Core)
dpg.add_theme_color(dpg.mvThemeCol_MenuBarBg, (48, 48, 48), category=dpg.mvThemeCat_Core)
dpg.add_theme_color(dpg.mvThemeCol_Text, (168, 168, 168), category=dpg.mvThemeCat_Core)
Expand Down

0 comments on commit 68fb446

Please sign in to comment.