Advertisement
Guest User

Football game

a guest
Aug 31st, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.07 KB | None | 0 0
  1. #Imports
  2. import pygame
  3. import sys
  4. import os
  5. import time
  6. import random
  7.  
  8. # CURRENT BUGS
  9. # When i click anywhere while the game is loading, once it switches to the start menu, the game crashes,
  10.  
  11. #Initializing
  12. pygame.init()
  13. pygame.font.init()
  14. myFont = pygame.font.SysFont('Comic Sans MS', 30)
  15.  
  16. #Setting up screen
  17. screen_width = 1000
  18. screen_height = 650
  19. screen = pygame.display.set_mode((screen_width, screen_height))
  20.  
  21. #Variables
  22. game_state = "loadingScreen"
  23. selectedCountry = 0
  24. teamAPoints = []
  25. teamBPoints = []
  26. teamCPoints = []
  27. teamDPoints = []
  28. counter = 0
  29. teams = ["England", "Belgium", "Hungary", "Portugal", "Slovakia", "France", "Croatia", "Germany", "Romania", "Turkey", "Albania", "Czechia", "Italy", "Scotland", "Spain", "Austria", "Denmark", "Netherlands", "Serbia", "Switzerland"]
  30. team1 = 0
  31. team2 = 0
  32. team3 = 0
  33. team4 = 0
  34. team5 = 0
  35. team6 = 0
  36. team7 = 0
  37. team8 = 0
  38. team9 = 0
  39. team10 = 0
  40. team11 = 0
  41. team12 = 0
  42. team13 = 0
  43. team14 = 0
  44. team15 = 0
  45. team16 = 0
  46. team17 = 0
  47. team18 = 0
  48. team19 = 0
  49. team20 = 0
  50.  
  51. count = 0
  52.  
  53. #Buttons
  54.  
  55. englandButton = 0
  56. belgiumButton = 0
  57. hungaryButton = 0
  58. portugalButton = 0
  59. slovakiaButton = 0
  60. franceButton = 0
  61. croatiaButton = 0
  62. germanyButton = 0
  63. romaniaButton = 0
  64. turkeyButton = 0
  65. albaniaButton = 0
  66. czechiaButton = 0
  67. italyButton = 0
  68. scotlandButton = 0
  69. spainButton = 0
  70. austriaButton = 0
  71. denmarkButton = 0
  72. netherlandsButton = 0
  73. serbiaButton = 0
  74. switzerlandButton = 0
  75.  
  76. buttons = [englandButton, belgiumButton, hungaryButton, portugalButton, slovakiaButton, franceButton, croatiaButton, germanyButton, romaniaButton, turkeyButton, albaniaButton, czechiaButton, italyButton, scotlandButton, spainButton, austriaButton, denmarkButton, netherlandsButton, serbiaButton, switzerlandButton]
  77.  
  78. #Backgrounds
  79. loadingScreen = pygame.image.load('FootballBackground.png')
  80. teamSelect = pygame.image.load("countries.png")
  81. tornementPlan = pygame.image.load("TornementPlan.png")
  82. pointsTable = pygame.image.load("PointsTable.png")
  83.  
  84.  
  85. #Classes
  86. class Country:
  87.     def __init__(self, image):
  88.         self.Img = pygame.image.load(image)
  89.         self.Icon = pygame.transform.scale(self.Img, (40,40))
  90.  
  91. class Table:
  92.    
  93.     def __init__(self, MP, Wins, Draws, Losses, GD, Points):
  94.         self.MP: int = MP
  95.         self.Wins: int = Wins
  96.         self.Draws: int = Draws
  97.         self.Losses: int = Losses
  98.         self.GD: int = GD
  99.         self.Points: int = Points
  100.  
  101.  
  102. #Drawing loading screen
  103. def drawLoadingScreen():
  104.     screen.blit(loadingScreen, (0,0))
  105.     pygame.display.update()
  106.  
  107. #Drawing the team select page
  108. def drawStartMenu(buttons):
  109.    
  110.    screen.blit(teamSelect, (0,0))
  111.  
  112.    pygame.display.update()
  113.  
  114.    #Buttons
  115.    
  116.    buttons[0] = pygame.Rect(60, 126, 129.3, 80)
  117.    buttons[1] = pygame.Rect(248, 126, 129.3, 80)
  118.    buttons[2] = pygame.Rect(436, 126, 129.3, 80)
  119.    buttons[3] = pygame.Rect(624, 126, 129.3, 80)
  120.    buttons[4] = pygame.Rect(812, 126, 129.3, 80)
  121.    buttons[5] = pygame.Rect(60, 255, 129.3, 80)
  122.    buttons[6] = pygame.Rect(248, 255, 129.3, 80)
  123.    buttons[7] = pygame.Rect(436, 255, 129.3, 80)
  124.    buttons[8] = pygame.Rect(624, 255, 129.3, 80)
  125.    buttons[9] = pygame.Rect(812, 255, 129.3, 80)
  126.    buttons[10] = pygame.Rect(60, 384, 129.3, 80)
  127.    buttons[11] = pygame.Rect(248, 384, 129.3, 80)
  128.    buttons[12] = pygame.Rect(436, 384, 129.3, 80)
  129.    buttons[13] = pygame.Rect(624, 384, 129.3, 80)
  130.    buttons[14] = pygame.Rect(812, 384, 129.3, 80)
  131.    buttons[15] = pygame.Rect(60, 513, 129.3, 80)
  132.    buttons[16] = pygame.Rect(248, 513, 129.3, 80)
  133.    buttons[17] = pygame.Rect(436, 513, 129.3, 80)
  134.    buttons[18] = pygame.Rect(624, 513, 129.3, 80)
  135.    buttons[19] = pygame.Rect(812, 513, 129.3, 80)
  136.  
  137. #Drawing tornement plan
  138.  
  139. def tornementPlanDisplay():
  140.  
  141.     screen.blit(tornementPlan, (0,0))
  142.  
  143.     global count
  144.     count = count + 1
  145.  
  146.     team = [team1, team2, team3, team4, team5, team6, team7, team8, team9, team10, team11, team12, team13, team14, team15, team16, team17, team18, team19, team20]
  147.  
  148.     if count == 1:
  149.         random.shuffle(teams)
  150.    
  151.  
  152.     for i in range(len(teams)):
  153.         team[i] = teams[i]
  154.  
  155.     groupA = [team[0], team[1], team[2], team[3], team[4]]
  156.     groupB = [team[5], team[6], team[7], team[8], team[9]]
  157.     groupC = [team[10], team[11], team[12], team[13], team[14]]
  158.     groupD = [team[15], team[16], team[17], team[18], team[19]]
  159.    
  160.     for i in range (5):
  161.  
  162.         if groupA[i] == selectedCountry:
  163.             teamText = myFont.render(groupA[i], False, (255,0,0))
  164.         else:
  165.             teamText = myFont.render(groupA[i], False, (0,0,0))
  166.         teamRect = teamText.get_rect()
  167.         teamRect.center = (137, 390 + (40 * i))
  168.         screen.blit(teamText, teamRect.topleft)
  169.  
  170.     for i in range (5):
  171.  
  172.         if groupB[i] == selectedCountry:
  173.             teamText = myFont.render(groupB[i], False, (255,0,0))
  174.         else:
  175.             teamText = myFont.render(groupB[i], False, (0,0,0))
  176.         teamRect = teamText.get_rect()
  177.         teamRect.center = (379, 390 + (40 * i))
  178.         screen.blit(teamText, teamRect.topleft)
  179.  
  180.     for i in range (5):
  181.         if groupC[i] == selectedCountry:
  182.             teamText = myFont.render(groupC[i], False, (255,0,0))
  183.         else:
  184.             teamText = myFont.render(groupC[i], False, (0,0,0))
  185.         teamRect = teamText.get_rect()
  186.         teamRect.center = (621, 390 + (40 * i))
  187.         screen.blit(teamText, teamRect.topleft)
  188.  
  189.     for i in range (5):
  190.  
  191.         if groupD[i] == selectedCountry:
  192.             teamText = myFont.render(groupD[i], False, (255,0,0))
  193.         else:
  194.             teamText = myFont.render(groupD[i], False, (0,0,0))
  195.         teamRect = teamText.get_rect()
  196.         teamRect.center = (863, 390 + (40 * i))
  197.         screen.blit(teamText, teamRect.topleft)
  198.    
  199.     pygame.display.update()
  200.  
  201.     return groupA, groupB, groupC, groupD
  202.  
  203. #Points table display
  204. def pointsTableDisplay(groupA, groupB, groupC, groupD, counter, teamAPoints, teamBPoints, teamCPoints, teamDPoints):
  205.     screen.blit(pointsTable, (0,0))
  206.  
  207.     #Displaying icons
  208.     for i in range(5):
  209.         text = groupA[i] + "Flag.png"
  210.         countryIcon = Country(text)
  211.         screen.blit(countryIcon.Icon, (72.5, 42.5*i + 83))
  212.  
  213.         if counter == 0:
  214.             tempCountry = groupA[i]
  215.             tempCountry = Table(0, 0, 0, 0, 0, 0)
  216.             teamAPoints.insert(len(teamAPoints), tempCountry)
  217.         else:
  218.            
  219.  
  220.             countriesMatchesPlayed = myFont.render(str(teamAPoints[i].MP), False, (0,0,0))
  221.             teamRect = countriesMatchesPlayed.get_rect()
  222.             teamRect.center = (138, 101 + (43 * i))
  223.             screen.blit(countriesMatchesPlayed, teamRect.topleft)
  224.  
  225.             countriesWins = myFont.render(str(teamAPoints[i].Wins), False, (0,0,0))
  226.             teamRect = countriesWins.get_rect()
  227.             teamRect.center = (180, 101 + (43 * i))
  228.             screen.blit(countriesWins, teamRect.topleft)
  229.  
  230.             countriesDraws = myFont.render(str(teamAPoints[i].Draws), False, (0,0,0))
  231.             teamRect = countriesDraws.get_rect()
  232.             teamRect.center = (224, 101 + (43 * i))
  233.             screen.blit(countriesDraws, teamRect.topleft)
  234.  
  235.             countriesLosses = myFont.render(str(teamAPoints[i].Losses), False, (0,0,0))
  236.             teamRect = countriesLosses.get_rect()
  237.             teamRect.center = (265, 101 + (43 * i))
  238.             screen.blit(countriesLosses, teamRect.topleft)
  239.  
  240.             countriesGD = myFont.render(str(teamAPoints[i].GD), False, (0,0,0))
  241.             teamRect = countriesGD.get_rect()
  242.             teamRect.center = (308, 101 + (43 * i))
  243.             screen.blit(countriesGD, teamRect.topleft)
  244.  
  245.             countriesPoints = myFont.render(str(teamAPoints[i].Points), False, (0,0,0))
  246.             teamRect = countriesPoints.get_rect()
  247.             teamRect.center = (350, 101 + (43 * i))
  248.             screen.blit(countriesPoints, teamRect.topleft)
  249.  
  250.     for i in range(5):
  251.         text = groupB[i] + "Flag.png"
  252.         countryIcon = Country(text)
  253.         screen.blit(countryIcon.Icon, (635.5, 42.5*i + 83))
  254.  
  255.         if counter == 0:
  256.             tempCountry = groupB[i]
  257.             tempCountry = Table(0, 0, 0, 0, 0, 0)
  258.             teamBPoints.insert(len(teamBPoints), tempCountry)
  259.         else:
  260.  
  261.             countriesMatchesPlayed = myFont.render(str(teamBPoints[i].MP), False, (0,0,0))
  262.             teamRect = countriesMatchesPlayed.get_rect()
  263.             teamRect.center = (701, 101 + (43 * i))
  264.             screen.blit(countriesMatchesPlayed, teamRect.topleft)
  265.  
  266.             countriesWins = myFont.render(str(teamBPoints[i].Wins), False, (0,0,0))
  267.             teamRect = countriesWins.get_rect()
  268.             teamRect.center = (743, 101 + (43 * i))
  269.             screen.blit(countriesWins, teamRect.topleft)
  270.  
  271.             countriesDraws = myFont.render(str(teamBPoints[i].Draws), False, (0,0,0))
  272.             teamRect = countriesDraws.get_rect()
  273.             teamRect.center = (786, 101 + (43 * i))
  274.             screen.blit(countriesDraws, teamRect.topleft)
  275.  
  276.             countriesLosses = myFont.render(str(teamBPoints[i].Losses), False, (0,0,0))
  277.             teamRect = countriesLosses.get_rect()
  278.             teamRect.center = (827, 101 + (43 * i))
  279.             screen.blit(countriesLosses, teamRect.topleft)
  280.  
  281.             countriesGD = myFont.render(str(teamBPoints[i].GD), False, (0,0,0))
  282.             teamRect = countriesGD.get_rect()
  283.             teamRect.center = (871, 101 + (43 * i))
  284.             screen.blit(countriesGD, teamRect.topleft)
  285.  
  286.             countriesPoints = myFont.render(str(teamBPoints[i].Points), False, (0,0,0))
  287.             teamRect = countriesPoints.get_rect()
  288.             teamRect.center = (913, 101 + (43 * i))
  289.             screen.blit(countriesPoints, teamRect.topleft)
  290.     for i in range(5):
  291.         text = groupC[i] + "Flag.png"
  292.         countryIcon = Country(text)
  293.         screen.blit(countryIcon.Icon, (72.5, 42.5*i + 403))
  294.  
  295.         if counter == 0:
  296.             tempCountry = groupC[i]
  297.             tempCountry = Table(0, 0, 0, 0, 0, 0)
  298.             teamCPoints.insert(len(teamCPoints), tempCountry)
  299.         else:
  300.  
  301.             countriesMatchesPlayed = myFont.render(str(teamCPoints[i].MP), False, (0,0,0))
  302.             teamRect = countriesMatchesPlayed.get_rect()
  303.             teamRect.center = (138, 420 + (43 * i))
  304.             screen.blit(countriesMatchesPlayed, teamRect.topleft)
  305.  
  306.             countriesWins = myFont.render(str(teamCPoints[i].Wins), False, (0,0,0))
  307.             teamRect = countriesWins.get_rect()
  308.             teamRect.center = (180, 420 + (43 * i))
  309.             screen.blit(countriesWins, teamRect.topleft)
  310.  
  311.             countriesDraws = myFont.render(str(teamCPoints[i].Draws), False, (0,0,0))
  312.             teamRect = countriesDraws.get_rect()
  313.             teamRect.center = (224, 420 + (43 * i))
  314.             screen.blit(countriesDraws, teamRect.topleft)
  315.  
  316.             countriesLosses = myFont.render(str(teamCPoints[i].Losses), False, (0,0,0))
  317.             teamRect = countriesLosses.get_rect()
  318.             teamRect.center = (265, 420 + (43 * i))
  319.             screen.blit(countriesLosses, teamRect.topleft)
  320.  
  321.             countriesGD = myFont.render(str(teamCPoints[i].GD), False, (0,0,0))
  322.             teamRect = countriesGD.get_rect()
  323.             teamRect.center = (308, 420 + (43 * i))
  324.             screen.blit(countriesGD, teamRect.topleft)
  325.  
  326.             countriesPoints = myFont.render(str(teamCPoints[i].Points), False, (0,0,0))
  327.             teamRect = countriesPoints.get_rect()
  328.             teamRect.center = (350, 420 + (43 * i))
  329.             screen.blit(countriesPoints, teamRect.topleft)
  330.     for i in range(5):
  331.         text = groupD[i] + "Flag.png"
  332.         countryIcon = Country(text)
  333.         screen.blit(countryIcon.Icon, (635.5, 42.5*i + 403))
  334.  
  335.         if counter == 0:
  336.             tempCountry = groupD[i]
  337.             tempCountry = Table(0, 0, 0, 0, 0, 0)
  338.             teamDPoints.insert(len(teamDPoints), tempCountry)
  339.         else:
  340.  
  341.             countriesMatchesPlayed = myFont.render(str(teamDPoints[i].MP), False, (0,0,0))
  342.             teamRect = countriesMatchesPlayed.get_rect()
  343.             teamRect.center = (701, 420 + (43 * i))
  344.             screen.blit(countriesMatchesPlayed, teamRect.topleft)
  345.  
  346.             countriesWins = myFont.render(str(teamDPoints[i].Wins), False, (0,0,0))
  347.             teamRect = countriesWins.get_rect()
  348.             teamRect.center = (743, 420 + (43 * i))
  349.             screen.blit(countriesWins, teamRect.topleft)
  350.  
  351.             countriesDraws = myFont.render(str(teamDPoints[i].Draws), False, (0,0,0))
  352.             teamRect = countriesDraws.get_rect()
  353.             teamRect.center = (786, 420 + (43 * i))
  354.             screen.blit(countriesDraws, teamRect.topleft)
  355.  
  356.             countriesLosses = myFont.render(str(teamDPoints[i].Losses), False, (0,0,0))
  357.             teamRect = countriesLosses.get_rect()
  358.             teamRect.center = (827, 420 + (43 * i))
  359.             screen.blit(countriesLosses, teamRect.topleft)
  360.  
  361.             countriesGD = myFont.render(str(teamDPoints[i].GD), False, (0,0,0))
  362.             teamRect = countriesGD.get_rect()
  363.             teamRect.center = (871, 420 + (43 * i))
  364.             screen.blit(countriesGD, teamRect.topleft)
  365.  
  366.             countriesPoints = myFont.render(str(teamDPoints[i].Points), False, (0,0,0))
  367.             teamRect = countriesPoints.get_rect()
  368.             teamRect.center = (913, 420 + (43 * i))
  369.             screen.blit(countriesPoints, teamRect.topleft)
  370.    
  371.     pygame.display.update()
  372.    
  373.     return counter, teamAPoints, teamBPoints, teamCPoints, teamDPoints
  374.  
  375. def gameSimulator(groupA, groupB, groupC, groupD, counter, teamAPoints, teamBPoints, teamChecks, teamDPoints):
  376.    
  377.     for i in range(4):
  378.  
  379.         def randomFirstTeam():
  380.             randomFirstTeam = teamAPoints[random.randint(0,4)]
  381.             return randomFirstTeam
  382.  
  383.         def randomSecondTeam():
  384.             randomSecondTeam = teamAPoints[random.randint(0,4)]
  385.             return randomSecondTeam
  386.        
  387.         randomTeam1 = randomFirstTeam()
  388.         randomTeam2 = randomSecondTeam()
  389.  
  390.         def teamChecks(randomTeam1, randomTeam2):
  391.             if randomTeam1 == randomTeam2:
  392.                 randomTeam2 = randomSecondTeam()
  393.                 randomTeam1, randomTeam2 = teamChecks(randomTeam1, randomTeam2)
  394.             elif randomTeam1.MP == 4:
  395.                 randomTeam1 = randomFirstTeam()
  396.                 randomTeam1, randomTeam2 = teamChecks(randomTeam1, randomTeam2)
  397.             elif randomTeam2.MP == 4:
  398.                 randomTeam2 = randomSecondTeam()
  399.                 randomTeam1, randomTeam2 = teamChecks(randomTeam1, randomTeam2)
  400.  
  401.         randomTeam1, randomTeam2 = teamChecks(randomTeam1, randomTeam2)
  402.  
  403.         winner = 1
  404.  
  405.         if winner == random.randint(1,2):
  406.             randomTeam1.MP += 1
  407.             randomTeam1.Points += 3
  408.             randomTeam1.Wins += 1
  409.             randomTeam2.MP += 1
  410.             randomTeam2.Losses -= 1
  411.  
  412.         game_state = "PointsTable"
  413.         return counter, teamAPoints, teamBPoints, teamChecks, teamDPoints
  414.  
  415.  
  416.  
  417. #Game loop
  418. while True:
  419.  
  420.     if game_state == "loadingScreen":
  421.  
  422.         drawLoadingScreen()
  423.  
  424.         for event in pygame.event.get():
  425.             if event.type == pygame.QUIT:
  426.                 pygame.quit()
  427.                 exit(0)
  428.             elif event.type == pygame.MOUSEBUTTONDOWN:
  429.                 print("Clicked")
  430.  
  431.         time.sleep(4)
  432.  
  433.         game_state = "startMenu"
  434.  
  435.     elif game_state == "startMenu":
  436.         for event in pygame.event.get():
  437.             if event.type == pygame.QUIT:
  438.                 pygame.quit()
  439.                 exit(0)
  440.             elif event.type == pygame.MOUSEBUTTONDOWN:
  441.                 #Checking which team they selected
  442.                 for i in range(len(buttons)):
  443.                     if buttons[i].collidepoint(event.pos):
  444.                         selectedCountry = teams[i]
  445.                         game_state = "tornementPlan"
  446.                         break
  447.                     else:
  448.                         print("No")
  449.         drawStartMenu(buttons)
  450.     elif game_state == "tornementPlan":
  451.         for event in pygame.event.get():
  452.             if event.type == pygame.QUIT:
  453.                 pygame.quit()
  454.                 exit(0)
  455.             elif event.type == pygame.MOUSEBUTTONDOWN:
  456.                 game_state = "PointsTable"
  457.         groupA, groupB, groupC, groupD = tornementPlanDisplay()
  458.     elif game_state == "PointsTable":
  459.         for event in pygame.event.get():
  460.             if event.type == pygame.QUIT:
  461.                 pygame.quit()
  462.                 exit(0)
  463.             elif event.type == pygame.MOUSEBUTTONDOWN:
  464.                 game_state = "Game"
  465.         counter, teamAPoints, teamBPoints, teamCPoints, teamDPoints = pointsTableDisplay(groupA, groupB, groupC, groupD, counter, teamAPoints, teamBPoints, teamCPoints, teamDPoints)
  466.         counter = counter + 1
  467.     elif game_state == "Game":
  468.         for event in pygame.event.get():
  469.             if event.type == pygame.QUIT:
  470.                 pygame.quit()
  471.                 exit(0)
  472.         counter, teamAPoints, teamBPoints, teamCPoints, teamDPoints = gameSimulator(groupA, groupB, groupC, groupD, counter, teamAPoints, teamBPoints, teamCPoints, teamDPoints)
  473.    
  474.    
  475.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement