小言_互联网的博客

世界杯太精彩了,带大家用Python做个足球游戏,边玩游戏边看比赛

506人阅读  评论(0)

文章目录

Python零基础快速制作足球游戏(附源代码)

前言

一、Python环境说明

二、游戏程序说明

1、游戏开始界面

2、人物移动规则说明,可支持两位玩家

3、足球规则

4、主方法调取

三、游戏运行效果与比赛结果

1、游戏开始界面

2、下届世界杯预测比赛结果


Python零基础快速制作足球游戏(附源代码)

前言

卡塔尔世界杯正是进行得火热,十六强队伍已经诞生,后面就是越来越紧张的争夺八强的淘汰赛。目前爆冷的赛果让球迷一度情绪失落,比如:日本2-1战胜西班牙,韩国2-1战胜葡萄牙。

这正是足球的魅力所在,结果只会给更努力的一方,过去的成绩在比赛不在起决定性的作用,亚洲强队越战越强,期望国足能在下届世界杯有出场的机会。

没能看到国足在这届世界杯的球场奔驰,只能用Python制作世界杯足球游戏,让国足可以在游戏里的世界杯上场。国足能否在足球游戏里拿到大力神杯,请看到文末,结果让人惊喜,接下是对源代码简单讲述。

一、Python环境说明

详细的Python安装教程:Python基础(二):不同系统安装Python3_Lansonli的博客-CSDN博客

Python版本:3.9.13

主要模块:

pygame

安装步骤:

python -m pip install --upgrade pip

pip install pygame

二、游戏程序说明

1、游戏开始界面

首先游戏需要一个开始界面,为了方便大家操作,设置成了按任意键就可以开始游戏。


  
  1. def myinit():
  2. screen = pygame.display.set_mode(( 769, 563))
  3. g1 = pygame.image.load( "g1.jpg").convert()
  4. g2 = pygame.image.load( "hh.png").convert()
  5. t = 0
  6. timer = pygame.time.Clock()
  7. while( 1):
  8. timer.tick( 30)
  9. ticks = pygame.time.get_ticks();
  10. for event in pygame.event.get():
  11. if event. type == QUIT:
  12. pygame.quit()
  13. sys.exit()
  14. screen.blit(g1,( 0, 0))
  15. t+= 1
  16. print(t)
  17. if t > 66:
  18. break;
  19. pygame.display.update()
  20. while( 1):
  21. timer.tick( 30)
  22. ticks = pygame.time.get_ticks();
  23. for event in pygame.event.get():
  24. if event. type == QUIT:
  25. pygame.quit()
  26. sys.exit()
  27. if event. type == MOUSEBUTTONUP:
  28. mouse_up = event.button
  29. mouse_up_x,mouse_up_y = event.pos
  30. if mouse_up_x > 245 and mouse_up_x < 469 and mouse_up_y> 368 and mouse_up_y < 470:
  31. return
  32. screen.blit(g2,( 0, 0))
  33. pygame.display.update()

游戏开始界面效果如下:

2、人物移动规则说明,可支持两位玩家

人物移动规则:

  • 守门员:就在球门边上来回走;
  • 负责上半场的球员:在上半场出现球的时候就往球的位置移动,如果捕获到了球,则往对方球门移动并随机射门,否则随机移动;
  • 负责下半场的球员:在下半场出现球的时候就往球的位置移动,如果捕获到了球,则往对方球门移动并随机射门,否则随机移动;
  • 负责全场的球员:往球的位置移动,如果捕获到了球,则往对方球门移动并随机射门。

操作说明:

  • 一号玩家,WASD + T 射门
  • 二号玩家,方向键 + K 射门

核心代码如下: 


  
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from pygame. locals import *
  4. from MyLibrary import *
  5. filename = 'p2.png'
  6. filename2 = 'p1.png'
  7. size_of_player = ( 32, 47.5)
  8. size_of_action = 4
  9. size_of_playground = ( 1200, 850)
  10. dict_ = {( 0, 0): 0,(- 1, 0): 3, ( 1, 0): 0, ( 0, 1): 2, ( 0, - 1): 1, (- 1, 1): 2, (- 1, - 1): 1, ( 1, - 1): 1, ( 1, 1): 2};
  11. def player2_AI( myball,player,game_over,player_moving,Reference):
  12. x_bias,y_bias,X,Y = Reference
  13. TEMP = [ 0, 0]
  14. player.direction = list(player.direction)
  15. keys = pygame.key.get_pressed()
  16. if keys[K_ESCAPE]: sys.exit()
  17. if keys[K_UP]: TEMP[ 0] = - 1
  18. if keys[K_RIGHT]: TEMP[ 1] = 1
  19. if keys[K_DOWN]: TEMP[ 0] = 1
  20. if keys[K_LEFT]: TEMP[ 1] = - 1
  21. if keys[K_k] and myball.player == player: myball.kick_off()
  22. if ([ 0, 0] == TEMP):
  23. player_moving = False
  24. else:
  25. player_moving = True
  26. if player_moving:
  27. player.direction = TEMP
  28. which_column = dict_[ tuple(player.direction)]
  29. if not game_over:
  30. # 根据角色的不同方向,使用不同的动画帧
  31. player.first_frame = which_column * player.columns
  32. player.last_frame = player.first_frame + player.columns - 1
  33. if player.frame < player.first_frame:
  34. player.frame = player.first_frame
  35. # print(player.direction)
  36. if player.X >= 0 and player.X <= 70 and player.Y >= 255 and player.Y <= 260:
  37. if player.direction[ 0] == 1:
  38. player.direction[ 0] = 0
  39. if player.X >= 70 and player.X <= 75 and player.Y >= 260 and player.Y <= 497:
  40. if player.direction[ 1] == - 1:
  41. player.direction[ 1] = 0
  42. if player.X >= 0 and player.X <= 70 and player.Y >= 497 and player.Y <= 502:
  43. if player.direction[ 0] == - 1:
  44. player.direction[ 0] = 0
  45. if player.X >= 1080 and player.X <= 1200 and player.Y >= 255 and player.Y < 260:
  46. if player.direction[ 0] == 1:
  47. player.direction[ 0] = 0
  48. if player.X > 1075 and player.X <= 1080 and player.Y >= 260 and player.Y < 503:
  49. if player.direction[ 1] == 1:
  50. player.direction[ 1] = 0
  51. if player.X >= 1080 and player.X <= 1200 and player.Y >= 503 and player.Y <= 507:
  52. if player.direction[ 0] == - 1:
  53. player.direction[ 0] = 0
  54. if not player_moving:
  55. # 当停止按键(即人物停止移动的时候),停止更新动画帧
  56. player.frame = player.last_frame= player.first_frame
  57. player.moving = False;
  58. else:
  59. player.moving = True;
  60. player.velocity.x = player.direction[ 1] * 2
  61. player.velocity.y = player.direction[ 0]* 2
  62. player.velocity.x *= 1
  63. player.velocity.y *= 1
  64. if player_moving:
  65. X += player.velocity.x
  66. Y += player.velocity.y
  67. if X < 0: X = 0
  68. if X > size_of_playground[ 0] - 48: X = size_of_playground[ 0] - 48
  69. if Y < 0: Y = 0
  70. if Y > size_of_playground[ 1] - 88: Y = size_of_playground[ 1] - 88
  71. player.X = X + x_bias
  72. player.Y = Y + y_bias
  73. # Reference = x_bias,y_bias,X,Y
  74. Reference[ 0] = x_bias
  75. Reference[ 1]= y_bias
  76. Reference[ 2] = X
  77. Reference[ 3] = Y
  78. def player1_AI( myball,player,game_over,player_moving,Reference):
  79. x_bias,y_bias,X,Y = Reference
  80. TEMP = [ 0, 0]
  81. player.direction = list(player.direction)
  82. keys = pygame.key.get_pressed()
  83. if keys[K_ESCAPE]: sys.exit()
  84. if keys[K_w]: TEMP[ 0] = - 1
  85. if keys[K_d]: TEMP[ 1] = 1
  86. if keys[K_s]: TEMP[ 0] = 1
  87. if keys[K_a]: TEMP[ 1] = - 1
  88. if keys[K_t] and myball.player == player: myball.kick_off()
  89. if ([ 0, 0] == TEMP):
  90. player_moving = False
  91. else:
  92. player_moving = True
  93. if player_moving:
  94. player.direction = TEMP
  95. which_column = dict_[ tuple(player.direction)]
  96. # print(player.direction)
  97. # print(which_column)
  98. if not game_over:
  99. # 根据角色的不同方向,使用不同的动画帧
  100. player.first_frame = which_column * player.columns
  101. player.last_frame = player.first_frame + player.columns - 1
  102. if player.frame < player.first_frame:
  103. player.frame = player.first_frame
  104. if player.X >= 0 and player.X <= 70 and player.Y >= 255 and player.Y <= 260:
  105. if player.direction[ 0] == 1:
  106. player.direction[ 0] = 0
  107. if player.X >= 70 and player.X <= 75 and player.Y >= 260 and player.Y <= 497:
  108. if player.direction[ 1] == - 1:
  109. player.direction[ 1] = 0
  110. if player.X >= 0 and player.X <= 70 and player.Y >= 497 and player.Y <= 502:
  111. if player.direction[ 0] == - 1:
  112. player.direction[ 0] = 0
  113. if player.X >= 1080 and player.X <= 1200 and player.Y >= 255 and player.Y < 260:
  114. if player.direction[ 0] == 1:
  115. player.direction[ 0] = 0
  116. if player.X > 1075 and player.X <= 1080 and player.Y >= 260 and player.Y < 503:
  117. if player.direction[ 1] == 1:
  118. player.direction[ 1] = 0
  119. if player.X >= 1080 and player.X <= 1200 and player.Y >= 503 and player.Y < 507:
  120. if player.direction[ 0] == - 1:
  121. player.direction[ 0] = 0
  122. if not player_moving:
  123. # 当停止按键(即人物停止移动的时候),停止更新动画帧
  124. player.frame = player.first_frame = player.last_frame
  125. player.moving = False;
  126. else:
  127. player.moving = True;
  128. player.velocity.x = player.direction[ 1] * 2
  129. player.velocity.y = player.direction[ 0]* 2
  130. player.velocity.x *= 1
  131. player.velocity.y *= 1
  132. if player_moving:
  133. X += player.velocity.x
  134. Y += player.velocity.y
  135. if X < 0: X = 0
  136. if X > size_of_playground[ 0] - 48: X = size_of_playground[ 0] - 48
  137. if Y < 0: Y = 0
  138. if Y > size_of_playground[ 1] - 88: Y = size_of_playground[ 1] - 88
  139. player.X = X + x_bias
  140. player.Y = Y + y_bias
  141. Reference[ 0] = x_bias
  142. Reference[ 1]= y_bias
  143. Reference[ 2] = X
  144. Reference[ 3] = Y

3、足球规则

状态说明:

  • 被球员捕获,跟着球员走;
  • 被球员踢出去之后根据球员踢的方向和设定的初速度进行减速运动,如果碰到边界则反方向弹出。

核心代码:


  
  1. from __future__ import unicode_literals
  2. import sys, time, random, math, pygame
  3. from pygame. locals import *
  4. from math import pow
  5. class ball(pygame.sprite.Sprite):
  6. def __init__( self):
  7. pygame.sprite.Sprite.__init__(self)
  8. self.image_list = []
  9. self.image = None
  10. self.frame = 0
  11. self.old_frame = 0
  12. self.first_frame = 0
  13. self.last_frame = 2
  14. self.direction = list([ 0, 0])
  15. self.speed = 0;
  16. self.fetch = False;
  17. self.f = 1.7
  18. self.last_time = 0;
  19. self.player = None
  20. self.cal = 0
  21. def _getx( self): return self.rect.x
  22. def _setx( self,value):self.rect.x = value
  23. X = property(_getx,_setx)
  24. #Y property
  25. def _gety( self): return self.rect.y
  26. def _sety( self,value):self.rect.y = value
  27. Y = property(_gety,_sety)
  28. #position property
  29. def _getpos( self): return self.rect.topleft
  30. def _setpos( self,pos): self.rect.topleft = pos
  31. position = property(_getpos,_setpos)
  32. def load( self):
  33. filename = 'ball1.png', 'ball2.png', 'ball3.png'
  34. for x in filename:
  35. ball = pygame.image.load(x).convert_alpha()
  36. self.image_list.append(ball)
  37. self.frame = 0;
  38. self.old_frame = 2;
  39. self.image = self.image_list[ 0];
  40. self.frame_height = self.image_list[ 0].get_rect().height
  41. self.frame_width = self.image_list[ 0].get_rect().width
  42. self.rect = Rect( 0, 0,self.frame_width,self.frame_height);
  43. def update( self,current_time,rate =30):
  44. if self.fetch and self.player.moving:
  45. self.speed = (self.player.velocity.x ** 2 + self.player.velocity.y ** 2)**( 1/ 2)
  46. if self.speed == 0 or (self.fetch and self.player.moving == False):
  47. return
  48. if current_time > self.last_time + ( 4-self.speed// 4)* 20:
  49. self.frame += 1
  50. self.frame %= 3
  51. self.last_time = current_time
  52. if self.frame != self.old_frame:
  53. self.image = self.image_list[self.frame]
  54. self.old_frame = self.frame
  55. def run( self):
  56. self.speed -= self.f* 0.05;
  57. self.speed = max( 0,self.speed)
  58. if(self.direction==[ 0, 0]): return;
  59. # print(self.direction)
  60. # print(self.speed)
  61. self.X += ((self.direction[ 0]*self.speed)/ pow((self.direction[ 1]** 2 + self.direction[ 0]** 2),( 1/ 2)))
  62. self.Y += ((self.direction[ 1]*self.speed)/ pow((self.direction[ 0]** 2 + self.direction[ 1]** 2),( 1/ 2)))
  63. def fetched( self,player_):
  64. self.fetch = True;
  65. if player_ != None:
  66. self.player = player_
  67. player = self.player
  68. if(player.direction[ 1] > 0):
  69. self.X = self.player.X + self.player.frame_width* 3/ 4
  70. else :
  71. self.X = self.player.X - self.player.frame_width/ 3
  72. self.Y = self.player.Y + self.player.frame_height -self.frame_height;
  73. def kick_off( self):
  74. self.speed = 12
  75. self.direction[ 0] = self.player.direction[ 1]
  76. self.direction[ 1] =self.player.direction[ 0]
  77. self.player = None
  78. self.fetch = False
  79. self.cal = 0
  80. def check_bound( self,width,height):
  81. temp = self.X,self.Y
  82. if self.X < 0:
  83. self.X = 0
  84. self.direction[ 0] = abs(self.direction[ 0])
  85. if self.Y < 0:
  86. self.Y = 0
  87. self.direction[ 1] = abs(self.direction[ 1])
  88. if self.X >width- 34:
  89. self.X= width- 34
  90. self.direction[ 0] = - 1* abs(self.direction[ 0])
  91. if self.Y > height- 14:
  92. self.Y = height- 14;
  93. self.direction[ 1] = - 1* abs(self.direction[ 1])
  94. if self.X >= 0 and self.X < 72 and self.Y > 300 - 17 and self.Y < 315 - 17:
  95. self.Y = 300- 17
  96. self.direction[ 1] = - 1* abs(self.direction[ 1])
  97. if self.X > 1110 and self.X < 1200 and self.Y > 300 - 17 and self.Y < 315 - 17:
  98. self.Y = 300- 17
  99. self.direction[ 1] = - 1* abs(self.direction[ 1])
  100. if self.X >= 0 and self.X < 72 and self.Y > 495 and self.Y < 510:
  101. self.Y = 510
  102. self.direction[ 1] = - 1* abs(self.direction[ 1])
  103. if self.X > 1110 and self.X < 1200 and self.Y > 495 and self.Y < 510:
  104. self.Y = 510
  105. self.direction[ 1] = - 1* abs(self.direction[ 1])
  106. if((self.X,self.Y) != temp):
  107. self.speed *= 0.8

4、主方法调取

整合调取上面三大核心代码,游戏运行只需操作该方法文件即可。

注意:全部的源代码会文末链接上,拿来就可以直接运行无需修改。

核心代码:


  
  1. if __name__ == '__main__':
  2. pygame.init()
  3. screen = pygame.display.set_mode(( 1200, 800))
  4. pygame.display.set_caption( "世界杯足球游戏-大数据联盟")
  5. font = pygame.font.Font( None, 36)
  6. myinit()
  7. timer = pygame.time.Clock()
  8. n1 = 0
  9. n2 = 0
  10. screen = pygame.display.set_mode(( 1200, 800))
  11. for x in range( 10000):
  12. t = begin_a_game(n1,n2);
  13. if t == 1:
  14. n1 += 1
  15. else:
  16. n2 += 1

三、游戏运行效果与比赛结果

1、游戏开始界面

2、下届世界杯预测比赛结果

世界杯足球游戏娱乐为主,切勿用它模拟真实比赛结果,以免造成不必要的误判。

文末惊喜:

通过模拟比赛,希望国足下一届世界杯能以3:1的比分战胜日本队。

游戏源代码下载:

https://download.csdn.net/download/xiaoweite1/87242585


  • 📢博客主页:https://lansonli.blog.csdn.net
  • 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
  • 📢本文由 Lansonli 原创,首发于 CSDN博客🙉
  • 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活✨

转载:https://blog.csdn.net/xiaoweite1/article/details/128179715
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场