Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.99 KB | None | 0 0
  1. #imports
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. import time
  6. import random
  7. import psycopg2
  8.  
  9.  
  10. # Use the database
  11. def interact_with_database(command):
  12.     # Connect and set up cursor
  13.     connection = psycopg2.connect("dbname=postgres user=postgres password='2450086'")
  14.     cursor = connection.cursor()
  15.    
  16.     # Execute the command
  17.     cursor.execute(command)
  18.     connection.commit()
  19.  
  20.     # Save results
  21.     results = None
  22.     try:
  23.         results = cursor.fetchall()
  24.     except psycopg2.ProgrammingError:
  25.         # Nothing to fetch
  26.         pass
  27.  
  28.     # Close connection
  29.     cursor.close()
  30.     connection.close()
  31.    
  32.     return results
  33.  
  34.  
  35.  
  36. # Uploads a score into the hiscore table
  37. def upload_score(name, score):
  38.     interact_with_database("UPDATE users SET score = {} WHERE name = '{}'")
  39.  
  40.  
  41. # Downloads score data from database
  42. def download_scores():
  43.     return interact_with_database("SELECT * FROM users")
  44.  
  45.  
  46. # Downloads the top score from database
  47. def download_top_score():
  48.     result = interact_with_database("SELECT * FROM users ORDER BY name")[0][1]
  49.     return result
  50.  
  51.  
  52.  
  53.  
  54.  
  55. pygame.init()
  56. width = 800
  57. height = 850
  58.  
  59. screen = pygame.display.set_mode((width,height))
  60.  
  61. #colors
  62.  
  63. black = (0,0,0)
  64. red = (255,0,0)
  65. white = (255,255,255)
  66. grey = (158,158,158)
  67. done = True
  68.  
  69.  
  70. #highscore screen---
  71.  
  72. pygame.display.set_caption('HighScore')
  73.  
  74.  
  75. defaultfont = pygame.font.get_default_font()
  76. fontrenderer = pygame.font.Font(defaultfont,85)
  77.  
  78. #highscorelabel
  79. label3 = fontrenderer.render("HighScore",1,black)
  80. screen.blit(label3,(200,15))
  81.  
  82. #select *from users func
  83. score = download_scores()
  84.  
  85. #print table of users and score
  86. def table():
  87.     buffer=[]
  88.     for i in score:
  89.         x = "{} --> {}".format(i[0],i[1])
  90.         buffer.append(x)
  91.  
  92.     #for x in buffer:
  93.         #print(x)
  94.     return buffer
  95.    
  96. list = table()
  97.  
  98.  
  99.  
  100. #data from database
  101. font = pygame.font.SysFont(None, 40)
  102.  
  103.  
  104. #---------------------------------------------------------------------------
  105.  
  106. #gameboard background
  107. bg = pygame.image.load('speelbord.png')
  108. bg1 = pygame.transform.smoothscale(bg,(width,height))
  109. screen = pygame.display.set_mode((width,height))
  110.  
  111. #back button
  112. img5 = pygame.image.load('back.png')
  113. img6 = pygame.transform.smoothscale(img5,(200, 70))
  114.  
  115. defaultfont = pygame.font.get_default_font()
  116. fontrenderer = pygame.font.Font(defaultfont,85)
  117.  
  118. class hsscreen:
  119.     def __init__(self):
  120.         self.running = True
  121.  
  122.  
  123.  
  124.  
  125.     def introo(self):
  126.  
  127.         while self.running:
  128.             screen.blit(bg1,(0,0))
  129.             screen.blit(label3,(200,15))
  130.             screen.blit(img6,(50,730))
  131.  
  132.             #list
  133.             count = 0
  134.             for s in list:
  135.                 count += 1
  136.                 screen.blit(font.render("{}".format(s),1,black),(250 ,(150 + (100 * count))))
  137.  
  138.             for event in pygame.event.get():
  139.                 if event.type == pygame.QUIT:
  140.                     self.running = False
  141.                     Game(players)
  142.                 if event.type == pygame.MOUSEBUTTONDOWN:
  143.                     mousex, mousey = pygame.mouse.get_pos()
  144.                     if mousex > 50 and mousex < 150 and mousey > 25 and mousey < 125:
  145.                         self.running = False
  146.                        
  147.  
  148.             pygame.display.flip()
  149.  
  150.  
  151. hs = hsscreen()
  152.  
  153. done = False
  154.  
  155. pygame.init()
  156. width = 800
  157. height = 850
  158. img = pygame.image.load('play.png')
  159. img3 = pygame.image.load('exit.png')
  160. ologo = pygame.image.load('ontsnapperdam logo.png')
  161. img2 = pygame.transform.smoothscale(img,(200, 70))
  162. img4 = pygame.transform.smoothscale(img3,(200,70))
  163. bg1 = pygame.transform.smoothscale(bg,(width,height))
  164. ologo1 = pygame.transform.smoothscale(ologo,(400,80))
  165. screen = pygame.display.set_mode((width,height))
  166.  
  167.  
  168.  
  169. defaultfont = pygame.font.get_default_font()
  170. fontrenderer = pygame.font.Font(defaultfont,85)
  171.  
  172. label = fontrenderer.render("Ontsnapperdam",1,red)
  173. label2 = fontrenderer.render("Settings",1,black)
  174.  
  175.  
  176. class start:
  177.     def __init__(self):
  178.         self.running = True
  179.  
  180.  
  181.  
  182.  
  183.     def intro(self):
  184.  
  185.         while self.running:
  186.             screen.blit(bg1,(0,0))
  187.             screen.blit(img2,(width*0.35,height*0.35))
  188.             screen.blit(img4,(width*0.35,height*0.55))
  189.             screen.blit(ologo1,(15,15))
  190.             for event in pygame.event.get():
  191.                 if event.type == pygame.QUIT:
  192.                     self.running = False
  193.                     Game(players)
  194.                 if event.type == pygame.MOUSEBUTTONDOWN:
  195.                     mousex, mousey = pygame.mouse.get_pos()
  196.                     if  width*0.35 <= mousex and  width*0.60 >= mousex and height*0.35 <=mousey and height*0.466 >= mousey:
  197.                         self.running = False
  198.                     elif width*0.35 <= mousex and width*0.60 >= mousex and height*0.55 <= mousey and height*0.766 >= mousey:
  199.                         pygame.quit()
  200.                         quit()
  201.  
  202.             pygame.display.flip()
  203.  
  204.  
  205. menu=start()
  206.  
  207. done = False
  208.  
  209.  
  210. class Player:
  211.     def __init__(self, name, kleur, x, y):
  212.         self.name = name
  213.         self.kleur = kleur
  214.         self.x = x
  215.         self.y = y
  216.         self.r = 10
  217.         self.rect = pygame.Rect(self.x, self.y, 20, 20)
  218.         self.steps = 0
  219.  
  220.     def draw(self, screen):
  221.         pygame.draw.circle(screen, self.kleur, (self.rect.center), self.r)
  222.  
  223.     def Update(self, screen, event, blocks):
  224.         if event.type == pygame.KEYDOWN:
  225.             # to check the new position is within the game blocks
  226.             newpos = self.rect.copy()
  227.  
  228.             if event.key == pygame.K_LEFT:
  229.                 newpos.x -= 26
  230.             elif event.key == pygame.K_RIGHT:
  231.                 newpos.x += 26
  232.             elif event.key == pygame.K_UP:
  233.                 newpos.y -= 26
  234.             elif event.key == pygame.K_DOWN:
  235.                 newpos.y += 26
  236.  
  237.             # bool check is newpos is inside game rectangles
  238.             for rectangle in blocks:
  239.                 if newpos.colliderect(rectangle):
  240.                     # convert newpos in the new position
  241.                     self.steps += 1
  242.                     self.rect = newpos
  243.                     # stop check when matched
  244.                     break
  245.  
  246.     def Pos(self):
  247.         return self.x, self.y
  248.  
  249.  
  250. class Game:
  251.     def __init__(self, players):
  252.         pygame.init()
  253.         self.turn = 0
  254.         self.players = players
  255.         self.thrown = 0
  256.         self.size = (800, 850)
  257.         self.running = False
  258.  
  259.         # block width,height,margin
  260.         self.w = 25
  261.         self.h = 25
  262.         self.m = 1
  263.  
  264.         # colors
  265.         self.red = (191, 36, 36)
  266.         self.black = (23, 20, 20)
  267.  
  268.         # self.screen = pygame.display.set_mode(self.size)
  269.         self.blocks = []
  270.         self.battleblocks = []
  271.  
  272.         # Create list with all block position in the game
  273.         for row in range(30):
  274.             for col in range(30):
  275.                 if self.Filter(col, row) == "battleblock":
  276.                     self.battleblocks.append(
  277.                         pygame.Rect((self.w + self.m) * col + self.m + 40, ((self.h + self.m) * row + self.m + 50),
  278.                                     self.w, self.h))
  279.                 elif self.Filter(col, row) == "normalblock":
  280.                     self.blocks.append(
  281.                         pygame.Rect((self.w + self.m) * col + self.m + 40, ((self.h + self.m) * row + self.m + 50),
  282.                                     self.w, self.h))
  283.  
  284.     def Update(self, event):
  285.         player = self.players[self.turn]
  286.         if event.type == pygame.KEYDOWN:
  287.             player.Update(self.screen, event, self.blocks)
  288.  
  289.             # if all steps made
  290.             if player.steps == self.thrown:
  291.                 self.thrown = 0
  292.                 print(player.Pos())
  293.                 if self.turn == (len(self.players) - 1):
  294.                     self.turn = 0
  295.                 else:
  296.                     self.turn += 1
  297.                 player.steps = 0
  298.  
  299.     def Filter(self, x, y):
  300.         map_list = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [0, 9, 19], [0, 9, 10, 19],
  301.                     [0, 10, 19], [0, 10, 19],
  302.                     [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 19], [0, 2, 4, 8, 13, 17, 18, 19],
  303.                     [0, 2, 3, 4, 8, 13, 17, 19], [0, 4, 8, 11, 12, 13, 14, 15, 16, 17, 19], [0, 4, 8, 11, 19],
  304.                     [0, 4, 5, 6, 7, 8, 11, 13, 14, 15, 16, 17, 18, 19], [0, 6, 11, 12, 13, 16, 19],
  305.                     [0, 6, 13, 14, 15, 16, 19], [0, 3, 4, 5, 6, 13, 16, 19],
  306.                     [0, 3, 6, 7, 8, 9, 10, 11, 12, 13, 16, 17, 18, 19],
  307.                     [0, 1, 2, 3, 6, 13, 16, 19], [0, 3, 6, 13, 16, 19], [0, 3, 6, 7, 8, 9, 13, 14, 15, 16, 19],
  308.                     [0, 2, 3, 4, 5, 6, 9, 13, 19], [0, 2, 9, 13, 19], [0, 2, 9, 10, 11, 12, 13, 19],
  309.                     [0, 2, 3, 4, 5, 6, 13, 19], [0, 6, 13, 14, 15, 19],
  310.                     [0, 6, 15, 19], [0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 19], [0, 1, 2, 3, 4, 5, 6, 8, 15, 19],
  311.                     [3, 8, 13, 14, 15, 19], [3, 8, 13, 19], [3, 8, 13, 19],
  312.                     [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]
  313.         battle_blocks = [[], [], [], [0], [0], [0, 1], [0], [13, 17], [8, 13, 14, 15, 16, 17], [8], [7, 8], [],
  314.                          [13, 14], [3, 4, 5, 6, 13],
  315.                          [3, 6, 13], [13], [13], [13, 14], [], [], [], [], [], [], [], [3, 4, 5, 6], [3, 13], [13],
  316.                          [13], [11, 12, 13]]
  317.         if x in battle_blocks[y]:
  318.             return "battleblock"
  319.         elif x in map_list[y]:
  320.             return "normalblock"
  321.         return False
  322.  
  323.     def Draw(self):
  324.  
  325.         # draw canvas
  326.         self.screen = pygame.display.set_mode(self.size)
  327.         self.screen.fill((255, 255, 255))
  328.  
  329.         # draw dice
  330.         pygame.draw.rect(self.screen, (0, 0, 0), (10, 10, 50, 50), 1)
  331.  
  332.         # draw all player
  333.         for player in self.players:
  334.             player.draw(self.screen)
  335.  
  336.         # draw game board
  337.         for rectangle in self.blocks:
  338.             pygame.draw.rect(self.screen, self.black, rectangle, 1)
  339.         for rectangle in self.battleblocks:
  340.             pygame.draw.rect(self.screen, self.red, rectangle, 1)
  341.  
  342.         # update whole screen
  343.         pygame.display.flip()
  344.  
  345.     def Gameloop(self):
  346.  
  347.         self.Draw()
  348.  
  349.         while not self.running:
  350.             for event in pygame.event.get():
  351.                 if event.type == pygame.QUIT:
  352.                     self.running = True
  353.  
  354.                 # If click on dice
  355.                 if event.type == pygame.MOUSEBUTTONDOWN:
  356.                     mousex, mousey = pygame.mouse.get_pos()
  357.                     if mousex > 10 and mousex < 60 and mousey > 10 and mousey < 60:
  358.                         self.thrown = random.randint(1, 6)
  359.                         print(self.thrown)
  360.  
  361.                 # execute if dice is thrown
  362.                 if event.type == pygame.KEYDOWN:
  363.                     if self.thrown > 0:
  364.                         self.Update(event)
  365.  
  366.                         self.Draw()
  367.                         # pygame.display.flip()
  368.  
  369.  
  370. player1 = Player("A", (155, 255, 140), 200, 28)
  371. player2 = Player("B", (155, 255, 140), 226, 28)
  372. player3 = Player("C", (91, 183, 211), 252, 28)
  373. player4 = Player("D", (116, 59, 124), 278, 28)
  374. player5 = Player("E", (237, 65, 56), 304, 28)
  375. player6 = Player("F", (0, 0, 0), 330, 28)
  376.  
  377. players = [player1, player2, player3, player4]
  378.  
  379. game = Game(players)
  380.  
  381. class Program:
  382.     menu.intro()
  383.     hs.introo()
  384.     game.Gameloop()
  385.  
  386.  
  387. Program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement