Advertisement
Guest User

Untitled

a guest
May 26th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4.  
  5. #pygame.mixer.Sound(file).play
  6. #pygame.mixer.Sound.stop
  7. #pygame.mixer.Sound.fadeout
  8. pygame.init()
  9.  
  10. HEIGHT = 600
  11. WIDTH = 600 # 800 # 600 -> square
  12. visual = pygame.display.set_mode((HEIGHT, WIDTH))
  13. #title screen animation start
  14. dt=pygame.time.Clock()
  15. enterPressed=False
  16. Current_fps = None
  17. which_TitleScreen=1
  18. time_since_TitleScreen_changed=0
  19. TitleScreen=pygame.image.load("TitleScreen.png")
  20. TitleScreen = pygame.transform.scale(TitleScreen, (WIDTH, HEIGHT))
  21. visual.blit(TitleScreen,(0,1))
  22. #pygame.display.flip()
  23.  
  24. while enterPressed==False:
  25. time_since_TitleScreen_changed+=dt
  26. dt = clock.tick()
  27. if time_since_TitleScreen_changed>=350:
  28. if which_TitleScreen==1:
  29. which_TitleScreen=2
  30. TitleScreen= pygame.image.load("TitleScreen2.png")
  31. TitleScreen = pygame.transform.scale(TitleScreen, (WIDTH,HEIGHT))
  32. visual.blit(TitleScreen,(0,0))
  33. pygame.display.flip()
  34. time_since_TitleScreen_changed=0
  35.  
  36.  
  37. else:
  38. which_TitleScreen=1
  39. TitleScreen=pygame.image.load("TitleScreen.png")
  40. TitleScreen = pygame.transform.scale(TitleScreen, (WIDTH, HEIGHT))
  41. pygame.display.flip()
  42. time_since_TitleScreen_changed=0
  43.  
  44. for event in pygame.event.get():
  45. if event.type==pygame.KEYDOWN:
  46. if event.key==pygame.K_SPACE:
  47.  
  48.  
  49. enterPressed=True
  50. #title screen animation end
  51.  
  52.  
  53.  
  54. xImg = pygame.transform.scale(pygame.image.load('X.png'), (WIDTH // 3, HEIGHT // 3))
  55. oImg = pygame.transform.scale(pygame.image.load('o.png'), (WIDTH // 3, HEIGHT // 3))
  56.  
  57.  
  58. image=pygame.image.load("choice.png")
  59. image = pygame.transform.scale(image, (WIDTH, HEIGHT))
  60. visual.blit(image,(0,0))
  61. pygame.display.flip()
  62. #Kadables event mode choosing code
  63. mode=""
  64. pygame.event.clear()
  65. option_chosen=False
  66. while option_chosen==False:
  67. for event in pygame.event.get():
  68. if event.type==pygame.KEYDOWN:
  69. if event.key==pygame.K_0:
  70. mode=0
  71. option_chosen=True
  72. elif event.key==pygame.K_1:
  73. mode=1
  74. option_chosen=True
  75.  
  76. board = [
  77. 0,0,0,
  78. 0,0,0,
  79. 0,0,0
  80. ]
  81.  
  82. locations = [
  83. (0,0),(200,0),(400,0),
  84. (0,205),(200,205),(400,205),
  85. (0,410),(200,410),(400,410)
  86. ]
  87.  
  88. # Is it the player's player_peice
  89. player_move = True
  90.  
  91. running = True
  92. draw = False
  93. player_peice = 'x'
  94. ai_peice = 'o'
  95.  
  96. active_peice = 'x'
  97.  
  98. position = -1
  99.  
  100. while running:
  101. # Draw grid
  102. pygame.draw.line(visual, (255,255,255), (200,0), (200,600), 5)
  103. pygame.draw.line(visual, (255,255,255), (400,0), (400,600), 5)
  104.  
  105. pygame.draw.line(visual, (255,255,255), (0,200), (600,200), 5)
  106. pygame.draw.line(visual, (255,255,255), (0,400), (600,400), 5)
  107.  
  108.  
  109. if player_move:
  110.  
  111. for event in pygame.event.get():
  112. if event.type == pygame.QUIT:
  113. running = False
  114. # Where to place the X or O value
  115. if event.type == pygame.KEYDOWN:
  116. if event.key == pygame.K_1:
  117. position = event.unicode
  118. if event.key == pygame.K_2:
  119. position = event.unicode
  120. if event.key == pygame.K_3:
  121. position = event.unicode
  122. if event.key == pygame.K_4:
  123. position = event.unicode
  124. if event.key == pygame.K_5:
  125. position = event.unicode
  126. if event.key == pygame.K_6:
  127. position = event.unicode
  128. if event.key == pygame.K_7:
  129. position = event.unicode
  130. if event.key == pygame.K_8:
  131. position = event.unicode
  132. if event.key == pygame.K_9:
  133. position = event.unicode
  134.  
  135. position = int(position) - 1
  136.  
  137.  
  138. if mode == 0 and active_peice == ai_peice:
  139.  
  140. check = 0
  141. while check == 0: # for check if positionition is free
  142.  
  143. position = random.randint(0,8) # 9 is important
  144. if board[position] == 0:
  145. check = 1
  146.  
  147. board[position] = 'o'
  148.  
  149.  
  150. #if position >= 0 and board[position] == 0: # only when button has been pressed
  151. if position > -1: # and mode == 1:
  152. if board[position] == 0:
  153. board[position] = active_peice
  154. position = -1
  155.  
  156. if active_peice == 'x': # change player after edit board
  157. active_peice = 'o'
  158. else:
  159. active_peice = 'x'
  160.  
  161.  
  162.  
  163. for spot in range(len(board)): # 0 to 8
  164. if not board[spot] == 0:
  165. col = spot
  166. row = 0
  167. while col >= 3:
  168. col = col - 3
  169. row += 1
  170.  
  171. if board[spot] == 'x':
  172. visual.blit(xImg, (col * 200, row * 200))
  173. if board[spot] == 'o':
  174. visual.blit(oImg, (col * 200, row * 200))
  175. #print(board[spot], col, row)
  176.  
  177.  
  178.  
  179. # check if one player wins
  180. for i in range(3):
  181. for p in ["x", "o"]:
  182. if board[i*3] == p and board[i*3+1] == p and board[i*3+2] == p: # checking rows
  183. print("horizontal")
  184. win = p
  185. running = False
  186.  
  187. if board[i] == p and board[i+3] == p and board[i+6] == p:
  188. print("vertical")
  189. win = p
  190. running = False
  191.  
  192. if board[0] == p and board[4] == p and board[8] == p:
  193. print("diagonal right")
  194. win = p
  195. running = False
  196.  
  197. if board[2] == p and board[4] == p and board[6] == p:
  198. print("diagonal left")
  199. win = p
  200. running = False
  201. # Checks the board from 0-9
  202. used = 0
  203. for spot in board:
  204. if not spot == 0:
  205. used += 1
  206.  
  207. if used == 9:
  208. running = False
  209. draw = True
  210.  
  211.  
  212.  
  213. pygame.display.update()
  214. visual.fill((0,0,0))
  215.  
  216.  
  217. if draw == False:
  218. print("\nPlayer " + win + " wins")
  219.  
  220. if draw: # == True:
  221. print("No winner")
  222. # playing draw sound
  223.  
  224. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement