SHARE
TWEET

Untitled

a guest Mar 24th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #RickTheRocket
  2. #Team RagTag's MHV Project
  3.  
  4.  
  5. ###IMPORTS###
  6. from pygame import *
  7. #Pygame
  8. from math import *
  9. #Math
  10. from random import *
  11. #Random
  12. from rocket import *
  13. #Rocket
  14. from tkinter import *
  15. #Tkinter
  16. import gspread
  17. from oauth2client.service_account import ServiceAccountCredentials
  18.  
  19. #############
  20. name=str()
  21.  
  22. def getval():
  23.     global name
  24.     name=e1.get()
  25.     master.quit()
  26.  
  27. master = Tk()
  28. Label(master, text = "Name").grid(row = 0)
  29.  
  30. e1 = Entry(master)
  31.  
  32. e1.grid(row = 0, column = 1)
  33.  
  34. Button(master, text = 'Enter your name', command = getval).grid(row = 1, column = 1, sticky = W, pady = 4)
  35.  
  36. mainloop()
  37.  
  38. levels = []
  39. with open("levels.txt", "r") as file:
  40.     lines = file.read().splitlines()
  41.     level = []
  42.     for line in lines:
  43.         if line.count("#") == 0:
  44.             level.append(line)
  45.         else:
  46.             levels.append(level)
  47.             level = []
  48.  
  49. ###INITIALIZING###
  50. init()
  51. size=(1000,700)
  52. screen=display.set_mode(size)
  53. fps=time.Clock()
  54. font = font.SysFont("GiantRobotArmy Medium", 65)
  55. display.set_caption("Rick The Rocket")
  56. CHARRAD = 22
  57. GRAVITY = 0.38
  58. sprite = transform.scale(image.load("main1.png"), (40,40))
  59. background = image.load("resizedBack.jpg")
  60. level = 1
  61. runningScore = 0
  62. ##################
  63.  
  64. def rot_center(image, angle):
  65.     """rotate an image while keeping its center and size"""
  66.     orig_rect = image.get_rect()
  67.     rot_image = transform.rotate(image, angle)
  68.     rot_rect = orig_rect.copy()
  69.     rot_rect.center = rot_image.get_rect().center
  70.     rot_image = rot_image.subsurface(rot_rect).copy()
  71.     return rot_image
  72.  
  73. def game(level):
  74.     global runningScore
  75.     t = 0
  76.     running = True
  77.     fps = time.Clock()
  78.     rick = Rocket()
  79.     floors = levels[level - 1]
  80.     buttonVals = floors[-1]
  81.     floors.remove(floors[-1])
  82.     walls = []
  83.     for l in range(len(floors)):
  84.         for i in range(10):
  85.             layer = floors[l][i]
  86.             if layer == "L":
  87.                 rick.setPos((i + 1) * 100 - 75, (l + 1) * 100 - 300)
  88.             if layer == "G":
  89.                 flag = Rect(i * 100, l * 100 - 300, 100, 100)
  90.             if layer == "@":
  91.                 walls.append(Rect(i * 100, l * 100 - 300, 100, 100))
  92.     while running:
  93.         for evnt in event.get():
  94.             if evnt.type == QUIT:
  95.                 return "quit"
  96.             elif evnt.type == KEYDOWN:
  97.                 if evnt.key == K_SPACE:
  98.                     rick.jump()
  99.  
  100.         screen.blit(background,(0,-300))
  101.        
  102.         keys = key.get_pressed()
  103.         if keys[K_LEFT]:
  104.             rick.rotL()
  105.         elif keys[K_RIGHT]:
  106.             rick.rotR()
  107.        
  108.         rick.xPos += rick.xSpeed
  109.         rick.yPos += rick.ySpeed
  110.        
  111.         if rick.yPos >= 700 - CHARRAD:
  112.             rick.onGround = True
  113.             rick.ySpeed = 0
  114.             rick.yPos = 700 - CHARRAD
  115.            
  116.         if rick.onGround:
  117.             rick.refreshJumps()
  118.             rick.xSpeed = 0
  119.             rick.ySpeed = 0
  120.         else:
  121.             rick.ySpeed += GRAVITY
  122.            
  123.         screen.blit(background,(0,-300))
  124.         ###########################
  125.        
  126.         #Hitbox    
  127.         draw.circle(screen, (0,0,0), (int(rick.xPos), int(rick.yPos)), 22, 1)
  128.  
  129.         #Points of arc
  130.         draw.circle(screen,(255,0,0),(int(rick.xPos + (22 * cos(rick.ang1))), int(rick.yPos - (22 * sin(rick.ang1)))), 3, 0)
  131.         draw.circle(screen,(255,0,0),(int(rick.xPos + (22 * cos(rick.ang2))), int(rick.yPos - (22 * sin(rick.ang2)))), 3, 0)
  132.  
  133.         for r in walls:
  134.             draw.rect(screen, (255, 0, 0), r)
  135.         draw.rect(screen, (255, 255, 255), flag)
  136.         #Sprite
  137.         screen.blit(rot_center(sprite, degrees(rick.angle)), (rick.xPos - 20, rick.yPos - 22))
  138.         fps.tick(80)
  139.         display.flip()
  140.         t += 1
  141.         if flag.collidepoint(rick.xPos, rick.yPos):
  142.             runningScore += 10000 - t
  143.             return "game"
  144.        
  145. def highScore():
  146.     global name, runningScore
  147.     scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  148.     creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
  149.     client = gspread.authorize(creds)
  150.  
  151.     # Find a workbook by name and open the first sheet
  152.     # Make sure you use the right name here.
  153.     sheet = client.open("HighScore").sheet1
  154.     allScores = sheet.get_all_values()
  155.     allScores.remove(allScores[0])
  156.     row = [str(name), runningScore]
  157.     sheet.insert_row(row , len(allScores) + 2)
  158.     allScores = sheet.get_all_values()
  159.     allScores.remove(allScores[0])
  160.     nameList = []
  161.     scoreList = []
  162.     newList = [["Name", "Score"]]
  163.     for i in range (len(allScores)):
  164.         nameList.append(allScores[i][0])
  165.         scoreList.append(int(allScores[i][1]))
  166.    
  167.     scoreList.sort(reverse = True)
  168.     for num in scoreList:
  169.         pos = 0
  170.         for s in allScores:
  171.             if str(num) in s:
  172.                 pos = allScores.index(s)
  173.         newList.append(allScores[pos])
  174.         allScores.remove(allScores[pos])
  175.                
  176.     allScores = newList
  177.  
  178.     for i in range(len(allScores)):
  179.         for j in range(2):
  180.             sheet.update_cell(i + 1, j + 1, allScores[i][j])  
  181.    
  182.                
  183.    
  184. page = "game"
  185. while page != "quit":
  186.     if page == "game":
  187.         page = game(level)
  188.         if page == "game":
  189.             if level < 6:
  190.                 level += 1
  191.             if level == 6:
  192.                 page = "highScore"
  193.     if page == "highScore":
  194.         highScore()
  195.         page = "quit"
  196.  
  197. quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top