#演習問題@ from tkinter import* import time,random tk = Tk() canvas = Canvas(tk, width=1000, height=600, bg="white") canvas.pack() class Packman: def __init__(self): self.ID = canvas.create_arc(470,270,530,330,start=30,extent=300,fill="yellow") self.x = 500 self.y = 300 def move(self): dx = random.randint(-20, 20) dy = random.randint(-20, 20) canvas.move(self.ID, dx, dy) self.x += dx self.y += dy self.reverse() tk.update() time.sleep(0.05 / num) def reverse(self): if self.x > 1000: self.x += -1000; canvas.move(self.ID, -1000, 0) elif self.x < 0: self.x += 1000; canvas.move(self.ID, 1000, 0) if self.y > 600: self.y += -600; canvas.move(self.ID, 0, -600) elif self.y < 0: self.y += 600; canvas.move(self.ID, 0, 600) circle = [] num = int(input("個数")) for x in range(num): circle.append(Packman()) while True: for pack in circle: pack.move() #演習問題A import random class Card: def __init__(self, suit, number): self.suit = suit self.number = number class Trump: def __init__(self): self.trump = [] mark = ["heart ","spade ","diamonds","clover "] for m in mark: for x in range(1,14): self.trump.append(Card(m, x)) #Cardクラスのオブジェクトを生成し、self.trumpに追加していく def print_Trump(self): print("山札") for t in self.trump: print("[{},{}]".format(t.suit, t.number)) def hand(self): self.hand = [] print("手札") for x in range(5): i = random.choice(self.trump)#山札からランダムに一つ選択 self.hand.append(i)#それを手札に追加 self.trump.remove(i)#それを山札から削除 print("{}:[{},{}]".format(x + 1, i.suit, i.number)) t = Trump() t.hand() t.print_Trump()