Advertisement
Enrro

collectorTheGame 111627.1107

Nov 16th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.15 KB | None | 0 0
  1. '''
  2. Created on 11/11/2014
  3. pygame
  4. Proven and tested on python 2.7
  5. @author: A01221672
  6. '''
  7. import pygame
  8. import random
  9. import sys
  10. # functions
  11. def terminate():
  12.     pygame.quit()
  13.     sys.exit()
  14.  
  15. def waitForPlayerToPressKey():
  16.     while True:
  17.         for event in pygame.event.get():
  18.             if event.type == pygame.QUIT:
  19.                 terminate()
  20.             if event.type == pygame.KEYDOWN:
  21.                 if event.key == pygame.K_ESCAPE: # pressing escape quits
  22.                     terminate()
  23.                 return
  24.  
  25. def drawText(text, font, surface, x, y, color):
  26.     textobj = font.render(text, 1, color)
  27.     textrect = textobj.get_rect()
  28.     textrect.center = (x, y)
  29.     surface.blit(textobj, textrect)
  30.  
  31. def playSong(song,volume,loop):
  32.     pygame.mixer.music.load(song)
  33.     pygame.mixer.music.set_volume(float(volume))
  34.     pygame.mixer.music.play(loop)
  35. #poniendo en starting the game
  36. pygame.init()
  37. clock = pygame.time.Clock()
  38.  
  39. #creating a new window for the game
  40. ancho = 640
  41. alto = 480
  42. lienzo = pygame.display.set_mode((ancho,alto)) #Aqui es el tamano de la pantalla
  43. pygame.display.set_caption("Circle Runner")
  44.  
  45. # creating the player
  46. player = pygame.Rect(30, 150, 50, 50)
  47.  
  48. # score
  49. score = 0
  50. # creating enemies
  51. enemyCounter = 0
  52. enemy = []
  53. enemySize = 20
  54.  
  55. #choosing the color palet
  56. colorSugar = [[254,67,101],[252,157,154],[249,205,173],[200,200,169],[131,175,155]] #paleta de colores calidos
  57. colorDreamMagnet = [[52,56,56],[0,95,107],[0,140,158],[0,180,204],[0,223,252]]
  58.  
  59. #adding music
  60. musiclist = ["leaving the past behind.mp3","This Is The End.mp3","21. TechnoMan.mp3","sky_idgaf.mp3"]
  61.  
  62. #adding sound
  63. sound = pygame.mixer.Sound('smb_coin.wav')
  64. sound.set_volume(.4)
  65. #creating a font constant for the game
  66. FONT = pygame.font.SysFont('monospace!!!',20) #SysFont creates a font object from available pygame fonts
  67.  
  68.  
  69. #Seting up the movement variables
  70. moveLeft = False
  71. moveRight = False
  72. moveUp = False
  73. moveDown = False
  74.  
  75. MOVESPEED = 6
  76.  
  77. drawText('Collector', FONT, lienzo, (ancho / 2), (190),colorSugar[1])
  78. drawText('Press a key to start.', FONT, lienzo, (ancho / 2), (230),colorSugar[1])
  79. pygame.display.update()
  80. waitForPlayerToPressKey()
  81.  
  82.  
  83. #running the game
  84. topScore = 0
  85. while True:
  86.     score = 0
  87.     gameLoop = True
  88.     playSong(musiclist[0],.4,-1)
  89.     moveLeft = moveRight = moveUp = moveDown = False
  90.     for i in range(20):
  91.         enemy.append(pygame.Rect(random.randint(30, ancho - enemySize), random.randint(0, alto - enemySize), enemySize, enemySize))
  92.     while gameLoop:
  93.         score+=1
  94.         # check every event before continuing with the game
  95.         for event in pygame.event.get():
  96.             keysPress = pygame.key.get_pressed()
  97.             # game exit conditions
  98.             if event.type == pygame.QUIT:
  99.                 gameLoop = False
  100.             if keysPress[pygame.K_ESCAPE]:
  101.                 gameLoop = False
  102.             if keysPress[pygame.K_LALT] and keysPress[pygame.K_F4]:
  103.                 gameLoop = False
  104.             # mouse feedback to use maybe for latter
  105.             elif event.type == pygame.MOUSEBUTTONDOWN:
  106.                 print("Mouse")
  107.  
  108.             # Keyboard variables
  109.             if event.type == pygame.KEYDOWN:
  110.                 #print(event.key)
  111.                 if event.key == pygame.K_LEFT or event.key == ord("a"):
  112.                     moveLeft = True
  113.                     moveRight = False
  114.                 if event.key == pygame.K_RIGHT or event.key == ord("d"):
  115.                     moveLeft = False
  116.                     moveRight = True
  117.                 if event.key == pygame.K_UP or event.key == ord("w"):
  118.                     moveUp = True
  119.                     moveDown = False
  120.                 if event.key == pygame.K_DOWN or event.key == ord("s"):
  121.                     moveUp = False
  122.                     moveDown = True
  123.  
  124.             if event.type == pygame.KEYUP:
  125.                 if event.key == pygame.K_LEFT or event.key == ord("a"):
  126.                     moveLeft = False
  127.                 if event.key == pygame.K_RIGHT or event.key == ord("d"):
  128.                     moveRight = False
  129.                 if event.key == pygame.K_UP or event.key == ord("w"):
  130.                     moveUp = False
  131.                 if event.key == pygame.K_DOWN or event.key == ord("s"):
  132.                     moveDown = False
  133.         #Creationg more enemies
  134.         '''
  135.        trabajo pendiente
  136.        '''
  137.  
  138.         # creating the background
  139.         pygame.draw.rect(lienzo,(colorSugar[4]),(0, 0, ancho, alto))#fondo
  140.         pygame.draw.rect(lienzo,(colorSugar[0]),(0,0,30,alto))#Piso
  141.  
  142.         # move the player
  143.         if moveDown and player.bottom < alto:
  144.             player.top += MOVESPEED
  145.         if moveUp and player.top > 0:
  146.             player.top -= MOVESPEED
  147.         if moveRight and player.right < ancho:
  148.             player.right += MOVESPEED
  149.         if moveLeft and player.left > 30:
  150.             player.left -= MOVESPEED
  151.  
  152.  
  153.         # creating the player inside the surface
  154.         pygame.draw.rect(lienzo,(colorSugar[1]),player)
  155.  
  156.         #interection if collision
  157.         for en in enemy[:]:
  158.             if player.colliderect(en):
  159.                 sound.play()
  160.                 enemy.remove(en)
  161.  
  162.         # draw the enemies
  163.         for i in range(len(enemy)):
  164.             pygame.draw.rect(lienzo, colorSugar[3], enemy[i])
  165.  
  166.         # score system
  167.         drawText('BEST GAME EVER!!',FONT,lienzo,ancho/2,20,(0,0,0))
  168.         drawText('CURRENT SCORE: ' + str(20-len(enemy)),FONT,lienzo,ancho/2, 40,(0,0,0))
  169.         drawText('Best time: '+ str(topScore),FONT,lienzo,110,20,(0,0,0))
  170.         drawText('Time: '+ str(score),FONT,lienzo,90,40,(0,0,0))
  171.  
  172.         # victory condition
  173.         if len(enemy) <= 0:
  174.             if score > topScore:
  175.                 topScore = score
  176.             gameLoop = False
  177.  
  178.         #updating the screen
  179.         pygame.display.update()
  180.         clock.tick(60)
  181.  
  182.     # gameover screen
  183.     pygame.mixer.music.stop()
  184.     playSong(musiclist[-1],.4,0)
  185.     drawText('GAME OVER!!',FONT,lienzo,ancho/2,150,(0,0,0))
  186.     drawText('PRESS ANY KEY TO RESTART',FONT,lienzo,ancho/2,200,(0,0,0))
  187.     pygame.display.update()
  188.     waitForPlayerToPressKey()
  189.     pygame.mixer.music.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement