IME, IZBOR, POENI, X_SL, Y_SL, X_TEKST, Y_TEKST = 0, 1, 2, 3, 4, 5, 6 covek = ["Човек", -1, 0, x_slike, y_slike, 0, 0] racunar = ["Рачунар", -1, 0, x_slike + sirina//2, y_slike, sirina//2, 0] font = pg.font.SysFont("Arial", 20) def crtaj_igraca(igrac): if igrac[IZBOR] >= 0: prozor.blit(slike[igrac[IZBOR]], (igrac[X_SL], igrac[Y_SL])) tekst = igrac[IME] + ": " + str(igrac[POENI]) slika_teksta = font.render(tekst, True, pg.Color("black")) prozor.blit(slika_teksta, (igrac[X_TEKST], igrac[Y_TEKST])) def nov_frejm(): prozor.fill(pg.Color("white")) crtaj_igraca(covek) crtaj_igraca(racunar) def obradi_dogadjaj(dogadjaj): global covek, racunar if dogadjaj.type == pg.KEYDOWN: pkm = {pg.K_p : PAPIR, pg.K_k : KAMEN, pg.K_m : MAKAZE} if dogadjaj.key in pkm.keys(): covek[IZBOR] = pkm[dogadjaj.key] racunar[IZBOR] = random.randint(0, 2) if racunar[IZBOR] == (covek[IZBOR] + 1) % 3: covek[POENI] += 1 elif covek[IZBOR] == (racunar[IZBOR] + 1) % 3: racunar[POENI] += 1 pygamebg.frame_loop(15, nov_frejm, obradi_dogadjaj)
sunce_slika = pg.image.load("sun.png") # slika sunca avion_slika = pg.image.load("airplane.png") # slika aviona avion_sirina = avion_slika.get_width() # sirina slike aviona avion_visina = avion_slika.get_height() # visina slike aviona # položaj aviona - na pocetku sredina leve ivice prozora (avion_x, avion_y) = (0, (visina - avion_visina) // 2) i_frejm = 0 KRAJ_PRVE_FAZE = 20 UKUPNO_FREJMOVA = 40 def nov_frejm(): global avion_x, avion_y, i_frejm # menjacemo položaj aviona i brojac frejmova i_frejm = (i_frejm + 1) % UKUPNO_FREJMOVA if i_frejm < KRAJ_PRVE_FAZE: avion_y -= 2 # u prvoj fazi pomeramo avion 1 piksel na gore else: avion_y += 2 # u drugoj fazi pomeramo avion 1 piksel na dole avion_x += 2 # pomeramo avion 1 piksel na desno if avion_x > sirina: avion_x = -avion_sirina prozor.fill(pg.Color("skyblue")) # bojimo pozadinu u plavo prozor.blit(sunce_slika, (0, 0)) # crtamo sunce prozor.blit(avion_slika, (avion_x, avion_y)) # crtamo avion pygamebg.frame_loop(50, nov_frejm)
import pygame as pg, pygamebg (sirina, visina) = (750, 150) prozor = pygamebg.open_window(sirina, visina, "Прича") font = pg.font.SysFont("Century", 40) # font kojim će biti prikazan tekst uvod = "Ово је једна стара прича" nastavak = " о једном човеку, који је желео да му неко исприча причу" br_slova_za_prikaz = 33 vidljivi_tekst = (uvod + nastavak)[0:br_slova_za_prikaz] poz_sledeceg_slova = br_slova_za_prikaz - len(uvod) fps = 5 i_frejm = 0 def nov_frejm(): global vidljivi_tekst, poz_sledeceg_slova, i_frejm i_frejm += 1 if i_frejm > 8: vidljivi_tekst = vidljivi_tekst[1:] + nastavak[poz_sledeceg_slova] poz_sledeceg_slova = (poz_sledeceg_slova + 1) % len(nastavak) prozor.fill(pg.Color("skyblue")) # bojimo pozadinu # gradimo sličicu од tekstа i prikazujemo je slika_teksta = font.render(vidljivi_tekst, True, pg.Color("black")) prozor.blit(slika_teksta, (50, 50)) pygamebg.frame_loop(fps, nov_frejm)
# ima li sudara? for bot_red, bot_kol in botovi: if bot_red == igrac_red and bot_kol == igrac_kol: kraj, pobeda = True, False if cilj_red == igrac_red and cilj_kol == igrac_kol: kraj, pobeda = True, True prozor.fill(pg.Color("gray")) # bojimo prozor u sivo if kraj: if pobeda: sl = font.render('Браво!', True, pg.Color("red")) else: sl = font.render('Штета!', True, pg.Color("blue")) prozor.blit(sl, (0, 0)) else: # crtamo 'igraca' (x, y) = (igrac_kol * a + a // 2, igrac_red * a + a // 2) pg.draw.circle(prozor, pg.Color('yellow'), (x, y), a // 3) (x, y) = (cilj_kol * a + a // 2, cilj_red * a + a // 2) pg.draw.circle(prozor, pg.Color('white'), (x, y), a // 5) # crtamo botove for bot_red, bot_kol in botovi: (x, y) = (bot_kol * a + a // 2, bot_red * a + a // 2) pg.draw.circle(prozor, pg.Color('black'), (x, y), a // 5) pygamebg.frame_loop(15, nov_frejm)
message = "Well done!" # message to be displayed text_image = font.render(message, True, pg.Color("black")) (text_width, text_height) = (text_image.get_width(), text_image.get_height() ) # determine text size (x, y) = ((width - text_width) / 2, (height - text_height) / 2 ) # we set position so that text is centered canvas.blit(text_image, (x, y)) # display text image at approptiate position def new_frame(): if not puzzle_solved(): # puzzle not solved yet, displayi it display_puzzle() else: # puzzle is solved, congratulating congratulate() def handle_event(event): global row, col, puzzle if puzzle_solved(): # if puzzle is solved, don't handle events return if event.type == pg.MOUSEBUTTONDOWN: # mouse click (x, y) = event.pos # the coordinates that were clicked row = y // ROW_HEIGHT # row that were clicked col = x // COL_WIDTH # column that were clicked move_hole(row, col) pygamebg.frame_loop(15, new_frame, handle_event)
import pygame as pg, pygamebg canvas = pygamebg.open_window(400, 400, "Background color") shade = 128 def new_frame(): global shade pressed_mouse_button = pg.mouse.get_pressed() if pressed_mouse_button[0] and shade < 255: # 0 is left button shade += 1 if pressed_mouse_button[2] and shade > 0: # 2 is ringt button shade -= 1 color = (shade, shade, shade) canvas.fill(color) pygamebg.frame_loop(100, new_frame)
yellow_off = (128, 128, 0) green_on = (0, 255, 0) green_off = (0, 128, 0) i_frame = 0 fps = 10 def draw_trafficlights(color_up, color_middle, color_down): pg.draw.circle(canvas, color_up, (x, y[0]), r) pg.draw.circle(canvas, color_middle, (x, y[1]), r) pg.draw.circle(canvas, color_down, (x, y[2]), r) def new_frame(): global i_frame i_frame = (i_frame + 1) % total_frames canvas.fill(pg.Color("darkgray")) # paint background if i_frame < stage_end[0]: # if the frame belongs to stage 'red' draw_trafficlights(red_on, yellow_off, green_off) elif i_frame < stage_end[1]: # if the frame belongs to stage 'red_yellow' draw_trafficlights(red_on, yellow_on, green_off) elif i_frame < stage_end[2]: # if the frame belongs to stage 'green' draw_trafficlights(red_off, yellow_off, green_on) else: # frame belongs to the last stage ('yellow') draw_trafficlights(red_off, yellow_on, green_off) pygamebg.frame_loop(fps, new_frame)
x1 += velocity1 if pressed[pygame.K_a]: x1 -= velocity1 if pressed[pygame.K_s]: y1 += velocity1 if pressed[pygame.K_w]: y1 -= velocity1 if x1 > 690: root = tkinter.Tk() root.withdraw() messagebox.showinfo("Pobeda!", "Drugi igrač je pobedio") raise Exception() pygame.draw.circle(prozor, pygame.Color("magenta"), (x1, y1), 30) pygamebg.frame_loop(30, update) ''' while running: ev = pygame.event.get() for event in ev: if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False elif event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: x0+=1 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x0-=1
prozor = pygamebg.open_window(500, 500, "Increasing and decreasing speed") x = 150 y = 150 vx = 0 vy = 0 fps = 30 def osvezi(): global x,y,vx,vy pritisnuto = pg.key.get_pressed() if pritisnuto[pg.K_RIGHT]: vx += 1 if pritisnuto[pg.K_LEFT]: vx -= 1 if pritisnuto[pg.K_DOWN]: vy += 1 if pritisnuto[pg.K_UP]: vy -= 1 x = (x + vx/fps) % sirina y = (y + vy/fps) % visina prozor.fill(pg.Color("white")) boja = pg.Color("red") pg.draw.circle(prozor, boja, (int(x), int(y)), 30) pygamebg.frame_loop(fps, osvezi)
fps = 30 x, y = 150, 150 vx, vy = 0, 0 def update(): global x, y x = (x + vx / fps) % width y = (y + vy / fps) % height surface.fill(pg.Color("white")) color = pg.Color("red") pg.draw.circle(surface, color, (int(x), int(y)), 30) def handle_event(d): global vx, vy if d.type == pg.KEYDOWN: if d.key == pg.K_RIGHT: vx += 1 elif d.key == pg.K_LEFT: vx -= 1 elif d.key == pg.K_DOWN: vy += 1 elif d.key == pg.K_UP: vy -= 1 pygamebg.frame_loop(fps, update, handle_event)
import pygamebg import random #inicijalizacija prozora (sirina, visina) = (400, 400) pg.init() prozor = pg.display.set_mode((sirina, visina)) prozor.fill(pg.Color("white")) #odredjivanje broja kvadrata po kolonama i njihova velicina broj_kvadrata = 10 a = int(sirina / broj_kvadrata) #funkcija koja će u dati neku nasumičnu boju u RGB obliku def nasumicna_boja(): return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) #funkcija koja iscrtava mrežu kvadrata nasumičnih boja def crtaj(): for i in range(broj_kvadrata): for j in range(broj_kvadrata): pg.draw.rect(prozor, nasumicna_boja(), (j * a, i * a, a, a)) #funkciju crtaj ćemo pozivati svake sekunde pa će se tako menjati boja kvadrata pygamebg.frame_loop(1, crtaj)
(sirina, visina) = (500, 300) # otvaramo prozor prozor = pygamebg.open_window(sirina, visina, "Auto i avion") avion_slika = pg.image.load("../images/avion.png") (avion_x, avion_y) = (0, 0)#avion se pojavljuje u gornjem levom uglu auto_slika = pg.image.load("../images/auto.png") (auto_x, auto_y) = (0, visina - auto_slika.get_height())#auto se pojavljuje u donjem levom uglu def crtaj(): prozor.fill(pg.Color("white")) prozor.blit(avion_slika, (avion_x, avion_y)) # crtamo avion prozor.blit(auto_slika, (auto_x, auto_y)) # crtamo auto def novi_frejm(): global avion_x, avion_y, auto_x, auto_y # pomeramo avion avion_x += 2 if avion_x > sirina: avion_x = - avion_slika.get_width() # pomeramo auto auto_x += 2 if auto_x > sirina: auto_x = - auto_slika.get_width() crtaj() pygamebg.frame_loop(100, novi_frejm)
width, height = 500, 300 surface = pygamebg.open_window(width, width, "Increasing and decreasing speed") pg.key.set_repeat(10,10) fps = 30 x, y = 150, 150 vx, vy = 0, 0 def update(): global x,y x = (x + vx/fps) % width y = (y + vy/fps) % height surface.fill(pg.Color("white")) color = pg.Color("red") pg.draw.circle(surface, color, (int(x), int(y)), 30) def keydown(e): global vx, vy if e.key == pg.K_RIGHT: vx += 1 elif e.key == pg.K_LEFT: vx -= 1 elif e.key == pg.K_DOWN: vy += 1 elif e.key == pg.K_UP: vy -= 1 pygamebg.frame_loop(fps, update, {pg.KEYDOWN: keydown})
font = pg.font.SysFont("Arial", 60) image_text = font.render("The end!", True, pg.Color("black")) x = (width - image_text.get_width()) // 2 y = (height - image_text.get_height()) // 2 canvas.blit(image_text, (x, y)) else: # draw snakes for color, body, direction, keys, i_head in snakes: for row, col in body: pg.draw.rect(canvas, color, (col*a, row*a, a, a)) def new_frame(): global snakes, done pressed = pg.key.get_pressed() for i_snake in range(2): color, body, (d_row, d_col), keys, i_head = snakes[i_snake] if pressed[keys[LEFT]]: (d_row, d_col) = (0, -1) if pressed[keys[RIGHT]]: (d_row, d_col) = (0, 1) if pressed[keys[UP]]: (d_row, d_col) = (-1, 0) if pressed[keys[DOWN]]: (d_row, d_col) = (1, 0) row, col = body[i_head] i_head = (i_head + 1) % len(body) body[i_head] = (row + d_row, col + d_col) if col < 0 or col >= num_cols or row < 0 or row >= num_rows: done = True # the snake came out of the board snakes[i_snake] = color, body, (d_row, d_col), keys, i_head draw() pygamebg.frame_loop(3, new_frame)
laser_on = False energy = 25 # how full is the laser from 0 to 100 def draw(): canvas.fill(pg.Color("black")) # background # the indicator shows how full the laser is pg.draw.rect(canvas, pg.Color("green"), (10, 10, 100, 10), 1) pg.draw.rect(canvas, pg.Color("green"), (10, 10, energy, 10)) if laser_on: reach = (4 * energy, height - 4 * energy) pg.draw.line(canvas, pg.Color("red"), (0, height), reach, 5) def new_frame(): global energy, laser_on pressed_mouse_button = pg.mouse.get_pressed() laser_on = pressed_mouse_button[0] # left button if laser_on: if energy > 0: # if the laser did not empty energy -= 1 # it empties else: # the laser is charged, but up to the maximum of 100 energy = min(energy + 2, 100) draw() pygamebg.frame_loop(15, new_frame)
import pygame as pg, pygamebg (width, height) = (400, 400) canvas = pygamebg.open_window(width, height, "Ball following the mouse") (x, y) = (width // 2, height // 2) # ball starts from center of the window def new_frame(): global x, y (xm, ym) = pg.mouse.get_pos() # mouse position coordinates # displacement is the tenth of the distance to the mouse dx = 0.1 * (xm - x) dy = 0.1 * (ym - y) pressed_mouse_button = pg.mouse.get_pressed() if pressed_mouse_button[0]: x, y = x - dx, y - dy # ball moves away from the mouse else: x, y = x + dx, y + dy # ball moves towards the mouse # draw a green ball on a white background canvas.fill(pg.Color("white")) pg.draw.circle(canvas, pg.Color("green"), (int(x), int(y)), 10) pygamebg.frame_loop(50, new_frame)
import pygame as pg, pygamebg, random (sirina, visina) = (400, 400) prozor = pygamebg.open_window(sirina, visina, "Лептир прати миша") leptir_slike = [pg.image.load("butterfly1.png"), pg.image.load("butterfly2.png")] i_frejm = 0 frejmova_po_slici = 10 def nov_frejm(): global i_frejm i_frejm += 1 i_slika = (i_frejm % (len(leptir_slike) * frejmova_po_slici)) // frejmova_po_slici (mis_x, mis_y) = pg.mouse.get_pos() prozor.fill(pg.Color("white")) slika = leptir_slike[i_slika] # slika koja se prikazuje # prikazujemo sliku centrirano (x, y) = (mis_x - slika.get_width() / 2, mis_y - slika.get_height() / 2) prozor.blit(slika, (x, y)) pygamebg.frame_loop(5 * frejmova_po_slici, nov_frejm)