def setScreen(n=0): if n: global screen screen = pg.display.set_mode(size) pg.display.set_caption('Quad Game') screen.fill(BG) drawButton(buttons,0) titleFont = pg.font.SysFont('Arial',75) t = 'Quad Game' s = titleFont.size(t) label = titleFont.render(t,1,FG) screen.blit(label,(width/2.0-s[0]/2.0,50)) Fade.fadein(screen.copy(),screen) pg.display.update()
def main(): global flagged,score,size,xgrid,ygrid,maxx,maxy,numberOfMines,screen,font global startTime,won startTime = time.time() score = won = 0 size = 40 xgrid,ygrid = 25,20 maxx,maxy = size*xgrid,size*ygrid numberOfMines = 75 screen = pg.display.set_mode((size*xgrid, size*ygrid)) pg.display.set_caption('Minesweeper!') # constants running = True mousex,mousey = 0,0 mouseDown = False turn = (255,0,0) font = pg.font.SysFont("monospace",25) gameOver = False # initial surface screen.fill(WHITE) mines = [] first = 1 flagged = [] # main loop while running or gameOver: # event handling for ev in pg.event.get(): if (ev.type == KEYDOWN and (ev.key == K_ESCAPE or ev.key == K_q)): running=False elif ev.type == QUIT: Fade.fadeout(screen.copy(),screen,0.0,1.0) pg.quit() sys.exit() if (ev.type == MOUSEBUTTONDOWN or ev.type == KEYDOWN) and gameOver: running=False gameOver = False elif ev.type == MOUSEBUTTONDOWN: button = pg.mouse.get_pressed() # actual clicks (l-click) if button[0]: if ev.pos[0] < maxx and ev.pos[1] < maxy: mouseDown = True mousex,mousey = ev.pos # for flagging (r-click) elif button[2]: coords = [(int(ev.pos[0]/int(maxx/xgrid))*int(maxx/xgrid)), (int(ev.pos[1]/int(maxy/ygrid))*int(maxy/ygrid))] x,y = int(coords[0]/size),int(coords[1]/size) # unflagging if tuple(coords) in flagged: flagged.remove(tuple(coords)) pg.draw.rect(screen,(255,255,255),(coords,(maxx/xgrid,maxy/ygrid))) pg.display.update() # flagging else: flagged.append(tuple(coords)) pg.draw.rect(screen,(255,255,0),(coords,(maxx/xgrid,maxy/ygrid))) pg.display.update() # mouse up/down if mouseDown and running: mouseDown = 0 # on first to make sure you don't click a mine if mines == []: mines = generateMines(screen,[mousex,mousey]) # don't try to fix these lines up pg.draw.rect(screen,GRAY,([(int(mousex/int(maxx/xgrid))*int(maxx/xgrid)), (int(mousey/int(maxy/ygrid))*int(maxy/ygrid))],(maxx/xgrid,maxy/ygrid))) screen.blit(font.render(clickSquare(screen,mousex,mousey,mines,font,True),1,(0,0,0)),((int(mousex/int(maxx/xgrid))*int(maxx/xgrid)) + 6*maxx/(xgrid*18),(int(mousey/int(maxy/ygrid))*int(maxy/ygrid)) + 4*maxy/(ygrid*18))) running = clickSquare(screen,mousex,mousey,mines,font) if not running: gameOver = True elif running == 2: won = 1 running = 0 gameOver = 1 # check if game is still running if running: drawGrid(screen) if first: # initial fade Fade.fadein(screen.copy(),screen) first = 0 pg.display.flip() # quit if the games won, or the game is over if gameOver or won: quitProgramDisplayMines(screen,mines) gameOver = running = False sc = ScoreScreen(screen.copy(),0,time=int(time.time()-startTime),win=won)
def main(): global BG,FG,screen,width,height,font BG = (0,0,0) FG = (255,255,255) # init pg.init() size = width, height = 800,800 screen = pg.display.set_mode(size) pg.display.set_caption('Pong') clock = pg.time.Clock() font = pg.font.SysFont('Arial',40) screen.fill(BG) drawLine() ball = Ball(int(width/2.0),int(height/2.0),12,FG,[1 if randint(0,1) is 1 else -1,1 if randint(0,1) is 1 else -1],width,height) p1 = Paddle(0,height/2.0-3*height/40.0,width/25.0,3*height/20.0,FG,height) p2 = Paddle(width-width/25.0,height/2.0-3*height/40.0,width/25.0,3*height/20.0,FG,height) drawBall(ball) drawPaddle(p1) drawPaddle(p2) Fade.fadein(screen.copy(),screen) pg.display.flip() s1,s2 = 0,0 displayScore(s1,s2) inc = 20 running = 1 started = scoreUpdated = 0 while running: # event handling for ev in pg.event.get(): # non-game keys pressed if ev.type is KEYDOWN: # quit if ev.key is K_ESCAPE or ev.key is K_q: running = False # reset elif ball.gameOver(): screen.fill(BG) displayScore(s1,s2,1) drawLine() ball = Ball(int(width/2.0),int(height/2.0),12,FG,[1 if randint(0,1) is 1 else -1,1 if randint(0,1) is 1 else -1],width,height) p1 = Paddle(0,height/2.0-3*height/40.0,width/25.0,3*height/20.0,FG,height) p2 = Paddle(width-width/25.0,height/2.0-3*height/40.0,width/25.0,3*height/20.0,FG,height) drawBall(ball) drawPaddle(p1) drawPaddle(p2) if running: pg.display.flip() started = scoreUpdated = 0 # start elif not started: started = 1 ball.resetDirection() # quit if ev.type is QUIT: Fade.fadeout(screen.copy(),screen,0.0,1.0) pg.quit() sys.exit() # if the games going on if not ball.gameOver() and started and running: displayScore(s1,s2,1) # check, update, draw ball drawBall(ball.checkIfHit(p1,p2).update()) # redraw if hit, in case for p in p1,p2: if p.wasHit(): pg.display.update(drawPaddle(p).getwholerect()) # check for up/down/w/s keys pressed keys = pg.key.get_pressed() if 1 in keys[:300]: if keys[K_w]: drawPaddle(p1.moveByY(-inc)) elif keys[K_s]: drawPaddle(p1.moveByY(inc)) if keys[K_UP]: drawPaddle(p2.moveByY(-inc)) elif keys[K_DOWN]: drawPaddle(p2.moveByY(inc)) displayScore(s1,s2) drawLine() pg.display.update() clock.tick(90) if ball.gameOver() and not scoreUpdated: scoreUpdated = 1 if ball.getx() < width/2.0: s2 += 1 else: s1 += 1 displayScore(s1,s2,1) pg.display.update() if s1 >= 7 or s2 >= 7: running = 0 score = [0,0] winner = 0 if s1 >= 7: winner = 1 elif s2 >= 7: winner = 2 sc = ScoreScreen(screen.copy(),1,scores=(s1,s2),winner=winner)
for b in buttons: if not b.mousedOver() and b.checkIfOnButton(ev.pos): b.setMousedOver(1) drawButton(b,1) break elif b.mousedOver() and not b.checkIfOnButton(ev.pos): b.setMousedOver(0) drawButton(b,0) break # clicking on button, launching game if ev.type == MOUSEBUTTONDOWN: for b in buttons: if b.checkIfOnButton(ev.pos): program = b.getprogram() break if program: p = program Fade.fadeout(screen.copy(),screen,0.0,1.0) program.main() setScreen() program = None pg.display.update() clock.tick(60) Fade.fadeout(screen.copy(),screen,0.0,1.0) pg.quit() sys.exit()
def main(): # init pg.init() screen = pg.display.set_mode((w,h)) pg.display.set_caption('Snake') screen.fill(BLACK) x,y = (int(((w-s)/2)/s))*s,(int(((h-s)/2)/s))*s gameOver = 0 dropCoords = [(int((randint(0,w-s))/s))*s,(int((randint(0,h-s))/s))*s] clock = pg.time.Clock() font = pg.font.SysFont('Arial',20) # up,down,left,right leftRight = [0,0] upDown = [0,0] boxes = [[x,y]] drawSnake(screen,boxes) Fade.fadein(screen.copy(),screen) direction = [0,0] running=True randomColor = RANDOMCOLOR() justDropped = 0 score = 0 paused = 0 while running: if paused: paused = 0 a=1 while a: key = pg.key.get_pressed() if not key[K_p]: a=0 a=1 while 1: key = pg.key.get_pressed() print('now here') if key[K_p]: a=0 a=1 while 1: key = pg.key.get_pressed() if not key[K_p]: a=0 oldUpDown = upDown oldLeftRight = leftRight # event handling for ev in pg.event.get(): if ev.type==QUIT: Fade.fadeout(screen.copy(),screen,0.0,1.0) pg.quit() sys.exit() elif ev.type == KEYDOWN: k = ev.key if k == K_p: paused = 1 if k == K_q or k == K_ESCAPE: running=False if k == K_LEFT or k == K_a: if leftRight[1]: break else: leftRight=[1,0] upDown = [0,0] elif k == K_RIGHT or k == K_d: if leftRight[0]: break else: leftRight=[0,1] upDown = [0,0] if k == K_DOWN or k == K_s: if upDown[0]: break else: upDown=[0,1] leftRight = [0,0] elif k == K_UP or k == K_w: if upDown[1]: break else: upDown=[1,0] leftRight = [0,0] # for a weird bug if (oldUpDown == upDown[::-1] and oldUpDown != [0,0]): upDown = oldUpDown if (oldLeftRight == leftRight[::-1] and oldLeftRight != [0,0]): leftRight = oldLeftRight # hitting itself if len(boxes)>3: for b in boxes: if boxes.index(b) != len(boxes)-1-boxes[::-1].index(b): gameOver = 1 drawSnake(screen,[boxes[0]],RED) # draw red square drawSnake(screen,boxes,None) # outline all squares in red pg.display.update() # check if player ran over the drop if dropCoords in [x for x in boxes]: score+=1 boxes.insert(1,dropCoords) a,b = (int((randint(0,w-s))/s))*s,(int((randint(0,h-s))/s))*s randomColor = RANDOMCOLOR() drawSnake(screen,[[a,b]],randomColor) dropCoords = [a,b] justDropped = 1 # if the game is over if x>=w or x+s<=0 or y+s<=0 or y>=h: gameOver=1 drawSnake(screen,boxes,None) pg.display.flip() if gameOver: running = 0 # move values down i = len(boxes)-1 if (len(boxes) > 2 and not justDropped) else 0 while i >= 1: boxes[i] = list(boxes[i-1]) i-=1 justDropped = 0 if 1 in upDown+leftRight: if len(boxes) <= 2: boxes.insert(1,[x,y]) if upDown[1]: y+=s boxes[0][1] += s elif upDown[0]: y-=s boxes[0][1] -= s if leftRight[0]: x-=s boxes[0][0] -= s elif leftRight[1]: x+=s boxes[0][0] += s if running: screen.fill(BLACK) updateScore(screen,font,score,0) drawSnake(screen,[dropCoords],randomColor) drawSnake(screen,boxes) updateScore(screen,font,score,1) if not gameOver: pg.display.flip() clock.tick(18) sc = ScoreScreen(screen.copy(),2,score=score)
def main(): global screen,board,locked,coords,puzzle,font,fontSize from . import generator puzzle = generator.main() global startTime,won startTime = time.time() won = 0 pg.init() size = width,height = int(200/2*N),int(200/2*N) xunit,yunit = int(width/N),int(height/N) screen = pg.display.set_mode((width+int(T/2),height+int(T/2))) # +T to account for thick lines pg.display.set_caption('Sudoku') screen.fill(BG) drawLines(width,height) # clock for ticking, font clock = pg.time.Clock() font = pg.font.SysFont('Arial',63) fontSize = font.size('1') # create coordinates dictionary # takes a tuple of coords (1-9,1-9) # ready to use with drawing fonts/rects coords = {} for i,y in enumerate(range(0,height,yunit)): for j,x in enumerate(range(0,width,xunit)): a = x + xunit/2.0 - fontSize[0]/2.0 + int(j/3) # x + halfunitx - halfselfx + accountForThickLines b = y + yunit/2.0 - fontSize[1]/2.0 + int(i/3) # CHANGE LAST COMPONENT FOR MINOR CHANGES IF NEEDED, WITH SIZING coords[(j,i)] = a,b # board: # locked: which numbers are correct (there from start, maybe other use later) # 2d lists.... board[2][0] == 3rd from the left, top row board = [[0]*N for i in range(N)] locked = [[0]*N for i in range(N)] # initial board for i in range(9): for j in range(9): if puzzle[i][j]: drawNum(puzzle[i][j],(i,j),1) Fade.fadein(screen.copy(),screen) pg.display.flip() running = darkBlinkingLine = True # main loop : blinking = picked = False # blinking line : already picked a number at pos pos=oldPos=c=0 while running: # event handling for ev in pg.event.get(): # keep track of clicks, blinking if ev.type == MOUSEBUTTONDOWN: oldPos,pos = pos,(int(ev.pos[0]/xunit),int(ev.pos[1]/yunit)) blinking = True c,darkBlinkingLine=79,1 picked = False # for clicking a little bit too far if pos[0] > 8: pos = (8,pos[1]) if pos[1] > 8: pos = (pos[0],8) # quit elif ev.type == KEYDOWN and (ev.key == K_ESCAPE or ev.key == K_q): running = False elif ev.type == QUIT: Fade.fadeout(screen.copy(),screen,0.0,1.0) pg.quit() sys.exit() # draw number if number pressed, mouse clicked if ev.type == KEYDOWN: # for numbers if 49 <= ev.key <= 57 and not picked: picked = True blinking = False drawNum(ev.key-48,pos) # -48 to index it from 0-8 if checkWin(): win() won = 1 elif ev.key == K_BACKSPACE: drawNum(0,pos) # arrow keys to navigate around if (blinking or picked) and ev.key in [K_UP,K_DOWN,K_LEFT,K_RIGHT]: drawNum(board[pos[0]][pos[1]],pos) oldPos=pos c,darkBlinkingLine=79,1 blinking,picked=True,False # position changing if ev.key == K_UP: pos = (pos[0],pos[1]-1) elif ev.key == K_DOWN: pos = (pos[0],pos[1]+1) elif ev.key == K_RIGHT: pos = (pos[0]+1,pos[1]) elif ev.key == K_LEFT: pos = (pos[0]-1,pos[1]) # correct going past allowed values if pos[0] > 8: pos = (8,pos[1]) elif pos[0] < 0: pos = (0,pos[1]) if pos[1] > 8: pos = (pos[0],8) elif pos[1] < 0: pos = (pos[0],0) # for blinking if blinking: c+=1 if c%80==0: blink(board,pos,oldPos,darkBlinkingLine) darkBlinkingLine = 0 if darkBlinkingLine else 1 c=0 # update, regulate fps for blinking pg.display.flip() clock.tick(100) score = None sc = ScoreScreen(screen.copy(),3,time=time.time()-startTime,win=won)