示例#1
0
import random, pygame as pg, pygamebg
(width, height) = (500, 300)
canvas = pygamebg.open_window(width, height, "Stars")
cx, cy = width // 2, height // 2


# A star is determined by its position (x, y) and size (r).
def new_star():
    r = random.randint(1, 3)
    x = random.randint(r, width - r)
    y = random.randint(r, height - r)
    return (x, y, r)


# Create a list of stars.
num_stars = 40
stars = []
for _ in range(num_stars):
    stars.append(new_star())


def new_frame():
    global stars
    next_stars = []  # list that will contain the next state
    for x, y, r in stars:
        x += 0.01 * (x - cx)  # x moves away from the center of the window
        y += 0.01 * (y - cy)  # y moves away from the center of the window
        r *= 1.01  # we see the star as bigger because we are "approaching"
        # if at least part of the star is in the window, we'll keep it
        if (x + r > 0 and x - r < width and y + r > 0 and y - r < height):
            next_stars.append((x, y, r))
示例#2
0
num_rows, num_cols = 2, 4
moles = []
mole_hit = []
for row in range(num_rows):
    moles.append([0] * num_cols)
    mole_hit.append([False] * num_cols)

# read mole images into the list
mole_images = []  # list that will contain images
for i in range(1, 11):  # read images mole1.png, ..., mole10.png
    image_name = "mole" + str(i) + ".png"  # build image name from parts
    mole_images.append(pg.image.load(image_name))

a = mole_images[0].get_width()  # image size (the pictures are square-shaped)
(width, height) = (num_cols * a, num_rows * a)
canvas = pygamebg.open_window(width, height, "Moles")
BROWN = (60, 42, 3)
num_visible_not_hit = 0
num_not_hit = num_rows * num_cols


def center_text(x, y, text, size):
    font = pg.font.SysFont("Arial", size)
    im = font.render(text, True, pg.Color("black"))
    (x, y) = (x - im.get_width() / 2, y - im.get_height() / 2)
    canvas.blit(im, (x, y))


def draw():
    if num_not_hit == 0:
        canvas.fill(pg.Color('white'))
示例#3
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
prozor = pygamebg.open_window(300, 300, "Pygame")

# -*- acsection: main -*-
(sirina, visina) = (300, 300)
def crtanje():
    prozor.blit(platno, (0, 0))  # crtanje onoga sto je zadato
    
    # ose
    pg.draw.line(prozor, pg.Color("black"), (mis_x, 0), (mis_x, visina), 1) # uspravna linija misa
    pg.draw.line(prozor, pg.Color("black"), (0, mis_y), (sirina, mis_y), 1) # vodoravna linija misa

    # ispis koordinata
    str_x, str_y = str(mis_x), str(mis_y)
    xt_y, yt_y = (5, mis_y - 25) if 2 * mis_y > visina else (visina - 25, mis_y + 5)
    xt_x, yt_x = (mis_x - 50, 5) if 2 * mis_x > sirina else (mis_x + 5, sirina - 50)
    sl_x = font.render(str_x, True, pg.Color("black"))
    sl_y = font.render(str_y, True, pg.Color("black"))
    if mis_x <= 150:
        prozor.blit(sl_x, (xt_x, xt_y))
    if mis_y <= 60 or mis_y >= 135:
        prozor.blit(sl_y, (yt_x, yt_y))

def obradi_dogadjaj(dogadjaj):
    global mis_x, mis_y
    if dogadjaj.type == pg.MOUSEMOTION:   # miš je pomeren
        mis_x, mis_y = dogadjaj.pos
        return True                         # ponovo iscrtavamo scenu
    return False                            # nema potrebe da iscrtavamo scenu
示例#4
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
canvas = pygamebg.open_window(300, 300, "Cat")

# -*- acsection: main -*-
(width, height) = (300, 300)


def draw():
    canvas.blit(user_canvas, (0, 0))  # attach users drawing

    # axes
    pg.draw.line(canvas, pg.Color("black"), (mouse_x, 0), (mouse_x, height),
                 1)  # vertical mouse line
    pg.draw.line(canvas, pg.Color("black"), (0, mouse_y), (width, mouse_y),
                 1)  # horizontal mouse line

    # write coordinates
    str_x, str_y = str(mouse_x), str(mouse_y)
    xt_y, yt_y = (5, mouse_y - 25) if 2 * mouse_y > height else (height - 25,
                                                                 mouse_y + 5)
    xt_x, yt_x = (mouse_x - 50, 5) if 2 * mouse_x > width else (mouse_x + 5,
                                                                width - 50)
    im_x = font.render(str_x, True, pg.Color("black"))
    im_y = font.render(str_y, True, pg.Color("black"))
    if mouse_x <= width // 2:
        canvas.blit(im_x, (xt_x, xt_y))
    canvas.blit(im_y, (yt_x, yt_y))


def handle_event(event):
示例#5
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
canvas = pygamebg.open_window(300, 300, "Кућа")

# -*- acsection: main -*-
(width, height) = (300, 300)


def draw():
    canvas.blit(user_canvas, (0, 0))  # attach users drawing

    # axes
    pg.draw.line(canvas, pg.Color("black"), (mouse_x, 0), (mouse_x, height),
                 1)  # vertical mouse line
    pg.draw.line(canvas, pg.Color("black"), (0, mouse_y), (width, mouse_y),
                 1)  # horizontal mouse line

    # write coordinates
    str_x, str_y = str(mouse_x), str(mouse_y)
    xt_y, yt_y = (5, mouse_y - 25) if 2 * mouse_y > height else (height - 25,
                                                                 mouse_y + 5)
    xt_x, yt_x = (mouse_x - 50, 5) if 2 * mouse_x > width else (mouse_x + 5,
                                                                width - 50)
    im_x = font.render(str_x, True, pg.Color("black"))
    im_y = font.render(str_y, True, pg.Color("black"))
    canvas.blit(im_x, (xt_x, xt_y))
    canvas.blit(im_y, (yt_x, yt_y))


def handle_event(event):
    global mouse_x, mouse_y
import pygame as pg, pygamebg
(width, height) = (400, 300)
canvas = pygamebg.open_window(width, height, "Car")

car_image = pg.image.load("car.png")
(car_width, car_height) = (car_image.get_width(), car_image.get_height()
                           )  # car image size

fps = 50  # number of frames per second
dt = 1 / fps  # duration of one frame in seconds
car_v = 100  # car speed (pixels per second)
(car_x, car_y) = (0, height - car_height
                  )  # car position (lower left corner initially)


def new_frame():
    global car_x  # we will only change x coordinate of the car
    car_x += car_v * dt  # move car to the right
    if car_x > width:  # if it went out of the canvas
        car_x = -car_width  # bring it back to the beginning

    canvas.fill(pg.Color("skyblue"))  # paint background to sky-blue
    canvas.blit(car_image, (car_x, car_y))  # displaying car image


pygamebg.frame_loop(fps, new_frame)
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
(sirina, visina) = (300, 300)
prozor = pygamebg.open_window(sirina, visina, "Медведић")

# -*- acsection: main -*-
# bojimo pozadinu prozora u belo
prozor.fill(pg.Color("white"))


def uokviren_krug(prozor, boja, centar, poluprecnik):
    pg.draw.circle(prozor, boja, centar, poluprecnik)
    pg.draw.circle(prozor, pg.Color("black"), centar, poluprecnik, 1)


def crtaj_medu(cx, cy, a):
    uokviren_krug(prozor, pg.Color("yellow"), (cx - 12 * a, cy - 14 * a),
                  9 * a)  # levo uvo
    uokviren_krug(prozor, pg.Color("yellow"), (cx + 12 * a, cy - 14 * a),
                  9 * a)  # desno uvo
    uokviren_krug(prozor, pg.Color("yellow"), (cx, cy), 20 * a)  # glava
    uokviren_krug(prozor, pg.Color("yellow"), (cx, cy + 10 * a),
                  10 * a)  # njuska
    uokviren_krug(prozor, pg.Color("black"), (cx - 10 * a, cy - 6 * a),
                  3 * a)  # levo oko
    uokviren_krug(prozor, pg.Color("black"), (cx + 10 * a, cy - 6 * a),
                  3 * a)  # desno oko
    uokviren_krug(prozor, pg.Color("black"), (cx, cy + 4 * a),
                  3 * a)  # vrh njuske

示例#8
0
import pygame as pg, pygamebg
tekst = "ПАЈТОН"
(sirina, visina) = (len(tekst) * 70, 100)
prozor = pygamebg.open_window(sirina, visina, "Реклама")

font = pg.font.SysFont("Arial", 80)  # font kojim će biti prikazan tekst
slika_teksta = font.render(tekst, True, pg.Color("yellow"))
x = (sirina - slika_teksta.get_width()) // 2
y = (visina - slika_teksta.get_height()) // 2
svetli = True


def nov_frejm():
    global svetli
    svetli = not svetli
    prozor.fill(pg.Color("black"))  # bojimo pozadinu prozora u crno
    if svetli:
        prozor.blit(slika_teksta, (x, y))


pygamebg.frame_loop(3, nov_frejm)
示例#9
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
sirina, visina = 400, 400
prozor = pygamebg.open_window(sirina, visina, "Убаци лоптицу")
font = pg.font.SysFont("Arial", 30) # font kojim će biti prikazan tekst

r = 10 # velicina loptice
(cilj_x, cilj_y) = (sirina//4, visina//4) # ciljna tacka
cilj_kutija = (cilj_x - 2*r, cilj_y - 2*r, 4*r, 4*r) # pravougaonik oko ciljne tacke

(x, y) = (sirina//2, visina//2) # loptica krece iz centra
pobedio, izgubio = False, False

def crtanje():
    prozor.fill(pg.Color("black")) # crna pozadina
    if pobedio or izgubio:
        # igra je zavrsena, ispisujemo poruku
        poruka = "Браво!" if pobedio else "Побеже..."
        slika_teksta = font.render(poruka, True, pg.Color("green"))
        tx = (sirina - slika_teksta.get_width()) // 2
        ty = (visina - slika_teksta.get_height()) // 2
        prozor.blit(slika_teksta, (tx, ty))
    else:
        # igra jos traje, crtamo kutiju i lopticu
        pg.draw.rect(prozor, pg.Color("red"), cilj_kutija, 3)
        pg.draw.circle(prozor, pg.Color("green"), (int(x), int(y)), 10)

# -*- acsection: main -*-
def nov_frejm():
    global x, y, pobedio, izgubio
    
示例#10
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
(width, height) = (300, 300)
canvas = pygamebg.open_window(width, height, "Стрелице")


# -*- acsection: main -*-
def draw_polygon(points, color, x0, y0):
    shifted_points = []
    for x, y in points:
        shifted_points.append((x + x0, y + y0))
    pg.draw.polygon(canvas, color, shifted_points)


arrow = [(0, 10), (40, 10), (40, 0), (60, 20), (40, 40), (40, 30), (0, 30)]
arrow_length, arrow_height = 60, 40
canvas.fill(pg.Color("white"))
for y0 in range(0, height, arrow_height):
    for x0 in range(0, width, arrow_length):
        draw_polygon(arrow, pg.Color("black"), x0, y0)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
prozor = pygamebg.open_window(800, 600, "Јабуке")

# -*- acsection: main -*-
drvo_slika = pg.image.load("tree.png")  # slika drveta
jabuka_slika = pg.image.load("apple_small.png")  # slika jabuke
korpa_slika = pg.image.load("basket.png")  # slika korpe
jabuke_na_drvetu_poz = ((43, 191), (61, 158), (124, 145), (134, 175), (160,
                                                                       180))
jabuke_u_korpi_poz = ((15, 38), (60, 41), (22, 43), (49, 45), (34, 48))
drvece_poz = ((200, 70), (120, 150), (240, 290), (550, 170), (400, 200))


def drvo_korpa_jabuke(drvo_x, drvo_y):
    korpa_x = drvo_x + drvo_slika.get_width() - korpa_slika.get_width()
    korpa_y = drvo_y + drvo_slika.get_height() - korpa_slika.get_height()
    prozor.blit(drvo_slika, (drvo_x, drvo_y))
    prozor.blit(korpa_slika, (korpa_x, korpa_y))
    for x, y in jabuke_na_drvetu_poz:
        prozor.blit(jabuka_slika, (drvo_x + x, drvo_y + y))
    for x, y in jabuke_u_korpi_poz:
        prozor.blit(jabuka_slika, (korpa_x + x, korpa_y + y))


prozor.fill(pg.Color("darkgreen"))  # bojimo pozadinu ekrana u tamno zeleno
for drvo_x, drvo_y in drvece_poz:
    drvo_korpa_jabuke(drvo_x, drvo_y)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#12
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
canvas = pygamebg.open_window(300, 300, "fence")
# -*- acsection: main -*-

canvas.fill(pg.Color("skyblue"))  # paint background
pg.draw.rect(canvas, pg.Color("green"), (0, 200, 300, 100))  # grass

pg.draw.line(canvas, pg.Color('brown'), (10, 100), (290, 100), 10)
pg.draw.line(canvas, pg.Color('brown'), (10, 250), (290, 250), 10)


def draw_polygon(points, color, x0, y0):
    shifted_points = [(x + x0, y + y0) for x, y in points]
    pg.draw.polygon(canvas, color, shifted_points)


picket = [(0, 80), (10, 70), (20, 80), (20, 270), (0, 270)]
for x0 in range(20, 300, 40):
    draw_polygon(picket, pg.Color('brown'), x0, 0)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#13
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
width, height = 300, 300
canvas = pygamebg.open_window(width, height, "Colored hexagons")


# -*- acsection: main -*-
def draw_polygon(points, color, x0, y0):
    shifted_points = []
    for x, y in points:
        shifted_points.append((x + x0, y + y0))
    pg.draw.polygon(canvas, color, shifted_points)


hexagon = [(10, 0), (30, 0), (40, 17), (30, 34), (10, 34), (0, 17)]
for y0 in range(-17, height, 102):
    for x0 in range(-10, width, 60):
        draw_polygon(hexagon, pg.Color("blue"), x0, y0)
        draw_polygon(hexagon, pg.Color("yellow"), x0, y0 + 34)
        draw_polygon(hexagon, pg.Color("green"), x0, y0 + 68)
        draw_polygon(hexagon, pg.Color("green"), x0 + 30, y0 + 17)
        draw_polygon(hexagon, pg.Color("blue"), x0 + 30, y0 + 51)
        draw_polygon(hexagon, pg.Color("yellow"), x0 + 30, y0 + 85)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#14
0
import pygame as pg, pygamebg
(sirina, visina) = (800, 500)
prozor = pygamebg.open_window(sirina, visina, "Прекидачи")

shema_slike = (pg.image.load('Shema3_Off.png'), pg.image.load('Shema3_On.png'))
prekidac_slike = (pg.image.load('SwitchOff.png'),
                  pg.image.load('SwitchOn.png'))
sijalica_slike = (pg.image.load('BulbOff.png'), pg.image.load('BulbOn.png'))

ukljucen_prekidac = [False, False, False]
prekidac_poz = [(100, 200), (300, 150), (300, 250)]
sijalica_poz = (500, 100)


def nov_frejm():
    svetli = ukljucen_prekidac[0] and (ukljucen_prekidac[1]
                                       or ukljucen_prekidac[2])
    prozor.blit(shema_slike[svetli], (0, 0))
    for i in range(3):
        prozor.blit(prekidac_slike[ukljucen_prekidac[i]], prekidac_poz[i])
    prozor.blit(sijalica_slike[svetli], sijalica_poz)


def tacka_u_pravougaoniku(tacka, gornje_levo_teme, sirina, visina):
    x, y = tacka
    x0, y0 = gornje_levo_teme
    return x0 <= x and x <= x0 + sirina and y0 <= y and y <= y0 + visina


def obradi_dogadjaj(dogadjaj):
    global ukljucen_prekidac
示例#15
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg

prozor = pygamebg.open_window(300, 300, "Кућа")

# -*- acsection: main -*-
(sirina, visina) = (300, 300)


def crtanje():
    prozor.blit(platno, (0, 0))  # crtanje onoga sto je zadato

    # ose
    pg.draw.line(prozor, pg.Color("black"), (mis_x, 0), (mis_x, visina),
                 1)  # uspravna linija misa
    pg.draw.line(prozor, pg.Color("black"), (0, mis_y), (sirina, mis_y),
                 1)  # vodoravna linija misa

    # ispis koordinata
    str_x, str_y = str(mis_x), str(mis_y)
    xt_y, yt_y = (5, mis_y - 25) if 2 * mis_y > visina else (visina - 25,
                                                             mis_y + 5)
    xt_x, yt_x = (mis_x - 50, 5) if 2 * mis_x > sirina else (mis_x + 5,
                                                             sirina - 50)
    sl_x = font.render(str_x, True, pg.Color("black"))
    sl_y = font.render(str_y, True, pg.Color("black"))
    prozor.blit(sl_x, (xt_x, xt_y))
    prozor.blit(sl_y, (yt_x, yt_y))


def obradi_dogadjaj(dogadjaj):
示例#16
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
canvas = pygamebg.open_window(400, 400, "Clouds")

# -*- acsection: main -*-

canvas.fill(pg.Color("skyblue"))  # paint background

# draw the sun
pg.draw.circle(canvas, pg.Color("yellow"), (100, 130), 80)


# function draws a cloud at a given position, given size and shade of gray
def cloud(xc, yc, shade):
    # draw a cloud of three circles
    color = (shade, shade, shade)
    pg.draw.circle(canvas, color, (xc, yc), 50)
    pg.draw.circle(canvas, color, (xc - 50, yc), 30)
    pg.draw.circle(canvas, color, (xc + 50, yc), 30)


cloud(240, 200, 180)
cloud(270, 250, 210)
cloud(230, 100, 230)
cloud(80, 80, 190)
cloud(110, 320, 255)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#17
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
(sirina, visina) = (300, 300)
prozor = pygamebg.open_window(sirina, visina, "Саће")

# -*- acsection: main -*-
def crtaj_mnogougao(temena, boja, x0, y0):
    pomerena_temena = []
    for x, y in temena:
        pomerena_temena.append((x+x0, y+y0))
    pg.draw.polygon(prozor, boja, pomerena_temena, 1)

sestougao = [(10, 0), (30, 0), (40, 17), (30, 34), (10, 34), (0, 17)]
prozor.fill(pg.Color("goldenrod"))
for y0 in range(-17, visina, 34):
    for x0 in range(-10, sirina, 60):
        crtaj_mnogougao(sestougao, pg.Color("brown"), x0, y0)
        crtaj_mnogougao(sestougao, pg.Color("brown"), x0 + 30, y0 + 17)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#18
0
import pygame as pg, pygamebg
(width, height) = (400, 400)
canvas = pygamebg.open_window(width, height, "Lines with mouse")

mosue_pos = (width // 2, height // 2)
line_start = mosue_pos
line_is_being_drawn = False
previous_lines = []


def new_frame():
    canvas.fill(pg.Color("white"))  # paint canvas
    if line_is_being_drawn:
        pg.draw.line(canvas, pg.Color('black'), line_start, mosue_pos)
    for a, b in previous_lines:
        pg.draw.line(canvas, pg.Color('black'), a, b)


def handle_event(event):
    global line_is_being_drawn, line_start, previous_lines, mosue_pos
    if event.type == pg.MOUSEBUTTONDOWN:
        if (event.button == 1):  # left
            line_is_being_drawn = True
            line_start = event.pos
        if (event.button == 3):  # right
            previous_lines = []
    elif event.type == pg.MOUSEBUTTONUP:
        if (event.button == 1):  # left
            line_is_being_drawn = False
            line_ends = (line_start, event.pos)
            previous_lines.append(line_ends)
示例#19
0
import pygame as pg
import pygamebg

surface = pygamebg.open_window(500, 500, "Keyboard and mouse events")
pg.key.set_repeat(10, 10)

x, y = 150, 150


def clicked(e):
    global x, y
    x, y = e.pos
    return True


def keypressed(e):
    global x, y
    if e.key == pg.K_RIGHT:
        x += 1
    elif e.key == pg.K_LEFT:
        x -= 1
    elif e.key == pg.K_DOWN:
        y += 1
    elif e.key == pg.K_UP:
        y -= 1
    else:
        return False
    return True


def paint():
示例#20
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
(sirina, visina) = (300, 300)
prozor = pygamebg.open_window(sirina, visina, "Мердевине")

# -*- acsection: main -*-
prozor.fill(pg.Color("green"))  # bojimo pozadinu ekrana u zeleno

pg.draw.line(prozor, pg.Color("brown"), (100, 10), (100, visina - 10),
             10)  # leva strana
pg.draw.line(prozor, pg.Color("brown"), (200, 10), (200, visina - 10),
             10)  # desna strana

for i in range(1, 6):
    pg.draw.line(prozor, pg.Color("brown"), (100, i * 50), (200, i * 50),
                 10)  # precaga

# -*- acsection: after-main -*-
pygamebg.wait_loop()
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
canvas = pygamebg.open_window(800, 600, "Apples")
# -*- acsection: main -*-

tree_image = pg.image.load("tree.png")  # image of a tree
apple_image = pg.image.load("apple_small.png")  # image of an apple
apple_positions = ((43, 191), (61, 158), (124, 145), (134, 175), (160, 180))

canvas.fill(pg.Color("darkgreen"))
canvas.blit(tree_image, (0, 0))
for x, y in apple_positions:
    canvas.blit(apple_image, (x, y))

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#22
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
prozor = pygamebg.open_window(300, 300, "Мачка")

# -*- acsection: main -*-
(sirina, visina) = (300, 300)


def crtanje():
    prozor.blit(platno, (0, 0))  # crtanje onoga sto je zadato

    # ose
    pg.draw.line(prozor, pg.Color("black"), (mis_x, 0), (mis_x, visina),
                 1)  # uspravna linija misa
    pg.draw.line(prozor, pg.Color("black"), (0, mis_y), (sirina, mis_y),
                 1)  # vodoravna linija misa

    # ispis koordinata
    str_x, str_y = str(mis_x), str(mis_y)
    xt_y, yt_y = (5, mis_y - 25) if 2 * mis_y > visina else (visina - 25,
                                                             mis_y + 5)
    xt_x, yt_x = (mis_x - 50, 5) if 2 * mis_x > sirina else (mis_x + 5,
                                                             sirina - 50)
    sl_x = font.render(str_x, True, pg.Color("black"))
    sl_y = font.render(str_y, True, pg.Color("black"))
    if mis_x <= sirina // 2:
        prozor.blit(sl_x, (xt_x, xt_y))
    prozor.blit(sl_y, (yt_x, yt_y))


def obradi_dogadjaj(dogadjaj):
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
(width, height) = (300, 300)
canvas = pygamebg.open_window(width, height, "Teddy-bear")
# -*- acsection: main -*-

canvas.fill(pg.Color("white"))  # paint background
YELLOW = pg.Color("yellow")
BLACK = pg.Color("black")


def draw_teddy(cx, cy, a):
    teddy = (
        #boja, (  x,   y),  r
        (YELLOW, (-12, -12), 9),  # left ear
        (YELLOW, (12, -12), 9),  # right ear
        (YELLOW, (0, 0), 20),  # head
        (YELLOW, (0, 10), 10),  # snout
        (BLACK, (-10, -6), 3),  # left eye
        (BLACK, (10, -6), 3),  # right eye
        (BLACK, (0, 4), 3),  # snout top
    )
    for color, (dx, dy), radius in teddy:
        center = (cx + dx * a, cy + dy * a)
        pg.draw.circle(canvas, color, center, a * radius)
        pg.draw.circle(canvas, BLACK, center, a * radius, 1)


draw_teddy(width // 2, height // 2, 6)

# -*- acsection: after-main -*-
示例#24
0
import pygame as pg, pygamebg
(sirina, visina) = (100, 300)
prozor = pygamebg.open_window(sirina, visina, "Семафор")

# faze su: crveno, crveno_zuto, zeleno, zuto
trajanje_faze = (25, 10, 25, 10
                 )  # 25 frejmova za crveno, 10 za crveno_zuto itd.

kraj_faze = []
ukupno_frejmova = 0
for f in trajanje_faze:
    ukupno_frejmova += f
    kraj_faze.append(ukupno_frejmova)

x = 50  # x koordinata centara krugova
y = [50, 150, 250]  # y koordinate centara krugova
r = 40  # poluprecnik (svih) krugova
crvena_uklj = (255, 0, 0)
crvena_isklj = (128, 0, 0)
zuta_uklj = (255, 255, 0)
zuta_isklj = (128, 128, 0)
zelena_uklj = (0, 255, 0)
zelena_isklj = (0, 128, 0)

i_frejm = 0
fps = 10


def crtaj_semafor(boja_gore, boja_sredina, boja_dole):
    pg.draw.circle(prozor, boja_gore, (x, y[0]), r)
    pg.draw.circle(prozor, boja_sredina, (x, y[1]), r)
示例#25
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
prozor = pygamebg.open_window(300, 500, "Страшило")
# -*- acsection: main -*-
prozor.fill(pg.Color("white")) # bojimo pozadinu ekrana u belo

pg.draw.circle(prozor, pg.Color("black"), (150, 70), 50, 6)        # glava
pg.draw.line(prozor, pg.Color("black"), (150, 120), (150, 300), 6) # telo
pg.draw.line(prozor, pg.Color("black"), (80, 170), (220, 170), 6)  # ruke
pg.draw.line(prozor, pg.Color("black"), (150, 300), (90, 480), 6)  # leva noga
pg.draw.line(prozor, pg.Color("black"), (150, 300), (210, 480), 6) # desna noga

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#26
0
import pygame as pg
import pygamebg

surface = pygamebg.open_window(400, 400, "Blue circle")

pg.draw.circle(surface, pg.Color("blue"), (200,200), 100)

pygamebg.wait_loop()
示例#27
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
prozor = pygamebg.open_window(300, 300, "Дрвеће")

# -*- acsection: main -*-
prozor.fill(pg.Color("green"))  # bojimo pozadinu ekrana u zeleno

for i in range(3):
    pg.draw.rect(prozor, pg.Color("brown"),
                 (100 * i + 40, 180, 20, 100))  # stablo
    pg.draw.ellipse(prozor, pg.Color("darkgreen"),
                    (100 * i + 10, 50, 80, 150))  # krosnja

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#28
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
prozor = pygamebg.open_window(300, 300, "Жирафа")

tacke = [(40, 208), (40, 107), (88, 82), (134, 13), (128, 9), (134, 13),
         (137, 11), (128, 6), (160, 25), (159, 28), (136, 28), (98, 101),
         (100, 106), (101, 207), (97, 207), (95, 164), (83, 121), (85, 128),
         (54, 128), (55, 119), (44, 165), (44, 208)]

# -*- acsection: main -*-

# bojimo pozadinu u tamno zeleno
prozor.fill(pg.Color("darkgreen"))

# iscrtavamo mnogougao bojom 'khaki'
pg.draw.polygon(prozor, pg.Color("khaki"), tacke)

# -*- acsection: after-main -*-
pygamebg.wait_loop()
示例#29
0
import pygame as pg, pygamebg

(sirina, visina) = (700, 250)
prozor = pygamebg.open_window(sirina, visina, "Прича")
tekst = (
    "Мала деца уче да ходају",
    "тако што почну да ходају.",
    "У почетку често падају,",
    "али устају и настављају",
    "и са временом",
    "постају све боља.",
    "Зашто не бисмо тако учили",
    "и све остале вештине?",
    " "
)

font = pg.font.SysFont("Arial", 40) # font kojim će biti prikazan tekst
MARGINA_GORE_DOLE = 30
VISINA_JEDNOG_REDA = 50
y_pocetka_teksta = 200
i_prvi_vidljivi_red = 0
br_vidljivih_redova = 1

def crtaj():
    prozor.fill(pg.Color("skyblue"))        # bojimo pozadinu
    
    i_red = i_prvi_vidljivi_red
    y = y_pocetka_teksta
    for _ in range(br_vidljivih_redova):
        # gradimo i prikazujemo sliku jednog reda tekstа
        siva = min(230 - y, 192)
示例#30
0
# -*- acsection: general-init -*-
import pygame as pg, pygamebg
num_rows, num_cols = 5, 5
a = 50  # square size
(width, height) = (a * num_cols, a * num_rows)
canvas = pygamebg.open_window(width, height, "Crossword")

font = pg.font.SysFont("Arial", 30)
board = []
for row in range(num_rows):
    board.append([' '] * num_cols)

(frame_row, frame_col) = (0, 0)


# -*- acsection: main -*-
def handle_event(event):
    global frame_row, frame_col
    if event.type == pg.KEYDOWN:
        if event.key == pg.K_LEFT:
            if frame_col > 0:
                frame_col -= 1
        elif event.key == pg.K_RIGHT:
            if frame_col < num_cols - 1:
                frame_col += 1
        elif event.key == pg.K_UP:
            if frame_row > 0:
                frame_row -= 1
        elif event.key == pg.K_DOWN:
            if frame_row < num_rows - 1:
                frame_row += 1