Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import os
- import arcade
- import random
- path = "tet/"
- dirs = os.listdir(path)
- rows_num = 25
- columns_num = 15
- scale = 1
- block_size = scale * 20
- SCREEN_WIDTH = block_size * columns_num
- SCREEN_HEIGHT = block_size * rows_num
- rotate_list = [0,-180,-90,90,180]
- blocks_shapes = [
- [[0, 0, 1],
- [1, 1, 1]],
- [[1, 1, 1, 1]],
- [[1, 1],
- [1, 1]]
- ]
- class MyGame(arcade.Window):
- """ Main application class. """
- def __init__(self, width, height, title):
- super().__init__(width, height, title)
- arcade.set_background_color(arcade.color.BLACK)
- self.board = None
- self.frame_count = 0
- self.game_over = False
- self.paused = False
- self.board_sprite_list = None
- self.stone = None
- self.stone_x = 0
- self.stone_y = 0
- def draw_sprites(self):
- for sprite_list in self.blocks_list:
- sprite_list.draw()
- def back_to_previous(self,copy,origin):
- for x, y in copy:
- origin[copy.index((x, y))].center_x = x
- origin[copy.index((x, y))].center_y = y
- def setup(self):
- self.blocks_list = []
- self.block_sprite_list = arcade.SpriteList()
- self.current_block = None
- for i in range(15):
- rand_block = random.randint(0,2)
- block = blocks_shapes[rand_block]
- x_rand = random.randrange(0, SCREEN_WIDTH - block_size * len(block[0]), block_size)
- y_rand = random.randrange(0, SCREEN_HEIGHT - block_size * len(block), block_size)
- for row in range(len(block)):
- for col in range(len(block[0])):
- if block[row][col] == 1:
- temp_sprite = arcade.Sprite(path + dirs[rand_block],scale)
- temp_sprite.left = x_rand + col * block_size
- temp_sprite.bottom = y_rand + row * block_size
- self.block_sprite_list.append(temp_sprite)
- self.current_block = self.block_sprite_list
- self.rotate(random.choice(rotate_list))
- while self.check_collision(self.block_sprite_list,self.blocks_list):
- self.block_sprite_list = arcade.SpriteList()
- x_rand = random.randrange(0, SCREEN_WIDTH - block_size * len(block[0]), block_size)
- y_rand = random.randrange(0, SCREEN_HEIGHT - block_size * len(block), block_size)
- for row in range(len(block)):
- for col in range(len(block[0])):
- if block[row][col] == 1:
- temp_sprite = arcade.Sprite(path + dirs[rand_block],scale)
- temp_sprite.left = x_rand + col * block_size
- temp_sprite.bottom = y_rand + row * block_size
- self.block_sprite_list.append(temp_sprite)
- self.rotate(random.choice(rotate_list))
- self.blocks_list.append(self.block_sprite_list)
- self.block_sprite_list = arcade.SpriteList()
- self.current_block = self.blocks_list[-1]
- self.draw_sprites()
- def on_mouse_press(self, x, y, button, modifiers):
- for sprite_list in self.blocks_list:
- for sprite in sprite_list:
- if sprite.collides_with_point((x,y)):
- self.current_block = sprite_list
- break
- def check_collision(self,current,list):
- is_collision = False
- for sprite_list in list:
- for current_sprite in current:
- if arcade.check_for_collision_with_list(current_sprite, sprite_list):
- is_collision = True
- return is_collision
- def rotate(self,angle):
- prev_point = []
- for sprite in self.current_block:
- old_x = sprite.center_x
- old_y = sprite.center_y
- prev_point.append((old_x,old_y))
- ox = self.current_block[0].center_x
- oy = self.current_block[0].center_y
- deg = math.radians(angle)
- newx = math.cos(deg) * (old_x - ox) - math.sin(deg) * (old_y - oy) + ox
- newy = math.sin(deg) * (old_x - ox) + math.cos(deg) * (old_y - oy) + oy
- sprite.center_x = newx
- sprite.center_y = newy
- if self.check_collision(self.current_block,self.blocks_list):
- self.back_to_previous(prev_point,self.current_block)
- break
- elif sprite.right > SCREEN_WIDTH:
- self.back_to_previous(prev_point, self.current_block)
- break
- elif sprite.left < 0:
- self.back_to_previous(prev_point, self.current_block)
- break
- elif sprite.top < 0:
- self.back_to_previous(prev_point, self.current_block)
- break
- elif sprite.bottom > SCREEN_HEIGHT - block_size:
- self.back_to_previous(prev_point, self.current_block)
- break
- def move_x(self, side):
- self.current_block.move(side*block_size,0)
- for sprite_list in self.blocks_list:
- for current_sprite in self.current_block:
- if arcade.check_for_collision_with_list(current_sprite, sprite_list):
- self.current_block.move(-side*block_size,0)
- elif current_sprite.right > SCREEN_WIDTH:
- self.current_block.move(-side*block_size,0)
- elif current_sprite.left < 0:
- self.current_block.move(-side*block_size,0)
- def move_y(self, side):
- self.current_block.move(0,side*block_size)
- for sprite_list in self.blocks_list:
- for current_sprite in self.current_block:
- if arcade.check_for_collision_with_list(current_sprite, sprite_list):
- self.current_block.move(0,-side*block_size)
- elif current_sprite.top < 0:
- self.current_block.move(0,-side*block_size)
- elif current_sprite.bottom > SCREEN_HEIGHT - block_size:
- self.current_block.move(0,-side*block_size)
- def on_key_press(self, key, modifiers):
- if key == arcade.key.LEFT:
- self.move_x(-1)
- elif key == arcade.key.RIGHT:
- self.move_x(1)
- elif key == arcade.key.UP:
- self.move_y(1)
- elif key == arcade.key.DOWN:
- self.move_y(-1)
- elif key == arcade.key.SPACE:
- self.rotate(90)
- elif key == arcade.key.R:
- print(self.current_block[0].left, self.current_block[0].bottom)
- def on_draw(self):
- pass
- arcade.start_render()
- self.draw_sprites()
- def main():
- """ Create the game window, setup, run """
- my_game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, "title")
- my_game.setup()
- arcade.run()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement