def display(self): g.screen.fill((220, 64, 64)) g.grid.draw() buttons.draw() x = 5 cy = g.sy(3) np = g.grid.counts[1] nw = g.grid.counts[2] if nw > 0: utils.display_number(nw, (g.sx(x), cy), g.font2, utils.ORANGE) if np > 0: utils.display_number(np, (g.sx(32 - x), cy), g.font2, utils.CYAN) x = 3.3 cy = g.sy(13.2) if g.lost > 0: utils.display_number(g.lost, (g.sx(x), cy), g.font1) cy = g.sy(14.6) if g.won > 0: utils.display_number(g.won, (g.sx(32 - x), cy), g.font1) cy = g.sy(18.7) img = g.magician if g.state != 2: img = g.magician_grey utils.centre_blit(g.screen, img, (g.sx(x), cy)) img = g.xo2 if g.state != 1: img = g.xo2_grey utils.centre_blit(g.screen, img, (g.sx(32 - x), cy)) cy = g.sy(9) if g.result != None: # smiley utils.centre_blit(g.screen, g.result, (g.sx(32 - x), cy))
class Turtle: def __init__(self, unit, side, (x, y)): #.03,16 for lge screen # unit=turtle unit (on 32x24 grid of course) # side=width of turtle screen self.top_left = (g.sx(x), g.sy(y)) # convert to pixels self.unit = g.sy(unit) self.side = g.sy(side) self.offset = self.side / 2 # turtle origin is at screen centre - hence the need fort offset self.screen = pygame.Surface((self.side, self.side)) self.screen.fill((255, 255, 255)) self.no_turtle_screen = copy.copy(self.screen) self.x = 0.0 self.y = 0.0 self.h = 0.0 self.colour = (0, 0, 0) self.repeat = 0 self.poly_n = 1 self.angle = 0 self.size = 0 #program self.turtle_showing = True self.delay = 200 self.ms = 0 self.running = False self.steps = 0 self.complete = False self.aaline_state = 0 # not tested yet, 1=ok -1=not ok
def which(self): page = 1 ind = 0 while page < self.current: nr = 4 if page == 6: nr = 3 for r in range(nr): for c in range(6): ind += 1 page += 1 y = 0 dx = g.sy(32) / 6 dy = g.sy(18.5) / 4 nr = 4 if page == 6: nr = 3 for r in range(nr): x = g.sx(0) for c in range(6): if utils.mouse_in(x, y, x + dx, y + dy): country = g.countries[ind][0] return country x += dx ind += 1 if ind == len(g.countries): return None y += dy return None
def __init__(self, cx, cy, steps, colour=BLACK): self.easy = load_image('easy.png', True) self.hard = load_image('hard.png', True) self.xo = load_image('xo.png', True) iw = self.hard.get_width() ih = self.hard.get_height() w = g.sy(20) w2 = w / 2 h2 = ih / 2 self.x1 = cx - w2 self.y = cy - h2 self.x2 = cx + w2 - iw x = cx - w2 + iw * 1.2 w = w - 2 * iw * 1.2 h = g.sy(.12) y = cy + g.sy(.34) self.rect = pygame.Rect(x, y, w, h) mh = g.sy(.5) self.mark = pygame.Rect(x, y - mh / 2 + h / 2, h, mh) self.steps = steps self.dx = w / (steps - 1) self.colour = colour self.cx = cx self.cy = cy marks = [] x = self.rect.left for i in range(self.steps): rect = copy.copy(self.mark) rect.left = x - 5 rect.width = 10 marks.append(rect) x += self.dx self.marks = marks
def new1(self): # called when nr & nc changed global squares nr, nc = RC[g.level] self.total = (nr + 2) * (nc + 2) self.d = 0.0 + g.sy(18) / nr w = nc * self.d x0 = (g.w - w - g.sx(2.7)) / 2 y0 = g.sy(.5) squares = [] y = y0 ind = 0 for r in range(nr + 2): x = x0 for c in range(nc + 2): N = ind - nc - 2 W = ind - 1 S = ind + nc + 2 E = ind + 1 sq = Square(ind, r, c, x, y, N, S, E, W) squares.append(sq) if c > 0 and c <= nc: if r > 0 and r <= nr: sq.group = 0 x += self.d ind += 1 if r > 0: y += self.d self.nr = nr self.nc = nc
def init(): global pieces, tns for ind in range(10): pieces.append(Piece()) x = 0 y = 0 hh = 0 for ind in range(10): puzzle = puzzles[ind] piece = puzzle[0] img = utils.load_image(piece + '.png', True, 'pieces') f = .5 w = int(img.get_width() * f + .5) h = int(img.get_height() * f + .5) if hh == 0: hh = h # take height from 1st piece g.sq = int(img.get_width() * .5 + .5) # and grid square side dy = (hh - h) / 2 img = pygame.transform.scale(img, (w, h)) tns.append(TN(x, y + dy, img)) x += img.get_width() + g.sy(.5) ww = x - g.sy(.5) x0 = (g.w - ww) / 2 y0 = g.screen.get_height() - hh - g.sy(1) for ind in range(10): tn = tns[ind] x, y = tn.xy x += x0 y += y0 tn.xy = (x, y)
def display(self): if self.tessl.complete(): g.screen.fill((0, 0, 0)) else: cols = [(128, 0, 128), (255, 255, 192), (0, 255, 0), (0, 255, 0), (128, 0, 128)] cols += [(0, 255, 0), (0, 0, 255), (255, 193, 127), (255, 255, 255), (0, 0, 0)] cols += [(255, 255, 192), [0, 0, 0]] g.screen.fill(cols[g.tesses[g.tess_n - 1] - 1]) self.tessl.draw() pygame.draw.rect(g.screen, (0, 0, 0), (g.sx(0), g.sy(21.55), g.sy(32), g.sy(2.55))) if self.tessl.complete(): if g.max_n < g.tess_n: g.max_n = g.tess_n self.max_n = g.max_n utils.centre_blit(g.screen, g.magician, (g.sx(16.6), g.sy(16.9))) g.button_display = True if self.tessl.carrying: g.button_display = False self.buttons_control() buttons.draw() x = g.sx(.2) y = g.sy(.2) s = str(g.tess_n) + ' / ' + str(len(g.tesses)) + ' (' + str( g.max_n) + ')' utils.display_string(s, (x, y))
def mouse_magician(self): x, y = g.magician_c x -= g.sy(.15) y -= g.sy(.52) c = x, y pygame.mouse.set_pos(c) g.pos = c
def __init__(self): gap = g.sy(.4) x = g.sx(.3) maxw = 0 maxh = 0 for ind in range(10): shape = Shape() shapes.append(shape) img = utils.load_image('0' + str(ind) + '.png', True, 'shapes') w = img.get_width() h = img.get_height() if w > maxw: maxw = w if h > maxh: maxh = h w = int(.2 * w) h = int(.2 * h) shape.img_tn = pygame.transform.scale(img, (w, h)) shape.x = x shape.y = (g.margin - h) / 2 x += (w + gap) shape.w = w shape.h = h shape.imgs = [img] for n in range(1, 5): img = utils.load_image( str(n) + str(ind) + '.png', True, 'shapes') shape.imgs.append(img) side = maxw if maxh > maxw: side = maxh self.colours_xy = (g.sx(.2), g.margin + g.sy(.2)) self.colours_wh = (g.sy(2.96), g.surf_h / len(colours))
def __init__(self): self.img = None self.capital = '' self.xy = None self.side = None # calculated after image loaded self.origin = (g.sx(16), g.sy(8)) self.textxy = (g.sx(16), g.sy(17))
def draw(self): self.draw_legend() utils.display_number(self.count, (g.sx(16), g.sy(8)), g.font2) img = self.scales[self.state] utils.centre_blit(g.screen, img, self.c) g.screen.blit(self.light_img, self.light_xy) g.screen.blit(self.heavy_img, self.heavy_xy) for ball in self.z: if ball.active: if ball != self.carry: img = self.ball_img(ball) utils.centre_blit(g.screen, img, ball.c) if self.carry != None: ball = self.carry img = self.ball_img(ball) x, y = g.pos x -= self.mdx y -= self.mdy utils.centre_blit(g.screen, self.shadow, (x + g.sy(.35), y + g.sy(1.1))) utils.centre_blit(g.screen, img, (x, y)) if utils.mouse_on_img(self.light_img, self.light_xy): self.drop_ball('light') if utils.mouse_on_img(self.heavy_img, self.heavy_xy): self.drop_ball('heavy') if self.result != None: utils.centre_blit(g.screen, self.result[0], self.result[1])
def click(self): # deal with click mx, my = pygame.mouse.get_pos() if self.carrying <> None: # dropping piece piece = self.carrying self.carrying = None piece.c = (mx + self.dx, my + self.dy) if self.check(piece): # check if in place self.glowing = piece self.ms = pygame.time.get_ticks() else: # check not out of sight cx, cy = piece.c h2 = self.images[piece.img_n].get_height() / 2 ylim = g.sy(21.55) if (cy - h2) > ylim: piece.c = (cx, ylim + h2 - g.sy(.4)) return True else: for ind in range(len(self.pieces) - 1, 0, -1): piece = self.pieces[ind] if not piece.done: img = self.images[piece.img_n] if utils.mouse_on_img(img, piece.c): self.top(ind) self.dx = piece.c[0] - mx self.dy = piece.c[1] - my self.carrying = piece return True return False
def display(self): g.screen.fill(utils.WHITE) if g.toc_showing: g.screen.blit(self.toc.surf, (g.offset, 0)) buttons.draw() return g.screen.blit(self.layout.surf, (g.offset, 0)) if g.jig: self.jig.draw() if g.mouse_locn: x, y = pygame.mouse.get_pos() m = '(' + str(x) + ',' + str(y) + ')' cxy = (g.sx(29), g.sy(1.2)) utils.message(g.screen, g.font1, m, cxy, 5) if g.colors: g.screen.blit(g.colors_img, g.colors_xy) if utils.mouse_on_img(g.colors_img, g.colors_xy): c = g.screen.get_at(pygame.mouse.get_pos()) r, g1, b = '0', '0', '0' if c[0] > 200: r = '255' if c[1] > 200: g1 = '255' if c[2] > 200: b = '255' m = '(' + r + ',' + g1 + ',' + b + ')' cxy = (g.sx(5.3), g.sy(19.5)) utils.message(g.screen, g.font1, m, cxy, 5) if self.layout.movie != None: self.layout.movie.draw() buttons.on(['fd', 'back']) if g.page == 1: buttons.off('back') elif g.page == g.last_page: buttons.off('fd') buttons.draw() if g.rect_on: pygame.draw.rect(g.screen, utils.GREEN, g.rect) if self.popup != None: surf, x, y = self.popup g.screen.blit(surf, (x, y))
def display(self): # called each loop g.screen.fill((255,255,192)) g.screen.blit(g.magician,(g.sx(0),g.sy(18.0))) cx=g.sx(26); cy=g.sy(5.0); utils.centre_blit(g.screen,g.target,(cx,cy)); if g.aim>0: self.display_n_glow(g.aim,(cx,cy)) x=g.sx(3);y=g.sy(3.0) pale=False if buttons.active('plus'): pale=True for i in range(len(g.top)): x=self.display_n(g.top[i],(x,y),True,False,pale);x+=g.sp1 x=g.sx(3);y=g.sy(13) for i in range(len(g.buffr)): m=g.buffr[i] if m=='plus': g.screen.blit(g.plus,(x,y));x+=g.plus.get_width() elif m=='times': g.screen.blit(g.times,(x,y));x+=g.times.get_width() elif m=='equals': g.screen.blit(g.equals,(x,y));x+=g.equals.get_width() else: x=self.display_n(g.buffr[i],(x,y)) x+=g.sp2 buttons.draw() self.slider.draw() if self.correct(): utils.centre_blit(g.screen,g.smiley,(g.sx(16),g.sy(12))) if g.score>0: utils.display_score()
def _centres(self): self.centres = [] yb = 20 posns = [(2.2, 10), (6.0, 5.75), (10, 3.8), (14, 2.95)] for i in range(4): x = g.sx(32 - posns[i][0]) y = g.sy(yb - posns[i][1]) self.centres.append((int(x), int(y))) for i in range(3): x = g.sx(posns[3 - i][0]) y = g.sy(yb - posns[3 - i][1]) self.centres.append((int(x), int(y))) for i in range(4): x = g.sx(posns[i][0]) y = g.sy(posns[i][1]) self.centres.append((int(x), int(y))) for i in range(3): x = g.sx(32 - posns[3 - i][0]) y = g.sy(posns[3 - i][1]) self.centres.append((int(x), int(y))) self.centres.reverse() self.centres = self.centres[13:] + self.centres[:13] c11 = self.centres[11] c12 = self.centres[12] c = (c12[0], c11[1]) self.centres[12] = c
def display_score(): if pygame.font: text=g.font2.render(str(g.score),True,ORANGE,BLUE) w=text.get_width(); h=text.get_height() x=g.sx(5.7); y=g.sy(18.8); d=g.sy(.3) pygame.draw.rect(g.screen,BLUE,(x-d-g.sy(.05),y-d,w+2*d,h+2*d)) g.screen.blit(text,(x,y)) centre_blit(g.screen,g.sparkle,(x-d+g.sy(.05),y+h/2-g.sy(.2)))
def buttons_setup(self): cy=g.sy(19.8) buttons.Button('new',(g.sx(30),cy),caption='new game') buttons.Button('replay',(g.sx(2),cy),caption='replay') cx=g.sx(26) buttons.Button('try',(cx,g.sy(8.5)),caption='try') buttons.Button('clear',(cx,g.sy(13)),caption='clear') buttons.Button('help',(g.sx(30),g.sy(3.3)),caption='help')
def run(self): g.init() if not self.journal: utils.load() self.tess_n = g.tess_n self.max_n = g.max_n else: g.tess_n = self.tess_n g.max_n = self.max_n self.tessl = tess.Tess(g.tesses[g.tess_n - 1]) dx = g.sy(2.4) bx = g.sx(16) - dx / 2 by = g.sy(20.2) buttons.Button("back", (bx, by), True) bx += dx buttons.Button("fd", (bx, by), True) bx = g.sx(16) by = g.sy(11) buttons.Button("solve", (bx, by), True) if self.journal: # Sugar only a, b, c, d = pygame.cursors.load_xbm('my_cursor.xbm', 'my_cursor_mask.xbm') pygame.mouse.set_cursor(a, b, c, d) going = True while going: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: if not self.journal: utils.save() going = False elif event.type == pygame.MOUSEMOTION: g.redraw = True elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw = True if event.button == 2: # centre button if not self.journal: g.version_display = not g.version_display break bu = '' if not self.tessl.carrying: bu = buttons.check() if bu <> '': self.do_button(bu) else: self.tessl.click() self.tessl.update() if g.redraw: self.display() if g.version_display: utils.version_display() pygame.display.flip() g.redraw = False tf = False if pygame.mouse.get_focused(): tf = True pygame.mouse.set_visible(tf) g.clock.tick(40)
def buttons_setup(self): dx = g.sy(2.4) bx = g.sx(16) - dx by = g.sy(20.2) buttons.Button("back", (bx, by), True) bx += dx buttons.Button("toc", (bx, by), True) bx += dx buttons.Button("fd", (bx, by), True)
def buttons_setup(self): cx=g.sx(30.25); cy=g.sy(6.5); dy=g.sy(2) buttons.Button('save',(cx,cy)); cy+=dy y1=cy; cy+=2*dy buttons.Button('load',(cx,cy)); cy+=dy y2=cy; cy+=2*dy buttons.Button('clear',(cx,cy)); cy+=dy self.save_c=(cx,y1); self.load_c=(cx,y2) if g.saves_n==0: buttons.off('load') self.load_d=g.sy(.5) # half size of number for clicking
def buttons_setup(self): cx=g.sx(1.8); cy=g.sy(18.8); dx=g.sy(3.2); dy=g.sy(2) buttons.Button('green',(cx,cy)); cx+=dx buttons.Button('yellow',(cx,cy)); cx+=dx buttons.Button('cream',(cx,cy)); cx+=dx buttons.Button('cyan',(cx,cy)) self.load_c=cx,cy+dy; cx+=dx; self.load_d=g.sy(.5) buttons.Button('orange',(cx,cy)) self.save_c=cx,cy+dy; cx+=dx buttons.Button('help',(g.sx(27.77),g.sy(2.8)))
def run(self): g.init() self.rects=rects.Rects() if not self.journal: utils.load() else: g.level=self.level self.slider=slider.Slider(g.sx(16),g.sy(20.5),10,utils.GREEN) self.new1() bx=g.sx(29.5); by=g.sy(12) buttons.Button("cyan",(bx,by),True) by=g.sy(16) buttons.Button("black",(bx,by),True) if self.journal: # Sugar only a,b,c,d=pygame.cursors.load_xbm('my_cursor.xbm','my_cursor_mask.xbm') pygame.mouse.set_cursor(a,b,c,d) going=True while going: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type==pygame.QUIT: if not self.journal: utils.save() going=False elif event.type == pygame.MOUSEMOTION: g.redraw=True elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw=True if event.button==2: # centre button if not self.journal: g.version_display=not g.version_display; break bu=buttons.check() if bu<>'': self.button(bu); break if self.slider.mouse(): self.new1(); break if self.rects.click(): if not g.magic_show: if self.rects.correct(): g.score+=g.level; g.magic_show=True buttons.off("black") if not going: break if g.redraw: self.display() if g.version_display: utils.version_display() pygame.display.flip() g.redraw=False tf=False if pygame.mouse.get_focused(): tf=True pygame.mouse.set_visible(tf) g.clock.tick(40) # be ready for xo quit at any time self.level=g.level
def buttons_setup(self): x = g.sx(3.5) y = g.sy(19) dx = g.sy(5) g.arrows_xy = (x + g.sy(5), y - g.sy(1.9)) g.score_c = (x, y) x += dx # since we dropped '0' for i in range(1, 5): buttons.Button(str(i), (x, y)) x += dx buttons.Button('new', (x, y))
def over_pan(self, ball): cx, cy = ball.c for pan in range(2): x, y = self.pan_posns[pan][0] dx, dy = 0, 0 if self.state < 2: ind = self.state * 2 + pan dx, dy = self.dxy_ud[ind] rect = pygame.Rect(x + dx - g.sy(4), y + dy - g.sy(3), g.sy(8), g.sy(4.5)) if rect.collidepoint(cx, cy): return pan return None
def run(self): g.init() if not self.journal: utils.load(); self.level=g.level else: g.level=self.level x=g.sx(26); y=g.sy(11.2) buttons.Button("new",(x,y)) x=g.sx(4); y=g.sy(10); dx=g.sy(4) buttons.Button("back",(x,y)); x+=dx buttons.Button("plus",(x,y)); x+=dx buttons.Button("times",(x,y)); x+=dx buttons.Button("equals",(x,y)) self.slider=slider.Slider(g.sx(20.5),g.sy(20.5),10,utils.GREEN) if self.journal: # Sugar only a,b,c,d=pygame.cursors.load_xbm('my_cursor.xbm','my_cursor_mask.xbm') pygame.mouse.set_cursor(a,b,c,d) self.level1() # initial animation going=True while going: self.level=g.level # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type==pygame.QUIT: if not self.journal: utils.save() going=False elif event.type == pygame.MOUSEMOTION: g.redraw=True elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw=True if event.button==2: # centre button if not self.journal: g.version_display=not g.version_display; break bu=buttons.check() if bu=='': if not self.check_numbers(): if self.slider.mouse(): self.level1() else: self.do_button(bu) # eg do_button('plus') if not going: break if g.redraw: self.display() if g.version_display: utils.version_display() pygame.display.flip() g.redraw=False tf=False if pygame.mouse.get_focused(): tf=True pygame.mouse.set_visible(tf) g.clock.tick(40)
def __init__(self): self.w = g.sy(32) self.h = g.sy(22) self.w2 = self.w / 2 self.h2 = self.h / 2 self.screen = pygame.Surface((self.w, self.h)) self.pic_n = 1 self.rc_n = 0 self.moving = True self.orange = None self.edge1, self.edge2 = g.sy(.4), g.sy(.2) # for green & white rects self.grid2 = make_grid(2, 2, self.w / 2, self.h / 2, utils.WHITE, self.edge2)
def buttons_setup(self): cx = g.sx(3.7) cy = g.sy(16.4) dx = g.sy(3.6) dy = g.sy(3.3) buttons.Button('cls', (cx, cy)) cx += dx buttons.Button('run', (cx, cy)) cx -= dx cy += dy buttons.Button('grid', (cx, cy)) cx += dx buttons.Button('hide', (cx, cy))
def display_score(): if pygame.font: text = g.font2.render(str(g.score), True, ORANGE, BLUE) w = text.get_width() h = text.get_height() x = g.sx(5.7) y = g.sy(18.8) d = g.sy(.3) pygame.draw.rect(g.screen, BLUE, (x - d - g.sy(.05), y - d, w + 2 * d, h + 2 * d)) g.screen.blit(text, (x, y)) centre_blit(g.screen, g.sparkle, (x - d + g.sy(.05), y + h / 2 - g.sy(.2)))
def setup_stars(self): self.stars = [] a = -math.pi / 2 da = math.pi * 2 / self.target x0 = self.x0 / 2 y0 = g.sy(9.5) r = g.sy(5.5) for i in range(self.target): cx = x0 + r * math.cos(a) cy = y0 + r * math.sin(a) a += da self.stars.append(Star(cx, cy)) g.smiley_c = (x0, y0)
def display(self,wait=False): g.screen.fill((0,0,100)) if wait: utils.centre_blit(g.screen,g.wait,(g.sx(16),g.sy(10))) else: self.rects.draw() buttons.draw() self.slider.draw() if g.magic_show: utils.centre_blit(g.screen,g.magic,(g.sx(27.8),g.sy(3.2))) if g.score>0: x=g.sx(29.5); y=g.sy(8) utils.display_number(g.score,(x,y),g.font2,utils.CREAM)
def setup(self): group = g.level # 1,2,3 or 4 self.nballs = group * 3 # 3,6,9,12 x_free = g.sx(3) y_free = g.sy(2.7) dy_free = g.sy(3.2) self.free_rect=pygame.Rect(x_free-self.d2,y_free-self.d2,\ 2*self.d2,3*dy_free+self.d2) i = 0 self.z = [] self.pan_n[0] = 0 self.pan_n[1] = 0 for ball in self.balls: ball.active = False ball.colour = 'blue' if i < self.nballs: if i < group: x, y = self.pan_posns[0][i] ball.pan = 0 self.pan_n[0] += 1 ball.posn = i elif i < group * 2: x, y = self.pan_posns[1][i - group] ball.pan = 1 self.pan_n[1] += 1 ball.posn = i - group else: x = x_free y = y_free y_free += dy_free ball.pan = None ball.c0 = (x, y) ball.c = (x, y) ball.active = True self.z.append(ball) i += 1 self.carry = None ind = random.randint(0, self.nballs - 1) self.rogue = self.balls[ind] self.heavy = random.randint(0, 1) self.count = 0 self.show_rogue = False self.ms = pygame.time.get_ticks() self.flutter_k = None self.flutter_ind = None self.state_save = None self.state = 2 # level self.result = None self.red_set = False # ensure exactly one red/gold per round
def display(self): # called each loop if g.big: g.screen.fill(self.colors[1]) utils.centre_blit(g.screen, g.big_surface, (g.sx(16), g.sy(11.5))) else: if self.sugar: g.screen.fill(self.colors[1]) else: g.screen.blit(g.bgd, (g.sx(0), 0)) g.screen.blit(g.box, (g.x0, g.y0)) g.screen.blit(g.box, (g.x1, g.y1)) if not self.sugar: utils.centre_blit(g.screen, g.magician, g.magician_c) self.draw_goal() utils.centre_blit(g.screen, g.turtle, (g.x0 + 4 * g.dd, g.y0 + 6 * g.dd)) self.tu.draw() if self.tu.win: if self.sugar: if self.good_job is None: path = self.parent.good_job_image_path() self.good_job = utils.load_image(path, True) if g.w > g.h: utils.centre_blit( g.screen, self.good_job, (g.sx(7), g.sy(17))) else: utils.centre_blit(g.screen, self.good_job, (g.sx(7), g.sy(38))) else: utils.centre_blit(g.screen, g.smiley, (g.sx(16.6), g.sy(2.2))) if self.sugar: self.cyan_button.set_sensitive(True) else: buttons.on('cyan') if not self.journal: utils.save() self.draw_nos() if not self.sugar: buttons.draw() self.slider.draw() if g.score > 0: if self.sugar: self.parent.update_score(int(g.score)) else: utils.display_score() utils.display_number1(g.pattern, (g.sx(2.4), g.sy(2)), g.font1, utils.WHITE)
def display_score(self): if self.sugar: self.label.set_markup( '<span><big><b> %s (%s)</b></big></span>' % ( str(int(g.score)), str(int(g.best)))) else: text = g.font2.render(str(g.score), True, utils.ORANGE, utils.BLUE) w = text.get_width() h = text.get_height() x = g.sx(5.15) y = g.sy(19.6) d = g.sy(.3) pygame.draw.rect(g.screen, utils.BLUE, (x - d, y - d, w + 2 * d, h + 2 * d - g.sy(.2))) g.screen.blit(text, (x, y)) utils.centre_blit(g.screen, g.sparkle, (x - d + g.sy(.05), y - d + g.sy(1.1)))
def buttons_setup(self): if self.sugar: self.tick_button.set_sensitive(False) self.back_button.set_sensitive(False) return dx = g.sy(4) cx = g.sx(16) - 1.5 * dx cy = g.sy(15) buttons.Button('new', (cx, cy)) cx += dx buttons.Button('tick', (cx, cy)) cx += dx buttons.off('tick') buttons.Button('back', (cx, cy)) cx += dx buttons.off('back') buttons.Button('help', (cx, cy))
def display(self): g.screen.fill(self.colors[1]) if not self.sugar: g.screen.fill((255, 255, 192)) g.screen.blit(g.magician, (g.sx(0), g.sy(18.0))) cx = g.sx(26) cy = g.sy(5.0) utils.centre_blit(g.screen, g.target, (cx, cy)) if g.aim > 0: self.display_n_glow(g.aim, (cx, cy)) x, y = g.xy1 pale = False if g.state == 2: pale = True for i in range(len(g.top)): x = self.display_n(g.top[i], (x, y), True, False, pale) x += g.sp1 x, y = g.xy3 for i in range(len(g.buffr)): m = g.buffr[i] if m == 'plus': g.screen.blit(g.plus, (x, y)) x += g.plus.get_width() elif m == 'times': g.screen.blit(g.times, (x, y)) x += g.times.get_width() elif m == 'equals': g.screen.blit(g.equals, (x, y)) x += g.equals.get_width() else: x = self.display_n(g.buffr[i], (x, y)) x += g.sp2 if len(g.top) == 0: buttons.off(['plus', 'times']) buttons.draw() if not self.sugar: self.slider.draw() if g.state == 4: utils.centre_blit(g.screen, g.smiley, (g.sx(16), g.sy(12))) if g.score > 0: self.display_score() if not self.sugar and g.best > 0: utils.text_blit( g.screen, str(g.best), g.font2, g.best_c, utils.ORANGE)
def big_pic(self): if not self.tu.running: d = g.sy(1) s = g.bw - 2 * d self.tu.draw() g.player_surface.blit( g.screen, (0, 0), (g.x1 + d, g.y0 + d, s, s)) g.big = True g.big_surface = pygame.transform.scale2x(g.player_surface)
def __init__(self, sugar, label, colors): self.sugar = sugar self.label = label self.colors = colors self.pics = [] for ind in range(10): pic = utils.load_image(str(ind) + '.png', True) self.pics.append(pic) self.back = utils.load_image('back.png', True) self.front = utils.load_image('front.png', True) self.grey = utils.load_image('grey.png', True) self.star = utils.load_image('star.png', True) self.w = self.back.get_width() self.h = self.back.get_height() self.nr = g.nr self.nc = g.nc gutter = (g.sy(32) - self.nc * self.w) / (self.nc + 1.0) / 1.5 self.locns = [] ind = 0 card = 0 y = gutter pic = True x0 = (g.w - self.nc * (self.w + gutter)) / 2 for r in range(self.nr): x = x0 for c in range(self.nc): locn = Locn(x, y, r, c, ind, card, pic) self.locns.append(locn) x += (self.w + gutter) ind += 1 card += 1 if card == 10: card = 0 pic = False y += (self.h + gutter) self.xn = x0 / 2 self.yn = y self.green = None self.tries_cxy = (g.sx(3), g.sy(20.8)) self.best_xcy = (g.sx(26.5), g.sy(20.8)) self.star_xy = (self.best_xcy[0] - self.star.get_width() - 5, g.sy(20.1)) self.gutter = gutter
def ladder(self): if g.score > g.best: g.best = g.score if g.best > 11: cx = g.sx(30.55) cy = g.sy(13.25) utils.centre_blit(g.screen, g.star, (cx, cy)) utils.display_number(g.best, (cx, cy), g.font2) if g.score > 0: n = g.score - 1 if n > 11: n = 11 g.screen.blit(g.ladder, (g.sx(26.95), g.sy(13.7))) x = g.man_x0 + n * g.man_dx y = g.man_y0 + n * g.man_dy g.screen.blit(g.man, (x, y)) cx = x + g.man_sc_dx cy = y + g.man_sc_dy if g.score < g.best or g.best < 12: utils.centre_blit(g.screen, g.star, (cx, cy)) utils.display_number(g.score, (cx, cy), g.font2)
def mouse_left(self): bu = '' cx = g.n_cx0 cy = g.n_cy0 c = None if not self.sugar: if utils.mouse_on_img1(g.magician, g.magician_c): c = (cx + 4 * g.n_dx, cy) elif buttons.mouse_on('cyan'): self.mouse_magician() return elif buttons.mouse_on('green'): if buttons.active('cyan'): bu = 'cyan' else: self.mouse_magician() return elif buttons.mouse_on('red'): bu = 'green' elif buttons.mouse_on('black'): bu = 'red' if bu != '': buttons.set_mouse(bu) return if c is None: c = (cx, cy) # default to 1st no. for i in range(5): n = g.numbers[i] if utils.mouse_on_img_rect(g.n[n - 1], (cx, cy)): c = (cx - g.n_dx, cy) break cx += g.n_dx cx, cy = c cx += g.sy(.2) cy += g.sy(1.2) c = cx, cy pygame.mouse.set_pos(c) g.pos = c return
def __init__(self, cx, cy, steps, colour=utils.BLACK): self.easy = utils.load_image('slow.png', True) self.hard = utils.load_image('fast.png', True) self.xo = utils.load_image('xo.png', True) iw = self.hard.get_width() ih = self.hard.get_height() w = g.sy(16) w2 = w / 2 h2 = ih / 2 self.x1 = cx - w2 self.y = cy - h2 self.x2 = cx + w2 - iw x = cx - w2 + iw * 1.2 w = w - 2 * iw * 1.2 h = g.sy(.12) y = cy + g.sy(.34) self.rect = pygame.Rect(x, y, w, h) mh = g.sy(1) self.mark = pygame.Rect(x, y - mh / 2 + h / 2, h, mh) self.steps = steps self.dx = w / (steps - 1) self.colour = colour self.cx = cx self.cy = cy marks = [] x = self.rect.left click_rects = [] dx = self.dx for i in range(self.steps): rect = copy.copy(self.mark) rect.left = x - 5 rect.width = 10 marks.append(rect) click_rect = pygame.Rect( x - dx / 2, self.mark.top, dx, self.mark.h) click_rects.append(click_rect) x += dx self.marks = marks self.click_rects = click_rects
def display(self): # called each loop if g.big: g.screen.fill(self.colors[1]) utils.centre_blit(g.screen, g.big_surface, (g.sx(16), g.sy(11.5))) else: if self.sugar: g.screen.fill(self.colors[1]) else: g.screen.blit(g.bgd, (g.sx(0), 0)) g.screen.blit(g.box, (g.x0, g.y0)) g.screen.blit(g.box, (g.x1, g.y1)) if not self.sugar: utils.centre_blit(g.screen, g.magician, g.magician_c) self.draw_goal() utils.centre_blit(g.screen, g.turtle, (g.x0 + 4 * g.dd, g.y0 + 6 * g.dd)) self.tu.draw() if self.tu.win: utils.centre_blit(g.screen, g.smiley, (g.sx(16.6), g.sy(2.2))) if self.sugar: self.cyan_button.set_sensitive(True) else: buttons.on('cyan') if not self.journal: utils.save() self.draw_nos() if not self.sugar: buttons.draw() self.slider.draw() if g.score > 0: if self.sugar: self.label.set_markup( '<span><big><b> %s</b></big></span>' % (str(g.score))) else: utils.display_score() utils.display_number1(g.pattern, (g.sx(2.4), g.sy(2)), g.font1, utils.BLUE)
def mouse_right(self): bu = '' if not self.sugar: if utils.mouse_on_img1(g.magician, g.magician_c): bu = 'green' if buttons.active('cyan'): bu = 'cyan' elif buttons.mouse_on('cyan'): bu = 'green' elif buttons.mouse_on('green'): bu = 'red' elif buttons.mouse_on('red'): bu = 'black' if bu != '': buttons.set_mouse(bu) return cx = g.n_cx0 cy = g.n_cy0 c = (cx, cy) # default to 1st no. if not buttons.mouse_on('black'): for i in range(5): n = g.numbers[i] if utils.mouse_on_img_rect(g.n[n - 1], (cx, cy)): if i == 4: self.mouse_magician() return c = (cx + g.n_dx, cy) break cx += g.n_dx cx, cy = c cx += g.sy(.2) cy += g.sy(1.2) c = cx, cy pygame.mouse.set_pos(c) g.pos = c return
def __init__(self): img = utils.load_image('abcd.png', True) wf = 0.0 + img.get_width() / 26.0 self.h = img.get_height() x = 0.0 y = 0 self.w = int(wf) self.imgs = [] for i in range(26): self.imgs.append(img.subsurface((int(x + .5), y, self.w, self.h))) x += wf self.x0 = g.sx(16) - 4 * self.w self.y0 = g.sy(2) self.y1 = self.y0 + 1.5 * self.h g.message_cxy = (g.sx(16), self.y0 + 1.25 * self.h) self.setup() self.ind = None
def draw(self): for locn in self.locns: if locn.state == 0: g.screen.blit(self.back, (locn.x, locn.y)) elif locn.state in (1, 3): if locn.pic or not g.words: g.screen.blit(self.pics[locn.card], (locn.x, locn.y)) else: g.screen.blit(self.front, (locn.x, locn.y)) cxy = (locn.x + self.w / 2, locn.y + self.h / 2) utils.text_blit(g.screen, words[locn.card], g.font1, cxy, utils.BLACK, False) if self.green is not None: pygame.draw.rect(g.screen, self.colors, (self.green.x, self.green.y, self.w, self.h), 5) if self.tries > 0: if not self.sugar: utils.display_number(self.tries, self.tries_cxy, g.font2, utils.CREAM) if g.best > 0 and not self.sugar: g.screen.blit(self.star, (self.star_xy)) utils.display_number1(g.best, self.best_xcy, g.font2, utils.ORANGE) if self.sugar: self.label.set_markup( '<span><big><b> %s (%s)</b></big></span>' % ( str(int(float(self.tries))), str(int(float(g.best))))) if len(self.clicks) == 3: locn = self.clicks[0] g.screen.blit(self.grey, (locn.x, locn.y)) if g.numbers: x = self.xn y = self.h / 2 + self.gutter for r in range(self.nr): n = self.nr - r utils.display_number(n, (x, y), g.font1, utils.CREAM) y += self.h + self.gutter x = self.xn * 2 + self.w / 2 y = self.yn + g.sy(.4) for c in range(self.nc): n = c + 1 utils.display_number(n, (x, y), g.font1, utils.CREAM) x += self.w + self.gutter
def run(self, restore=False): self.black = False g.init() if not self.journal: utils.load() load_save.retrieve() x = g.sx(26) y = g.sy(11.2) if not self.sugar: buttons.Button("new", (x, y)) x, y = g.cxy2 dx = g.sy(4) self.back_button = buttons.Button("back", (x, y)) x += dx buttons.Button("plus", (x, y)) x += dx buttons.Button("times", (x, y)) x += dx buttons.Button("equals", (x, y)) self.ops = ['back', 'plus', 'times', 'equals'] if not self.sugar: self.slider = slider.Slider(g.sx(22.4), g.sy(20.5), 10, utils.GREEN) self.mouse_auto = True self.anim_ms = None self.level1() # initial animation self.scored = False if restore: g.score = self.save_score ctrl = False pygame.key.set_repeat(600, 120) key_ms = pygame.time.get_ticks() going = True if self.canvas is not None: self.canvas.grab_focus() while going: if self.journal: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: if not self.journal: utils.save() going = False elif event.type == pygame.MOUSEMOTION: g.pos = event.pos g.redraw = True if self.canvas is not None: self.canvas.grab_focus() elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw = True self.anim_end() if event.button == 1: bu = buttons.check() if bu == '': if not self.check_numbers(): if not self.sugar: if self.slider.mouse(): self.level1() else: self.do_button(bu) # eg do_button('plus') self.flush_queue() elif event.type == pygame.KEYDOWN: self.anim_end() # throttle keyboard repeat if pygame.time.get_ticks() - key_ms > 110: key_ms = pygame.time.get_ticks() if ctrl: if event.key == pygame.K_q: if not self.journal: utils.save() going = False break else: ctrl = False if event.key in (pygame.K_LCTRL, pygame.K_RCTRL): ctrl = True break self.do_key(event.key) g.redraw = True self.flush_queue() elif event.type == pygame.KEYUP: ctrl = False if not going: break self.animation() if g.redraw: self.display() if g.version_display: utils.version_display() if not self.black: g.screen.blit(g.pointer, g.pos) pygame.display.flip() g.redraw = False g.clock.tick(40)
def buttons_setup(self): if not self.sugar: buttons.Button('new', (g.sx(16), g.sy(20.8)))
def run(self, restore=False): self.g_init() if not self.journal: utils.load() load_save.retrieve() self.aim = simon.Simon(1200) # arg is glow time if self.sugar: self.set_delay(800) else: self.set_delay() self.player = simon.Simon(200) if restore: self.restore_pattern() self.aim.started = True if self.sugar: self.green_button.set_sensitive(True) self.back_button.set_sensitive(False) else: bx = g.sx(22.42) by = g.sy(20.8) buttons.Button('green', (bx, by), True) buttons.Button('back', (bx, by), True) buttons.off('back') self.slider = slider.Slider(g.sx(9), g.sy(20.8), 3, utils.BLUE) self.rc = rc_skip_last.RC(3, 5) if self.canvas is not None: self.canvas.grab_focus() ctrl = False pygame.key.set_repeat(600, 120) key_ms = pygame.time.get_ticks() going = True while going: if self.journal: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: if not self.journal: utils.save() going = False elif event.type == pygame.MOUSEMOTION: g.pos = event.pos g.redraw = True if self.canvas is not None: self.canvas.grab_focus() self.mouse_set() elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw = True if event.button == 1: self.do_click() self.flush_queue() elif event.type == pygame.KEYDOWN: # throttle keyboard repeat if pygame.time.get_ticks() - key_ms > 110: key_ms = pygame.time.get_ticks() if ctrl: if event.key == pygame.K_q: if not self.journal: utils.save() going = False break else: ctrl = False if event.key in (pygame.K_LCTRL, pygame.K_RCTRL): ctrl = True break self.do_key(event.key) g.redraw = True self.flush_queue() elif event.type == pygame.KEYUP: ctrl = False if not going: break if self.sugar: if g.player_n == 0 and not self.green_button.get_sensitive(): self.back_button.set_sensitive(True) else: if g.player_n == 0 and not buttons.active('green'): buttons.on('back') self.player.do() self.aim.do() if g.redraw: self.display() if g.version_display: utils.version_display() if self.aim.running or self.aim.glow_active: pass else: g.screen.blit(g.pointer, g.pos) pygame.display.flip() g.redraw = False g.clock.tick(40)
def run(self, restore=False): self.g_init() if not self.journal: utils.load() load_save.retrieve() if restore: self.restore_pattern() else: g.delay = (3 - g.level) * 400 self.tu = my_turtle.TurtleClass() self.tu.current = [1, 1, 1, 3, 2] self.get_goal() if g.pattern == 1: self.tu.current = utils.copy_list(g.goal) self.tu.setup(self.colors[0]) g.numbers = utils.copy_list(self.tu.current) #buttons x = g.sx(7.3) y = g.sy(16.5) dx = g.sy(2.6) if not self.sugar: buttons.Button("cyan", (x, y), True) x += dx buttons.off('cyan') buttons.Button("green", (x, y), True) x += dx buttons.Button("red", (x, y), True) x += dx buttons.Button("black", (x, y), True) x += dx self.slider = slider.Slider(g.sx(23.5), g.sy(21), 3, utils.YELLOW) self.mouse_1st_no() # to 1st number if self.canvas is not None: self.canvas.grab_focus() ctrl = False pygame.key.set_repeat(600, 120) key_ms = pygame.time.get_ticks() going = True while going: if self.journal: # Pump GTK messages. while gtk.events_pending(): gtk.main_iteration() # Pump PyGame messages. for event in pygame.event.get(): if event.type == pygame.QUIT: if not self.journal: utils.save() going = False elif event.type == pygame.MOUSEMOTION: g.pos = event.pos g.redraw = True if self.canvas is not None: self.canvas.grab_focus() elif event.type == pygame.MOUSEBUTTONDOWN: g.redraw = True if g.big: g.big = False else: bu = buttons.check() if bu != '': self.do_button(bu) self.flush_queue() elif not self.sugar: if utils.mouse_on_img1(g.magician, g.magician_c): self.help2() elif utils.mouse_in(g.x1, g.y0, g.x1 + g.bw, g.y0 + g.bw): self.big_pic() elif self.slider.mouse(): g.delay = (3 - g.level) * 400 else: g.show_help = False self.check_nos(event.button) else: g.show_help = False self.check_nos(event.button) elif event.type == pygame.KEYDOWN: # throttle keyboard repeat if pygame.time.get_ticks() - key_ms > 110: key_ms = pygame.time.get_ticks() if ctrl: if event.key == pygame.K_q: if not self.journal: utils.save() going = False break else: ctrl = False if event.key in (pygame.K_LCTRL, pygame.K_RCTRL): ctrl = True break self.do_key(event.key) g.redraw = True self.flush_queue() elif event.type == pygame.KEYUP: ctrl = False if not going: break if self.tu.running: self.tu.move() if not g.crash_drawn: g.crash_drawn = True g.redraw = True if g.redraw: self.display() if g.version_display: utils.version_display() g.screen.blit(g.pointer, g.pos) pygame.display.flip() g.redraw = False g.clock.tick(40)
def mouse_1st_no(self): c = g.n_cx0 + g.sy(.2), g.n_cy0 + g.sy(1.2) pygame.mouse.set_pos(c) g.pos = c