انجمنهای فارسی اوبونتو
کمک و پشتیبانی => برنامهسازی => نویسنده: HSN6 در 26 تیر 1392، 09:31 بظ
-
این چیزی که دارم مینویسم یکی از اولین کدهای من با پایتونه. ;D
چیزی که قراره بشه :
یک تابلو (مثل تابلوهای تبلیغاتی) که بشه باهاش انیمیشن ساخت. خطزمان (TimeLine) در پایین و خود خونهها هم بالا.
چیزی که تا الآن شده:
هر خونه روشن و خاموش میشه ؛ در خطزمان هم به صورت RadioButton میشه بین فریمها جابهجا شد.
لطفا تا یه جایی کد رو برسونید که هر فریم بتونه خونههای روشن و خاموش خودشو تشخیص بده و بشه پشت سرهم فریمها رو نشون داد.......
import pygame
pygame.init()
size = (800, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Tablo")
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
blue = ( 0, 0, 255)
done = False
first_nps = True
first_kh = True
ni = 0
nj = 0
nps = 0
R = {}
F = 0
FC = {}
clock = pygame.time.Clock()
while done == False:
#Event processing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
p = pygame.mouse.get_pos()
x = p[0]
y = p[1]
ms = pygame.mouse.get_pressed()
#Event processing
#Draw Table
for x_offest in range(0, 850, 50):
pygame.draw.line(screen, blue, (x_offest, 0), (x_offest, 400), 5)
for y_offest in range(0, 650, 50):
pygame.draw.line(screen, blue, (0 , y_offest), (800 , y_offest), 5)
pygame.draw.line(screen, black, (0 , 0 + 500), (800 , 500), 5)
#Draw Table
#on/off LEDs
if ms == (1,0,0):
for i in range(0, 800, 50):
for j in range(0, 400,50):
if x > i and x < i + 50 and y > j and y < j + 50:
ni = i / 50
nis = str(ni)
nj = j / 50
njs = str(nj)
nps = nis + njs
if R.has_key(nps) == 0:
pygame.draw.circle(screen, red, (i + 25, j+ 25), 20, 20)
R[nps] = 1
pygame.time.delay(200)
else:
pygame.draw.circle(screen, black, (i + 25, j + 25), 20, 20)
del R[nps]
pygame.time.delay(200)
pygame.display.flip()
#on/off LEDs
#Draw Timeline
for x_offest in range(0, 800, 40):
pygame.draw.line(screen, white, (x_offest, 450), (x_offest, 550), 4)
#Draw Timeline
if ms == (1,0,0) and x > x_offest and x < x_offest + 40 and y > 450 and y < 550:
fi = x_offest / 40
F = fi + 1
FC[F] = R
print FC
#Frames radio button
for gclearer in range(0, 800, 40):
pygame.draw.circle(screen, black, (gclearer + 20, 500), 15 , 4)
pygame.draw.circle(screen, green, (x_offest + 20, 500), 15 , 4)
#Frames radio button
pygame.time.delay(50)
pygame.display.flip()
pygame.display.flip()
clock.tick(25)
پیشاپش از همهی کسانی که به من شلیل کمک میکنند و همینطور کسانی که اگه میتونستند کمک میکردند ، تشکر میکنیم (به این دلیل این فعل آخری رو جمع اوردم که ممکنه دوستانی که کمک میکنند زیاد بشن و یک نفر برای تشکر کردن کم باشه ;D )
-
سلام
ببخشید به خاطر این پست شبه اسپم!!!
پیشاپش از همهی کسانی که به من علیل و ذلیل کمک میکنند و همینطور کسانی که اگه میتونستند کمک میکردند ، تشکر میکنیم
دوست من حتی برای شوخی هم این واژه جذاب نیست!
علیل و ذلیل دیگه چیه؟
شما اگه بخوای خودت هم میتونی کاملش کنی ممکنه وقت بیشتری ازت بگیره...اما این دلیل نمیشه شما ناتوان باشی...
اگر واقعا از پیدا کردن پاسخ خسته شدی میتونی خیلی راحت سوالتو طرح کنی و مثل همیشه دوستان کمک می کنن اگر هم جواب ندادن فوقش تو انجمن های انگلیسی میپرسی...
-
باتشکر از همهی دوستانی که کمک کردند. ???
در خود انجمن پایتون مطرح کردم و یکنفر زحمت کشید و کد رو برام نوشت. 8)
فکر کنم باید بیشتر از کلاسها و توابع استفاده کنم.
import sys,os
import pygame as pg
SCREEN_SIZE = (805, 600)
CELL_SIZE = (50,50)
TIMELINE_CELL_SIZE = (40,100)
BLACK = (0,0,0)
WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
class Control(object):
def __init__(self):
os.environ["SDL_VIDEO_CENTERED"] = '1'
pg.init()
pg.display.set_caption("Tablo")
self.screen = pg.display.set_mode(SCREEN_SIZE)
self.done = False
self.clock = pg.time.Clock()
self.fps = 60
self.grid_image = self.make_grid()
self.grid_rect = pg.Rect(0,0,800,400)
self.timeline_rect = pg.Rect(0,450,800,100)
self.frames = [None for i in xrange(20)]
self.current_frame = 0
self.frames[self.current_frame] = Frame()
self.animate = False
self.animate_frame = 0
self.animate_frames = []
self.animate_fps = 5.0
self.timer = 0.0
def make_grid(self):
image = pg.Surface(SCREEN_SIZE).convert()
for i in xrange(0,SCREEN_SIZE[0]+50,50):
image.fill(BLUE,(i,0,5,400))
for j in xrange(0,SCREEN_SIZE[1]+50,50):
image.fill(BLUE,(0,j,SCREEN_SIZE[0],5))
image.fill(BLACK,(0,500,SCREEN_SIZE[0],5))
for i in range(0,SCREEN_SIZE[0],40):
image.fill(WHITE,(i,450,5,105))
return image
def get_target(self,mouse,cell_size):
return mouse[0]//cell_size[0],mouse[1]//cell_size[1]
def draw_frame_select(self):
location = (5+self.current_frame*TIMELINE_CELL_SIZE[0],450+5)
size = TIMELINE_CELL_SIZE[0]-5,TIMELINE_CELL_SIZE[1]-5
rect = pg.Rect((0,0),size)
image = pg.Surface(size).convert_alpha()
image.fill((0,0,0,0))
pg.draw.circle(image,GREEN,rect.center,15)
pg.draw.circle(image,(0,0,0,0),rect.center,10)
self.screen.blit(image,rect.move(location))
def draw_nonempty_frames(self):
for i,frame in enumerate(self.frames):
if frame:
self.fill_timeline_frame(i,(75,75,255))
def fill_timeline_frame(self,index,color):
location = (5+index*TIMELINE_CELL_SIZE[0],450+5)
size = TIMELINE_CELL_SIZE[0]-5,TIMELINE_CELL_SIZE[1]-5
rect = pg.Rect(location,size)
self.screen.fill(color,rect)
def animating(self):
current_time = pg.time.get_ticks()
if current_time-self.timer > 1000/self.animate_fps:
self.animate_frame = (self.animate_frame+1)%len(self.animate_frames)
self.timer = current_time
index,frame = self.animate_frames[self.animate_frame]
frame.update(self.screen)
self.fill_timeline_frame(index,GREEN)
def change_frame(self,coord):
current_dict = self.frames[self.current_frame].cell_dict
self.current_frame = coord
if not self.frames[self.current_frame]:
self.frames[self.current_frame] = Frame(current_dict)
def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
elif event.type == pg.MOUSEBUTTONDOWN:
self.process_mouse_events(event)
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
self.setup_animation()
def process_mouse_events(self,event):
if not self.animate:
if self.grid_rect.collidepoint(event.pos):
coord = self.get_target(event.pos,CELL_SIZE)
if event.button == 1:
self.frames[self.current_frame].add_cell(coord)
elif event.button == 3:
self.frames[self.current_frame].delete_cell(coord)
elif self.timeline_rect.collidepoint(event.pos):
if event.button == 1:
coord = self.get_target(event.pos,TIMELINE_CELL_SIZE)
self.change_frame(coord[0])
def setup_animation(self):
self.animate = not self.animate
self.animate_frames = [(i,frame) for (i,frame) in enumerate(self.frames) if frame]
self.animate_frame = 0
self.timer = pg.time.get_ticks()
def update(self):
self.screen.blit(self.grid_image,(0,0))
if not self.animate:
self.frames[self.current_frame].update(self.screen)
self.draw_nonempty_frames()
self.draw_frame_select()
else:
self.draw_nonempty_frames()
self.animating()
def game_loop(self):
while not self.done:
self.event_loop()
self.update()
pg.display.update()
self.clock.tick(self.fps)
class Frame(object):
def __init__(self,cell_dict=None):
if cell_dict:
self.cell_dict = cell_dict.copy()
else:
self.cell_dict = {}
def update(self,surface):
for cell in self.cell_dict:
location = 6+cell[0]*CELL_SIZE[0],5+cell[1]*CELL_SIZE[1]
size = CELL_SIZE[0]-5,CELL_SIZE[1]-5
rect = pg.Rect(location,size)
pg.draw.circle(surface,RED,rect.center,15)
def add_cell(self,coords):
self.cell_dict[coords] = True
def delete_cell(self,coords):
if coords in self.cell_dict:
self.cell_dict.pop(coords)
if __name__ == "__main__":
run_it = Control()
run_it.game_loop()
pg.quit();sys.exit()