import pygame
import random
# تنظیمات اولیه
pygame.font.init()
# اندازه صفحه
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 600
BLOCK_SIZE = 30
COLS = 10
ROWS = 20
# رنگها
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
SHAPES = [
[[1, 1, 1, 1]], # I
[[1, 1, 1], [0, 1, 0]], # T
[[1, 1], [1, 1]], # O
[[1, 1, 0], [0, 1, 1]], # S
[[0, 1, 1], [1, 1, 0]], # Z
[[1, 0, 0], [1, 1, 1]], # L
[[0, 0, 1], [1, 1, 1]], # J
]
SHAPES_COLORS = [CYAN, MAGENTA, YELLOW, GREEN, RED, BLUE, ORANGE]
# کلاس برای اشکال تتریس
class Piece(object):
def init(self, shape, color):
self.shape = shape
self.color = color
self.x = 3
self.y = 0
def rotate(self):
self.shape = [list(row) for row in zip(*self.shape[::-1])]
# کلاس برای صفحه بازی
class Tetris(object):
def init(self):
self.board = [[BLACK for _ in range(COLS)] for _ in range(ROWS)]
self.gameover = False
self.score = 0
self.current_piece = self.new_piece()
self.next_piece = self.new_piece()
def new_piece(self):
shape = random.choice(SHAPES)
color = SHAPES_COLORS[SHAPES.index(shape)]
return Piece(shape, color)
def valid_space(self, piece):
for y, row in enumerate(piece.shape):
for x, cell in enumerate(row):
if cell:
if x + piece.x < 0 or x + piece.x >= COLS or y + piece.y >= ROWS or self.board[y + piece.y][x + piece.x] != BLACK:
return False
return True
def place_piece(self):
for y, row in enumerate(self.current_piece.shape):
for x, cell in enumerate(row):
if cell:
self.board[y + self.current_piece.y][x + self.current_piece.x] = self.current_piece.color
def clear_lines(self):
lines_to_clear = []
for i, row in enumerate(self.board):
if BLACK not in row:
lines_to_clear.append(i)
if lines_to_clear:
self.score += len(lines_to_clear)
for i in lines_to_clear:
del self.board[i]
self.board.insert(0, [BLACK for _ in range(COLS)])
def draw_board(self, win):
for y, row in enumerate(self.board):
for x, color in enumerate(row):
pygame.draw.rect(win, color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
for y, row in enumerate(self.current_piece.shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(win, self.current_piece.color, ((self.current_piece.x + x) * BLOCK_SIZE, (self.current_piece.y + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
def move(self, dx, dy):
self.current_piece.x += dx
self.current_piece.y += dy
if not self.valid_space(self.current_piece):
self.current_piece.x -= dx
self.current_piece.y -= dy
def drop(self):
self.current_piece.y += 1
if not self.valid_space(self.current_piece):
self.current_piece.y -= 1
self.place_piece()
self.clear_lines()
if self.current_piece.y == 0:
self.gameover = True
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
def rotate(self):
self.current_piece.rotate()
if not self.valid_space(self.current_piece):
self.current_piece.rotate()
self.current_piece.rotate()
self.current_piece.rotate()
# تابع برای راهاندازی بازی
def draw_text_middle(text, size, color, surface):
font = pygame.font.Font(pygame.font.get_default_font(), size)
label = font.render(text, 1, color)
surface.blit(label, (SCREEN_WIDTH / 2 - (label.get_width() / 2), SCREEN_HEIGHT / 2 - (label.get_height() / 2)))
# تنظیمات صفحه نمایش
pygame.display.set_caption("Tetris")
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# راهاندازی بازی
def main():
clock = pygame.time.Clock()
game = Tetris()
while True:
win.fill(BLACK)
game.draw_board(win)
if game.gameover:
draw_text_middle("Game Over", 60, WHITE, win)
draw_text_middle(f"Score: {game.score}", 30, WHITE, win)
pygame.display.update()
pygame.time.delay(2000)
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
game.move(-1, 0)
if keys[pygame.K_RIGHT]:
game.move(1, 0)
if keys[pygame.K_DOWN]:
game.drop()
if keys[pygame.K_UP]:
game.rotate()
game.drop()
pygame.display.update()
clock.tick(10)
if __name__ == "__main__":
main()
ساخت کامل بازی تتریس با پایگیم😍😍😍
👍1
پروژه چی باشه؟
Anonymous Poll
0%
بررسی اول بودن عدد
28%
ماشین حساب گرافیکی
20%
گالری عکس
16%
مدیریت اطلاعات کاربران
36%
بازی گل یا پوچ
چنل پایتون | جنگو | برنامه نویسی وب سایت | HTML & CSS & JS
https://t.iss.one/+nJ-jD3kWkjpmMmM0
کسانی که میخوان همکاری کنن برای گل یا پوچ
توی این گروه عضو شن
توی این گروه عضو شن
بازی دوز
import tkinter as tk
from tkinter import messagebox
class TicTacToe:
def __init__(self, root):
self.root = root
self.root.title("Tic-Tac-Toe")
self.current_player = "X"
self.board = [" " for _ in range(9)]
self.buttons = []
for i in range(9):
row, col = divmod(i, 3)
button = tk.Button(root, text=" ", font=("Helvetica", 24), width=5, height=2,
command=lambda i=i: self.on_click(i))
button.grid(row=row, column=col)
self.buttons.append(button)
def on_click(self, index):
if self.board[index] == " ":
self.board[index] = self.current_player
self.buttons[index].config(text=self.current_player)
if self.check_winner():
messagebox.showinfo("Game Over", f"Player {self.current_player} wins!")
self.reset_game()
elif " " not in self.board:
messagebox.showinfo("Game Over", "It's a draw!")
self.reset_game()
else:
self.current_player = "X" if self.current_player == "O" else "O"
def check_winner(self):
win_patterns = [(0, 1, 2), (3, 4, 5), (6, 7, 8),
(0, 3, 6), (1, 4, 7), (2, 5, 8),
(0, 4, 8), (2, 4, 6)]
for pattern in win_patterns:
if self.board[pattern[0]] == self.board[pattern[1]] == self.board[pattern[2]] != " ":
return True
return False
def reset_game(self):
self.board = [" " for _ in range(9)]
for button in self.buttons:
button.config(text=" ")
self.current_player = "X"
if __name__ == "__main__":
root = tk.Tk()
game = TicTacToe(root)
root.mainloop()
❤4
توسعه مدرن جاوا اسکریپت اغلب با وظایفی در ارتباط با عملیات غیرهمزمان و مدیریت خطا مواجه است. معمولاً از ساختارهای try-catch و async-await برای این مقاصد استفاده میشود. با این حال، این روشها ممکن است کد را سنگین کرده و خوانایی آن را کاهش دهند. برای رفع این مشکل، یک اپراتور جدید به نام ?= پیشنهاد شده است که بهطور قابلتوجهی مدیریت خطا را سادهتر کرده و خوانایی کد را بهبود میبخشد.
https://t.iss.one/programming_languages390
https://t.iss.one/programming_languages390
👍2❤1❤🔥1
شما چه زبانی کار میکنید؟🤔
Anonymous Poll
64%
Python 🐍
16%
C/C++/C# 🖱️
2%
PHP 🐘
18%
JavaScript, HTML, CSS 💻
0%
Java😃
import os
from time import sleep
print("Hello world!")
os.system("cls")
sleep(2)
CLS در پایتون
❤5
چنل پایتون | جنگو | برنامه نویسی وب سایت | HTML & CSS & JS
import os from time import sleep print("Hello world!") os.system("cls") sleep(2) CLS در پایتون
❗️توجه داشته باشید که این کد در گوشی اجرا نمیشود و فقط برای ویندوز هست ‼️