def checkForKeyPress(): if kmodule.detect() != '': return True for event in pygame.event.get(): if event.type == QUIT: #event is quit terminate() elif event.type == KEYDOWN: if event.key == K_ESCAPE: #event is escape key terminate() else: return event.key #key found return with it # no quit or key events in queue so return None return None
def renderblocks_play(): myfont = pygame.font.Font(None, 30) global keybrightness kmodule.screen.fill((0, 0, 0)) bluespace = pygame.rect.Rect(0, kmodule.screenh * 0.5, kmodule.screenw, kmodule.screenh / 7) if keybrightness > 100: keybrightness -= 10 if keybrightness < 100: keybrightness += 2 pygame.draw.rect(kmodule.screen, (255, keybrightness, keybrightness), bluespace) keylen = [100, 80, 60, 40, 20, 40, 60, 80, 100, 120] keylen = [120, 100, 80, 60, 40, 20, 40, 60, 80, 100] for key in range(10): pygame.draw.rect( kmodule.screen, (100, 100, 100), ((key + 1) * kmodule.screenw * 0.075 + 50, 0, kmodule.screenw * 0.025, kmodule.screenh - keylen[key])) p = myfont.render(str(key + 1), 1, (200, 200, 255)) kmodule.screen.blit(p, ((key + 1) * kmodule.screenw * 0.075 + 50, kmodule.screenh * 0.75)) for note in kmodule.notes: pygame.draw.circle(kmodule.screen, (255, 255, 255), note.center, kmodule.screenw / 60) note.y += kmodule.screenh / 300 if note.y > kmodule.screenh * 0.5: kmodule.notes.remove(note) kmodule.port.send(kmodule.msgs[0]) kmodule.port.send( Message(type='note_off', channel=kmodule.msgs[0].channel, note=kmodule.msgs[0].note)) kmodule.msgs.pop(0) keybrightness = 30 detect = kmodule.detect() if detect != '': keybrightness = 255 kmodule.port.send( Message('note_on', note=kmodule.mapping.keys()[ kmodule.mapping.values().index(detect)], time=10)) kmodule.port.send( Message('note_off', note=kmodule.mapping.keys()[ kmodule.mapping.values().index(detect)], time=10)) pygame.display.flip()
def detectwin(leftscore, rightscore, font): if leftscore > 10 or rightscore > 10: if leftscore > 10: wintext = font.render('Left Wins!', 1, (255, 255, 255)) elif rightscore > 10: wintext = font.render('Right Wins!', 1, (255, 255, 255)) screen.blit(wintext, (250, 150)) pygame.display.flip() restart = False leftscore = 0 rightscore = 0 while True: if kmodule.detect() == 5: return leftscore, rightscore for event in pygame.event.get(): if event.type == KEYDOWN: return leftscore, rightscore elif event.type == QUIT: pygame.quit() else: return leftscore, rightscore
def main(): screen = pygame.display.set_mode((600, 400)) pygame.display.set_caption('Pong') global ball, balldirx, balldiry, leftscore, rightscore, t, wallbounce, rebounce, detectscore, detectwin, moveball, leftpaddle, rightpaddle, ballshadow, screen leftdir = '' rightdir = '' pongfont = pygame.font.Font(None, 50) screen.fill((20, 20, 20)) screen.blit( pongfont.render('Pong - Press a key to start', 1, (255, 255, 255)), (50, 100)) pygame.display.flip() start = False while True: if kmodule.detect() != '': break for event in pygame.event.get(): if event.type == KEYUP: start = True if start: break while True: screen.fill((20, 20, 20)) balldiry = wallbounce(ball, balldiry) balldirx, balldiry = rebounce(ball, leftpaddle, balldirx, balldiry) balldirx, balldiry = rebounce(ball, rightpaddle, balldirx, balldiry) ballshadow.append(ball) #if len(ballshadow) > 2000: # ballshadow.pop(0) for x in ballshadow: pygame.draw.rect(screen, (50, 50, 50), x) ball = moveball(ball, balldirx, balldiry) rightscore, leftscore = detectscore(ball, rightscore, leftscore) pygame.draw.rect(screen, (255, 255, 255), ball) pygame.draw.rect(screen, (255, 255, 255), leftpaddle) pygame.draw.rect(screen, (255, 255, 255), rightpaddle) scr = pongfont.render(str(leftscore), 1, (255, 255, 255)) screen.blit(scr, (220, 50)) scr = pongfont.render(str(rightscore), 1, (255, 255, 255)) screen.blit(scr, (350, 50)) presses = kmodule.constdetect() if presses != '': if presses == 1 and leftpaddle.y > 0: leftdir = 'up' elif presses == 2 and leftpaddle.y < 350: leftdir = 'down' elif presses == 9 and rightpaddle.y > 0: rightdir = 'up' elif presses == 10 and rightpaddle.y < 350: rightdir = 'down' else: leftdir = '' rightdir = '' if leftdir == 'up' and leftpaddle.y > 0: leftpaddle.y -= 20 elif leftdir == 'down' and leftpaddle.y < 350: leftpaddle.y += 20 elif rightdir == 'up' and rightpaddle.y > 0: rightpaddle.y -= 20 elif rightdir == 'down' and rightpaddle.y < 350: rightpaddle.y += 20 leftscore, rightscore = detectwin(leftscore, rightscore, pongfont) for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_w and leftpaddle.y > 0: leftdir = 'up' elif event.key == K_s and leftpaddle.y < 350: leftdir = 'down' elif event.key == K_UP and rightpaddle.y > 0: rightdir = 'up' elif event.key == K_DOWN and rightpaddle.y < 350: rightdir = 'down' elif event.type == KEYUP: leftdir = '' rightdir = '' elif event.type == QUIT: return t.tick(20) pygame.display.flip()
def main(): global r1,r2,r3,screen t = pygame.time.Clock() r1 = [0,0,0] r2 = [0,0,0] r3 = [0,0,0] pygame.display.set_caption('512') screen = pygame.display.set_mode((600,600)) def getNewPiece(): global r1,r2,r3 rows = [r1,r2,r3] while True: row = randint(0,2) col = randint(0,2) if rows[row][col] == 0: rows[row][col] = 2 print r1,r2,r3 return r1,r2,r3 def drawSquares(): global r1,r2,r3 row = 0 global screen screen.fill((0,0,0)) colors = {0:(100,100,100),2:(200,200,200),4:(200,100,100),8:(255,200,0),16:(100,200,100),32:(200,0,100),64:(100,0,200),128:(100,100,255),256:(255,100,100),512:(0,0,0)} squarefont = pygame.font.Font(None,100) for x in [r1,r2,r3]: col = 0 for y in x: color = colors[y] pygame.draw.rect(screen,color,((col*180)+20,(row*180)+20,140,140)) screen.blit(squarefont.render(str(y),1,(255,255,255)),((col*180)+40,(row*180)+40)) col += 1 row += 1 pygame.display.update() def moveUp(): global r1,r2,r3 possible = False for x in range(3): if r1[x] == 0: #Move one space up r1[x] = r2[x] r2[x] = 0 possible = True elif r1[x] == r2[x]: #Merge r1[x] *= 2 r2[x] = 0 possible = True if r2[x] == 0: r2[x] = r3[x] r3[x] = 0 possible = True elif r2[x] == r3[x]: r2[x] *= 2 r3[x] = 0 possible = True if possible: getNewPiece() return r1,r2,r3 def moveDown(): global r1,r2,r3 possible = False for x in range(3): if r3[x] == 0: #Move one space down r3[x] = r2[x] r2[x] = 0 possible = True elif r3[x] == r2[x]: #Merge r3[x] *= 2 r2[x] = 0 possible = True if r2[x] == 0: r2[x] = r1[x] r1[x] = 0 possible = True elif r2[x] == r1[x]: r2[x] *= 2 r1[x] = 0 possible = True if possible: getNewPiece() return r1,r2,r3 def moveRight(): global r1,r2,r3 possible = False for x in [r1,r2,r3]: if x[2] == 0: x[2] = x[1] x[1] = 0 possible = True elif x[1] == x[2]: x[2] *= 2 x[1] = 0 possible = True if x[1] == 0: x[1] = x[0] x[0] = 0 possible = True elif x[0] == x[1]: x[1] *= 2 x[0] = 0 possible = True if possible: getNewPiece() def moveLeft(): global r1,r2,r3 possible = False for x in [r1,r2,r3]: if x[0] == 0: x[0] = x[1] x[1] = 0 possible = True elif x[0] == x[1]: x[0] *= 2 x[1] = 0 possible = True if x[1] == 0: x[1] = x[2] x[2] = 0 possible = True elif x[1] == x[2]: x[1] *= 2 x[2] = 0 possible = True if possible: getNewPiece() t = pygame.time.Clock() while True: drawSquares() detect = kmodule.detect() if detect == 7: moveLeft() elif detect == 8: moveUp() elif detect == 9: moveDown() elif detect == 10: moveRight() elif detect == 1: main() elif detect == 3: import Arcade Arcade.main() for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_UP: for x in range(3): moveUp() #getNewPiece() elif event.key == K_DOWN: for x in range(3): moveDown() #getNewPiece() elif event.key == K_RIGHT: moveRight() #getNewPiece() elif event.key == K_LEFT: moveLeft() #getNewPiece() elif event.key == K_r: main() elif event.type == QUIT: import Arcade Arcade.main() t.tick(20)
def runGame(): # setup variables for the start of the game board = getBlankBoard() lastMoveDownTime = time.time() lastMoveSidewaysTime = time.time() lastFallTime = time.time() movingDown = False # note: there is no movingUp variable movingLeft = False movingRight = False score = 0 level, fallFreq = calculateLevelAndFallFreq(score) fallingPiece = getNewPiece() nextPiece = getNewPiece() while True: # game loop if fallingPiece == None: # No falling piece in play, so start a new piece at the top fallingPiece = nextPiece nextPiece = getNewPiece() lastFallTime = time.time() # reset lastFallTime if not isValidPosition(board, fallingPiece): return # can't fit a new piece on the board, so game over detection = kmodule.detect() constdetection = kmodule.constdetect() if constdetection == 7 and isValidPosition( board, fallingPiece, adjX=-1): fallingPiece['x'] -= 1 movingLeft = True movingRight = False lastMoveSidewaysTime = time.time() if constdetection == 10 and isValidPosition( board, fallingPiece, adjX=1): fallingPiece['x'] += 1 movingRight = True movingLeft = False lastMoveSidewaysTime = time.time() if detection == 8: fallingPiece['rotation'] = (fallingPiece['rotation'] + 1) % len( PIECES[fallingPiece['shape']]) if not isValidPosition(board, fallingPiece): fallingPiece['rotation'] = (fallingPiece['rotation'] - 1) % len( PIECES[fallingPiece['shape']]) if constdetection == 9: movingDown = True if isValidPosition(board, fallingPiece, adjY=1): fallingPiece['y'] += 1 lastMoveDownTime = time.time() if detection == 6: movingDown = False movingLeft = False movingRight = False for i in range(1, BOARDHEIGHT): if not isValidPosition(board, fallingPiece, adjY=i): break fallingPiece['y'] += i - 1 if detection == '' or constdetection == '': movingLeft = False movingRight = False movingDown = False checkForQuit() for event in pygame.event.get(): # event handling loop if event.type == KEYUP: if (event.key == K_p): # Pausing the game screen.fill(BGCOLOR) #pygame.mixer.music.stop() showTextScreen('Paused') # pause until a key press #pygame.mixer.music.play(-1, 0.0) lastFallTime = time.time() lastMoveDownTime = time.time() lastMoveSidewaysTime = time.time() elif (event.key == K_LEFT or event.key == K_a): movingLeft = False elif (event.key == K_RIGHT or event.key == K_d): movingRight = False elif (event.key == K_DOWN or event.key == K_s): movingDown = False elif event.type == KEYDOWN: # moving the piece sideways if (event.key == K_LEFT or event.key == K_a or detection == 6) and isValidPosition( board, fallingPiece, adjX=-1): fallingPiece['x'] -= 1 movingLeft = True movingRight = False lastMoveSidewaysTime = time.time() elif (event.key == K_RIGHT or event.key == K_d or detection == 9) and isValidPosition(board, fallingPiece, adjX=1): fallingPiece['x'] += 1 movingRight = True movingLeft = False lastMoveSidewaysTime = time.time() # rotating the piece (if there is room to rotate) elif (event.key == K_UP or event.key == K_w or detection == 7): fallingPiece['rotation'] = ( fallingPiece['rotation'] + 1) % len( PIECES[fallingPiece['shape']]) if not isValidPosition(board, fallingPiece): fallingPiece['rotation'] = ( fallingPiece['rotation'] - 1) % len( PIECES[fallingPiece['shape']]) elif (event.key == K_q): # rotate the other direction fallingPiece['rotation'] = ( fallingPiece['rotation'] - 1) % len( PIECES[fallingPiece['shape']]) if not isValidPosition(board, fallingPiece): fallingPiece['rotation'] = ( fallingPiece['rotation'] + 1) % len( PIECES[fallingPiece['shape']]) # making the piece fall faster with the down key elif (event.key == K_DOWN or event.key == K_s or detection == 8): movingDown = True if isValidPosition(board, fallingPiece, adjY=1): fallingPiece['y'] += 1 lastMoveDownTime = time.time() # move the current piece all the way down elif event.key == K_SPACE: movingDown = False movingLeft = False movingRight = False for i in range(1, BOARDHEIGHT): if not isValidPosition(board, fallingPiece, adjY=i): break fallingPiece['y'] += i - 1 # handle moving the piece because of user input if (movingLeft or movingRight ) and time.time() - lastMoveSidewaysTime > MOVESIDEWAYSFREQ: if movingLeft and isValidPosition(board, fallingPiece, adjX=-1): fallingPiece['x'] -= 1 elif movingRight and isValidPosition(board, fallingPiece, adjX=1): fallingPiece['x'] += 1 lastMoveSidewaysTime = time.time() if movingDown and time.time( ) - lastMoveDownTime > MOVEDOWNFREQ and isValidPosition( board, fallingPiece, adjY=1): fallingPiece['y'] += 1 lastMoveDownTime = time.time() # let the piece fall if it is time to fall if time.time() - lastFallTime > fallFreq: # see if the piece has landed if not isValidPosition(board, fallingPiece, adjY=1): # falling piece has landed, set it on the board addToBoard(board, fallingPiece) score += removeCompleteLines(board) level, fallFreq = calculateLevelAndFallFreq(score) fallingPiece = None else: # piece did not land, just move the piece down fallingPiece['y'] += 1 lastFallTime = time.time() # drawing everything on the screen screen.fill(BGCOLOR) drawBoard(board) drawStatus(score, level) drawNextPiece(nextPiece) if fallingPiece != None: drawPiece(fallingPiece) pygame.display.update() FPSCLOCK.tick(FPS)
def main(): screen = pygame.display.set_mode((500, 500)) pygame.display.set_caption('Lines') global screen, greenline, greendirection, greenx, greeny, blueline, bluedirection, bluex, bluey, move, isValidTurn, detectCollision while True: screen.fill((0, 0, 0)) greenx, greeny = move(greenline, greendirection, greenx, greeny) bluex, bluey = move(blueline, bluedirection, bluex, bluey) for piece in greenline: pygame.draw.rect(screen, (100, 255, 100), piece) for piece in blueline: pygame.draw.rect(screen, (100, 100, 255), piece) if detectCollision(greenline, blueline) or detectCollision( blueline, greenline): if detectCollision(greenline, blueline) and detectCollision( blueline, greenline): myfont = pygame.font.Font(None, 40) screen.blit(myfont.render('TIE!', 1, (255, 255, 255)), (100, 100)) else: if detectCollision(greenline, blueline) == True: kmodule.drawbutton(pygame.rect.Rect(100, 100, 300, 100), False, 'BLUE WINS!') if detectCollision(blueline, greenline) == True: kmodule.drawbutton(pygame.rect.Rect(100, 100, 300, 100), True, 'GREEN WINS!') greenline = [] greendirection = 'right' greenx = 100 greeny = 250 blueline = [] bluedirection = 'left' bluex = 400 bluey = 250 exitloop = False pygame.display.update() while True: if kmodule.detect() != '': break for event in pygame.event.get(): if event.type == QUIT: import Arcade Arcade.main() elif event.type == KEYDOWN: exitloop = True if exitloop: break detection = kmodule.detect() if detection == 2 and isValidTurn(greendirection, 'up'): greendirection = 'up' if detection == 1 and isValidTurn(greendirection, 'left'): greendirection = 'left' if detection == 3 and isValidTurn(greendirection, 'down'): greendirection = 'down' if detection == 4 and isValidTurn(greendirection, 'right'): greendirection = 'right' if detection == 7 and isValidTurn(bluedirection, 'left'): bluedirection = 'left' if detection == 10 and isValidTurn(bluedirection, 'right'): bluedirection = 'right' if detection == 8 and isValidTurn(bluedirection, 'up'): bluedirection = 'up' if detection == 9 and isValidTurn(bluedirection, 'down'): bluedirection = 'down' for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_w and isValidTurn(greendirection, 'up'): greendirection = 'up' if event.key == K_a and isValidTurn(greendirection, 'left'): greendirection = 'left' if event.key == K_s and isValidTurn(greendirection, 'down'): greendirection = 'down' if event.key == K_d and isValidTurn(greendirection, 'right'): greendirection = 'right' if event.key == K_LEFT and isValidTurn(bluedirection, 'left'): bluedirection = 'left' if event.key == K_RIGHT and isValidTurn( bluedirection, 'right'): bluedirection = 'right' if event.key == K_UP and isValidTurn(bluedirection, 'up'): bluedirection = 'up' if event.key == K_DOWN and isValidTurn(bluedirection, 'down'): bluedirection = 'down' elif event.type == QUIT: return pygame.display.flip() t.tick(10)
def main(): import time time.sleep(0) myfont = pygame.font.Font(None, 22) titlefont = pygame.font.Font(None, 80) medfont = pygame.font.Font(None, 40) screen = pygame.display.set_mode((700, 500)) pygame.display.set_caption('Kalictric - Arcade') buttoncolor = (100, 255, 100) newbuttoncolor = (255, 100, 100) t = pygame.time.Clock() buttonspace = 40 buttonwidth = 50 buttonindent = 25 buttony = 100 collide = pygame.Rect(buttonindent, buttony, 50, 200) minecraft = pygame.Rect(buttonspace + buttonwidth + buttonindent, buttony, buttonwidth, 200) tetris = pygame.Rect((buttonspace + buttonwidth) * 2 + buttonindent, buttony, buttonwidth, 200) pingpong = pygame.Rect((buttonspace + buttonwidth) * 3 + buttonindent, buttony, buttonwidth, 200) blocks512 = pygame.Rect((buttonspace + buttonwidth) * 4 + buttonindent, buttony, buttonwidth, 200) back = pygame.Rect((buttonspace + buttonwidth) * 5 + buttonindent, buttony, buttonwidth, 200) exit = pygame.Rect((buttonspace + buttonwidth) * 6 + buttonindent, buttony, buttonwidth, 200) #exit = pygame.Rect((buttonspace+buttonwidth)*7+buttonindent,buttony,buttonwidth,200) #Programs to run import Collide import lines import tetromino import pong import launchminecraft import Mainmenu import Blocky #import Quit buttons = [collide, minecraft, tetris, pingpong, blocks512, back, exit] names = [ 'Collide', 'Minecraft', 'Lines', 'Tetromino', 'Pong', '512', 'Back', 'Exit' ] fullnames = [ "Don't hit the yellow squares", 'Minecraft Pi', 'Lines (Tron Clone)', 'Tetromino (Tetris clone)', 'Make your opponent miss the ball', 'A Game like 2048', 'Back to Homescreen', 'Exit' ] links = [ Collide, launchminecraft, lines, tetromino, pong, Blocky, Mainmenu, 'Quit.py' ] title = 'Arcade' def drawbutton(RECT, newcolor, text): if newcolor: pygame.draw.rect(screen, newbuttoncolor, RECT) else: pygame.draw.rect(screen, buttoncolor, RECT) p = myfont.render(text, 1, (255, 255, 255)) screen.blit(p, (RECT.left, RECT.top - 20)) def clicked(x, y, rect): if rect.collidepoint(x, y) == True: return True else: return False #last_touched = cap.touched() while True: x, y = pygame.mouse.get_pos() screen.fill((0, 0, 0)) '''DRAW TITLE''' screen.blit(titlefont.render(title, 1, (255, 255, 255)), (10, 10)) '''DRAW BUTTONS''' for i in range(len(buttons)): if clicked(x, y, buttons[i]): drawbutton(buttons[i], True, names[i]) keyName = myfont.render(fullnames[i], 1, (255, 255, 255)) screen.blit(keyName, (buttonindent, 400)) else: drawbutton(buttons[i], False, names[i]) '''KALIMBA KEY DETECTION''' '''current_touched = cap.touched() # Check each pin's last and current state to see if it was pressed or released. for i in range(12): # Each pin is represented by a bit in the touched value. A value of 1 # means the pin is being touched, and 0 means it is not being touched. pin_bit = 1 << i # First check if transitioned from not touched to touched. if current_touched & pin_bit and not last_touched & pin_bit: print('{0} touched!'.format(i)) try: execfile(links[i]) except IndexError: print "That's not an option" # Next check if transitioned from touched to not touched. if not current_touched & pin_bit and last_touched & pin_bit: print('{0} released!'.format(i)) # Update last state and wait a short period before repeating. last_touched = current_touched''' detection = kmodule.detect() if detection <= len(links) - 1: links[detection - 1].main() '''EVENTS''' for event in pygame.event.get(): if event.type == QUIT: print 'END' pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: for i in range(len(buttons)): if clicked(x, y, buttons[i]): links[i].main() pygame.display.update() t.tick(30)