Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by Abbas ( 3 years ago )
import arcade
import math
import random

# معرفی متغیر ها
scr_w = 600
scr_h = 600
scr_tit = 'animiting a rectangle'
rect_w = 50
rect_h = 50
obstacle_w = 50
obstacle_h = 300
obstacle_x = scr_w / 2
obstacle_y = obstacle_h / 2
num = 3


# کلاس اصلی بازی
class Mygame(arcade.Window):
    def __init__(self, w, h, t):
        super().__init__(w, h, t)
        # تعیین بک گراند پنجره
        arcade.set_background_color(arcade.color.WHITE)
        # مشخص کردن مختصات و رنگ و سرعت مربع ها به صورت رندوم
        self.center_x = [random.randint(50, 550) for _ in range(num)]
        self.center_y = [random.randint(50, 550) for _ in range(num)]
        self.change_x = [random.randint(1, 5) for _ in range(num)]
        self.change_y = [random.randint(1, 5) for _ in range(num)]
        self.colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(num)]

    def setup(self):
        pass

    def on_draw(self):
        self.clear()
        for i in range(num):
            # کشیدن 4 مربع
            arcade.draw_rectangle_filled(self.center_x[i], self.center_y[i], rect_w, rect_h, self.colors[i])
        # کشیدن مستطیل مانع
        arcade.draw_rectangle_filled(obstacle_x, obstacle_y, obstacle_w, obstacle_h, (0, 0, 0))

    def on_update(self, delta_time):
        # تغییر جهت حرکت مربع ها در برخورد با کناره های پنجره
        for i in range(num):
            self.center_x[i] += self.change_x[i]
            self.center_y[i] += self.change_y[i]
            if self.center_x[i] > scr_w - rect_w / 2 or self.center_x[i] < rect_w / 2:
                self.change_x[i] *= -1
            if self.center_y[i] > scr_h - rect_h / 2 or self.center_y[i] < rect_h / 2:
                self.change_y[i] *= -1

            # تغییر جهت حرکت مربع ها در برخورد با مستطیل مانع
            if abs(self.center_x[i] - obstacle_x) < (rect_w + obstacle_w) / 2 and abs(self.center_y[i] - obstacle_y) < (
                    rect_h + obstacle_h) / 2:
                self.change_x[i] *= -1
                self.change_y[i] *= -1
            # تشخیص برخورد مربع ها با یکدیگر و تغییر جهت حرکت شان
        for i in range(num):
            for j in range(i + 1, num):
                dist = math.sqrt(
                    (self.center_x[i] - self.center_x[j]) ** 2 + (self.center_y[i] - self.center_y[j]) ** 2)
                if dist < rect_w:
                    self.change_x[i], self.change_x[j] = self.change_x[j], self.change_x[i]
                    self.change_y[i], self.change_y[j] = self.change_y[j], self.change_y[i]

    def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
        if button == arcade.MOUSE_BUTTON_LEFT:
            for i in range(num):
                self.change_x[i] /= 1.5
                self.change_y[i] /= 1.5
        elif button == arcade.MOUSE_BUTTON_RIGHT:
            for i in range(num):
                self.change_x[i] *= 1.5
                self.change_y[i] *= 1.5


def main():
    Mygame(scr_w, scr_h, scr_tit)
    arcade.run()


if __name__ == '__main__':
    main()

 

Revise this Paste

Your Name: Code Language: