Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.96 KB | None | 0 0
  1. import math
  2. import os
  3.  
  4. import arcade
  5. import random
  6.  
  7. path = "tet/"
  8.  
  9. dirs = os.listdir(path)
  10.  
  11. rows_num = 25
  12. columns_num = 15
  13.  
  14. scale = 1
  15. block_size = scale * 20
  16.  
  17. SCREEN_WIDTH = block_size * columns_num
  18. SCREEN_HEIGHT = block_size * rows_num
  19.  
  20. rotate_list = [0,-180,-90,90,180]
  21.  
  22. blocks_shapes = [
  23.  
  24.     [[0, 0, 1],
  25.      [1, 1, 1]],
  26.  
  27.     [[1, 1, 1, 1]],
  28.  
  29.     [[1, 1],
  30.      [1, 1]]
  31. ]
  32.  
  33.  
  34. class MyGame(arcade.Window):
  35.     """ Main application class. """
  36.  
  37.     def __init__(self, width, height, title):
  38.  
  39.         super().__init__(width, height, title)
  40.  
  41.         arcade.set_background_color(arcade.color.BLACK)
  42.  
  43.         self.board = None
  44.         self.frame_count = 0
  45.         self.game_over = False
  46.         self.paused = False
  47.         self.board_sprite_list = None
  48.  
  49.         self.stone = None
  50.         self.stone_x = 0
  51.         self.stone_y = 0
  52.  
  53.     def draw_sprites(self):
  54.         for sprite_list in self.blocks_list:
  55.             sprite_list.draw()
  56.  
  57.     def back_to_previous(self,copy,origin):
  58.         for x, y in copy:
  59.             origin[copy.index((x, y))].center_x = x
  60.             origin[copy.index((x, y))].center_y = y
  61.  
  62.     def setup(self):
  63.         self.blocks_list = []
  64.         self.block_sprite_list = arcade.SpriteList()
  65.         self.current_block = None
  66.  
  67.         for i in range(15):
  68.             rand_block = random.randint(0,2)
  69.             block = blocks_shapes[rand_block]
  70.  
  71.             x_rand = random.randrange(0, SCREEN_WIDTH - block_size * len(block[0]), block_size)
  72.             y_rand = random.randrange(0, SCREEN_HEIGHT - block_size * len(block), block_size)
  73.             for row in range(len(block)):
  74.                 for col in range(len(block[0])):
  75.                     if block[row][col] == 1:
  76.                         temp_sprite = arcade.Sprite(path + dirs[rand_block],scale)
  77.  
  78.                         temp_sprite.left = x_rand + col * block_size
  79.                         temp_sprite.bottom = y_rand + row * block_size
  80.                         self.block_sprite_list.append(temp_sprite)
  81.  
  82.             self.current_block = self.block_sprite_list
  83.  
  84.             self.rotate(random.choice(rotate_list))
  85.  
  86.             while self.check_collision(self.block_sprite_list,self.blocks_list):
  87.                 self.block_sprite_list = arcade.SpriteList()
  88.                 x_rand = random.randrange(0, SCREEN_WIDTH - block_size * len(block[0]), block_size)
  89.                 y_rand = random.randrange(0, SCREEN_HEIGHT - block_size * len(block), block_size)
  90.                 for row in range(len(block)):
  91.                     for col in range(len(block[0])):
  92.                         if block[row][col] == 1:
  93.                             temp_sprite = arcade.Sprite(path + dirs[rand_block],scale)
  94.  
  95.                             temp_sprite.left = x_rand + col * block_size
  96.                             temp_sprite.bottom = y_rand + row * block_size
  97.                             self.block_sprite_list.append(temp_sprite)
  98.                 self.rotate(random.choice(rotate_list))
  99.  
  100.             self.blocks_list.append(self.block_sprite_list)
  101.             self.block_sprite_list = arcade.SpriteList()
  102.  
  103.         self.current_block = self.blocks_list[-1]
  104.  
  105.         self.draw_sprites()
  106.  
  107.     def on_mouse_press(self, x, y, button, modifiers):
  108.         for sprite_list in self.blocks_list:
  109.             for sprite in sprite_list:
  110.                 if sprite.collides_with_point((x,y)):
  111.                     self.current_block = sprite_list
  112.                     break
  113.  
  114.     def check_collision(self,current,list):
  115.         is_collision = False
  116.         for sprite_list in list:
  117.             for current_sprite in current:
  118.                 if arcade.check_for_collision_with_list(current_sprite, sprite_list):
  119.                     is_collision = True
  120.         return is_collision
  121.  
  122.     def rotate(self,angle):
  123.         prev_point = []
  124.         for sprite in self.current_block:
  125.             old_x = sprite.center_x
  126.             old_y = sprite.center_y
  127.  
  128.             prev_point.append((old_x,old_y))
  129.  
  130.             ox = self.current_block[0].center_x
  131.             oy = self.current_block[0].center_y
  132.  
  133.             deg = math.radians(angle)
  134.             newx = math.cos(deg) * (old_x - ox) - math.sin(deg) * (old_y - oy) + ox
  135.             newy = math.sin(deg) * (old_x - ox) + math.cos(deg) * (old_y - oy) + oy
  136.  
  137.             sprite.center_x = newx
  138.             sprite.center_y = newy
  139.  
  140.             if self.check_collision(self.current_block,self.blocks_list):
  141.                 self.back_to_previous(prev_point,self.current_block)
  142.                 break
  143.             elif sprite.right > SCREEN_WIDTH:
  144.                 self.back_to_previous(prev_point, self.current_block)
  145.                 break
  146.             elif sprite.left < 0:
  147.                 self.back_to_previous(prev_point, self.current_block)
  148.                 break
  149.             elif sprite.top < 0:
  150.                 self.back_to_previous(prev_point, self.current_block)
  151.                 break
  152.             elif sprite.bottom > SCREEN_HEIGHT - block_size:
  153.                 self.back_to_previous(prev_point, self.current_block)
  154.                 break
  155.  
  156.     def move_x(self, side):
  157.         self.current_block.move(side*block_size,0)
  158.         for sprite_list in self.blocks_list:
  159.             for current_sprite in self.current_block:
  160.                 if arcade.check_for_collision_with_list(current_sprite, sprite_list):
  161.                     self.current_block.move(-side*block_size,0)
  162.                 elif current_sprite.right > SCREEN_WIDTH:
  163.                      self.current_block.move(-side*block_size,0)
  164.                 elif current_sprite.left < 0:
  165.                      self.current_block.move(-side*block_size,0)
  166.  
  167.     def move_y(self, side):
  168.         self.current_block.move(0,side*block_size)
  169.         for sprite_list in self.blocks_list:
  170.             for current_sprite in self.current_block:
  171.                 if arcade.check_for_collision_with_list(current_sprite, sprite_list):
  172.                     self.current_block.move(0,-side*block_size)
  173.                 elif current_sprite.top < 0:
  174.                     self.current_block.move(0,-side*block_size)
  175.                 elif current_sprite.bottom > SCREEN_HEIGHT - block_size:
  176.                     self.current_block.move(0,-side*block_size)
  177.  
  178.     def on_key_press(self, key, modifiers):
  179.  
  180.         if key == arcade.key.LEFT:
  181.             self.move_x(-1)
  182.         elif key == arcade.key.RIGHT:
  183.             self.move_x(1)
  184.         elif key == arcade.key.UP:
  185.             self.move_y(1)
  186.         elif key == arcade.key.DOWN:
  187.             self.move_y(-1)
  188.         elif key == arcade.key.SPACE:
  189.             self.rotate(90)
  190.         elif key == arcade.key.R:
  191.             print(self.current_block[0].left, self.current_block[0].bottom)
  192.  
  193.     def on_draw(self):
  194.         pass
  195.         arcade.start_render()
  196.         self.draw_sprites()
  197.  
  198.  
  199. def main():
  200.     """ Create the game window, setup, run """
  201.     my_game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, "title")
  202.     my_game.setup()
  203.     arcade.run()
  204.  
  205.  
  206. if __name__ == "__main__":
  207.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement