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

news/2024/11/28 23:33:36/

文章目录

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、游戏开始界面

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

def myinit():screen = pygame.display.set_mode((769,563))g1 = pygame.image.load("g1.jpg").convert()g2 = pygame.image.load("hh.png").convert()t = 0timer = pygame.time.Clock()while(1):timer.tick(30)ticks = pygame.time.get_ticks();for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()screen.blit(g1,(0,0))t+= 1print(t)if t > 66:break;pygame.display.update()while(1):timer.tick(30)ticks = pygame.time.get_ticks();for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()if event.type == MOUSEBUTTONUP:mouse_up = event.buttonmouse_up_x,mouse_up_y = event.posif mouse_up_x > 245 and mouse_up_x < 469 and mouse_up_y> 368 and mouse_up_y < 470:returnscreen.blit(g2,(0,0))pygame.display.update()

游戏开始界面效果如下:

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

人物移动规则:

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

操作说明:

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

核心代码如下: 

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from pygame.locals import *
from MyLibrary import *
filename = 'p2.png'
filename2 = 'p1.png'
size_of_player = (32,47.5)
size_of_action = 4
size_of_playground = (1200,850)
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};
def player2_AI(myball,player,game_over,player_moving,Reference):x_bias,y_bias,X,Y = ReferenceTEMP = [0,0]player.direction = list(player.direction)keys = pygame.key.get_pressed()if keys[K_ESCAPE]: sys.exit()if keys[K_UP]: TEMP[0] = -1if keys[K_RIGHT]: TEMP[1] = 1if keys[K_DOWN]: TEMP[0] = 1if keys[K_LEFT]: TEMP[1] = -1if keys[K_k] and myball.player == player: myball.kick_off()if ([0,0] == TEMP):player_moving = Falseelse:player_moving = Trueif player_moving:player.direction = TEMP which_column = dict_[tuple(player.direction)]if not game_over:# 根据角色的不同方向,使用不同的动画帧player.first_frame = which_column * player.columnsplayer.last_frame = player.first_frame + player.columns - 1if player.frame < player.first_frame:player.frame = player.first_frame# print(player.direction)if player.X >=0  and player.X <= 70 and player.Y >=255 and player.Y <=260:if player.direction[0] == 1:player.direction[0] = 0if player.X >=70 and player.X <=75 and player.Y >=260 and player.Y <=497:if player.direction[1] == -1:player.direction[1] =0if player.X >=0  and player.X <= 70 and player.Y >=497 and player.Y <=502:if player.direction[0] == -1:player.direction[0] = 0if player.X >=1080 and player.X <= 1200 and player.Y >=255 and player.Y <260:if player.direction[0] == 1:player.direction[0] = 0if player.X > 1075 and player.X <= 1080 and player.Y >=260 and player.Y <  503:if player.direction[1] == 1:player.direction[1] =0if player.X >=1080 and player.X <= 1200 and player.Y >=503 and player.Y <=507:if player.direction[0] == -1:player.direction[0] = 0if not player_moving:# 当停止按键(即人物停止移动的时候),停止更新动画帧player.frame = player.last_frame= player.first_frame player.moving = False;else:player.moving = True;player.velocity.x = player.direction[1] * 2player.velocity.y = player.direction[0]*   2player.velocity.x *= 1player.velocity.y *= 1if player_moving:X += player.velocity.xY += player.velocity.yif X < 0: X = 0if X > size_of_playground[0] - 48: X = size_of_playground[0] - 48if Y < 0: Y = 0if Y > size_of_playground[1] - 88: Y = size_of_playground[1] - 88player.X = X + x_biasplayer.Y = Y + y_bias# Reference = x_bias,y_bias,X,YReference[0] = x_biasReference[1]= y_biasReference[2] = XReference[3] = Ydef player1_AI(myball,player,game_over,player_moving,Reference):x_bias,y_bias,X,Y = ReferenceTEMP = [0,0]player.direction = list(player.direction)keys = pygame.key.get_pressed()if keys[K_ESCAPE]: sys.exit()if keys[K_w]: TEMP[0] = -1if keys[K_d]: TEMP[1] = 1if keys[K_s]: TEMP[0] = 1if keys[K_a]: TEMP[1] = -1if keys[K_t] and myball.player == player: myball.kick_off()if ([0,0] == TEMP):player_moving = Falseelse:player_moving = Trueif player_moving:player.direction = TEMP which_column = dict_[tuple(player.direction)]# print(player.direction)# print(which_column)if not game_over:# 根据角色的不同方向,使用不同的动画帧player.first_frame = which_column * player.columnsplayer.last_frame = player.first_frame + player.columns - 1if player.frame < player.first_frame:player.frame = player.first_frameif player.X >=0  and player.X <= 70 and player.Y >=255 and player.Y <=260:if player.direction[0] == 1:player.direction[0] = 0if player.X >=70 and player.X <=75 and player.Y >=260 and player.Y <=497:if player.direction[1] == -1:player.direction[1] =0if player.X >=0  and player.X <= 70 and player.Y >=497 and player.Y <=502:if player.direction[0] == -1:player.direction[0] = 0if player.X >=1080 and player.X <= 1200 and player.Y >=255 and player.Y <260:if player.direction[0] == 1:player.direction[0] = 0if player.X > 1075 and player.X <= 1080 and player.Y >=260 and player.Y <503:if player.direction[1] == 1:player.direction[1] =0if player.X >=1080 and player.X <= 1200 and player.Y >=503 and player.Y <507:if player.direction[0] == -1:player.direction[0] = 0if not player_moving:# 当停止按键(即人物停止移动的时候),停止更新动画帧player.frame = player.first_frame = player.last_frameplayer.moving = False;else:player.moving = True;player.velocity.x = player.direction[1] * 2player.velocity.y = player.direction[0]*  2player.velocity.x *= 1player.velocity.y *= 1if player_moving:X += player.velocity.xY += player.velocity.yif X < 0: X = 0if X > size_of_playground[0] - 48: X = size_of_playground[0] - 48if Y < 0: Y = 0if Y > size_of_playground[1] - 88: Y = size_of_playground[1] - 88player.X = X + x_biasplayer.Y = Y + y_biasReference[0] = x_biasReference[1]= y_biasReference[2] = XReference[3] = Y

3、足球规则

状态说明:

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

核心代码:

from __future__ import unicode_literals
import sys, time, random, math, pygame
from pygame.locals import *
from math import pow
class ball(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image_list = []self.image = Noneself.frame = 0self.old_frame = 0self.first_frame = 0self.last_frame = 2self.direction = list([0,0])self.speed = 0;self.fetch = False;self.f = 1.7self.last_time = 0;self.player = Noneself.cal = 0def _getx(self): return self.rect.xdef _setx(self,value):self.rect.x = valueX = property(_getx,_setx)#Y propertydef _gety(self):return self.rect.ydef _sety(self,value):self.rect.y = valueY = property(_gety,_sety)#position propertydef _getpos(self): return self.rect.topleftdef _setpos(self,pos): self.rect.topleft = posposition = property(_getpos,_setpos)def load(self):filename = 'ball1.png','ball2.png','ball3.png'for x in filename:ball = pygame.image.load(x).convert_alpha()self.image_list.append(ball)self.frame = 0;self.old_frame = 2;self.image = self.image_list[0];self.frame_height = self.image_list[0].get_rect().heightself.frame_width = self.image_list[0].get_rect().widthself.rect = Rect(0,0,self.frame_width,self.frame_height);def update(self,current_time,rate =30):if self.fetch and self.player.moving:self.speed = (self.player.velocity.x **2 + self.player.velocity.y **2)**(1/2)if self.speed == 0 or (self.fetch and self.player.moving == False):returnif current_time > self.last_time + (4-self.speed//4)*20:self.frame += 1self.frame %= 3self.last_time = current_timeif self.frame != self.old_frame:self.image = self.image_list[self.frame]self.old_frame = self.framedef run(self):self.speed -= self.f*0.05;self.speed = max(0,self.speed)if(self.direction==[0,0]):return;# print(self.direction)# print(self.speed)self.X += ((self.direction[0]*self.speed)/pow((self.direction[1]**2 + self.direction[0]**2),(1/2)))self.Y += ((self.direction[1]*self.speed)/pow((self.direction[0]**2 + self.direction[1]**2),(1/2)))def fetched(self,player_):self.fetch = True;if player_ != None:self.player = player_player = self.playerif(player.direction[1] >0):self.X = self.player.X + self.player.frame_width*3/4else :self.X = self.player.X - self.player.frame_width/3self.Y = self.player.Y + self.player.frame_height -self.frame_height;def kick_off(self):self.speed = 12self.direction[0] = self.player.direction[1]self.direction[1]  =self.player.direction[0]self.player = Noneself.fetch =Falseself.cal = 0def check_bound(self,width,height):temp = self.X,self.Yif self.X < 0:self.X =0self.direction[0] = abs(self.direction[0])if self.Y < 0:self.Y = 0self.direction[1] = abs(self.direction[1])if self.X >width-34:self.X= width-34self.direction[0] = -1*abs(self.direction[0])if self.Y > height-14:self.Y = height-14;self.direction[1] = -1*abs(self.direction[1])if self.X >=0 and self.X <72 and self.Y >300 - 17 and self.Y <315 - 17:self.Y = 300-17self.direction[1] = -1*abs(self.direction[1])if self.X >1110 and self.X <1200 and self.Y >300 - 17 and self.Y <315 - 17:self.Y = 300-17self.direction[1] = -1*abs(self.direction[1])if self.X >=0 and self.X <72 and self.Y >495 and self.Y <510:self.Y = 510 self.direction[1] = -1*abs(self.direction[1])if self.X >1110 and self.X <1200 and self.Y >495 and self.Y <510:self.Y = 510self.direction[1] = -1*abs(self.direction[1])if((self.X,self.Y) != temp):self.speed *= 0.8

4、主方法调取

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

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

核心代码:

if __name__ == '__main__':pygame.init()screen = pygame.display.set_mode((1200, 800))pygame.display.set_caption("世界杯足球游戏-大数据联盟")font = pygame.font.Font(None, 36)myinit()timer = pygame.time.Clock()n1 = 0n2 =0screen = pygame.display.set_mode((1200, 800))for x in range(10000):t = begin_a_game(n1,n2);if t == 1:n1 +=1else:n2 += 1

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

1、游戏开始界面

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

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

文末惊喜:

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

游戏源代码下载:

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


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

http://www.ppmy.cn/news/1629.html

相关文章

毫米波传感器原理介绍:测速_1相位

在前文中&#xff0c;我们分析了 IF信号的频率&#xff0c;并展示了该频率与物体到雷达的距离 成正比。在本文中&#xff0c;我们将探讨IF 信号的相位。如果我们希望了解 FMCW 雷达响应物体极小位移的能力&#xff0c;那么研究相位就非常重要。雷达正是凭此非常快速且准确地测量…

sql serve数据库基础入门(2)

前言 &#x1f388;个人主页:&#x1f388; :✨✨✨初阶牛✨✨✨ &#x1f43b;推荐专栏: &#x1f354;&#x1f35f;&#x1f32f; c语言初阶 &#x1f511;个人信条: &#x1f335;知行合一 &#x1f349;本篇简介:>: 在上一篇的最后,牛牛介绍了怎么创建表,本篇牛牛介绍如…

新手入门SLAM必备资料

新手入门SLAM必备资料 文章目录 新手入门SLAM必备资料一、SLAM学习书籍1.必读经典2.有很多期,跟着会议一起出的文集3.入门书籍,简单实现及代码4.SLAM入门教材吐血推荐,对深入理解SLAM实质非常有帮助5.作者Joan Sola关于Graph-SLAM的教程,包含位姿变换、传感器模型、图优化以…

【Unity3D】绘制物体表面三角形网格

1 仅绘制三角形网格 1&#xff09;创建游戏对象 创建一个空对象&#xff0c;重命名为 Grid&#xff0c;并在其下添加需要绘制网格的对象&#xff0c;如下&#xff1a; 场景显示如下&#xff1a; 2&#xff09;添加脚本组件 GridController.cs using System; using UnityEngin…

Python Gui之tkinter(下)

6.Radiobutton单按按钮 Radiobutton控件用于选择同一组单选按钮中的一个。Radiobutton可以显示文本&#xff0c;也可以显示图像。 7.Checkbutton复选按钮 Checkbutton控件用于选择多个按钮的情况。Checkbutton可以显示文本&#xff0c;也可以显示图像。 经典的Gui类的写法&a…

详解C语言二级指针三种内存模型

二级指针相对于一级指针&#xff0c;显得更难&#xff0c;难在于指针和数组的混合&#xff0c;定义不同类型的二级指针&#xff0c;在使用的时候有着很大的区别 第一种内存模型char *arr[] 若有如下定义 char *arr[] {"abc", "def", "ghi"};…

第一期 | 整洁,从桌面开始

文章目录前言一、主要内容介绍二、文件分类&#xff0c;整理你的桌面1.网格对齐图标&#xff0c;取消自动排列2.保持工作状态&#xff0c;提取近期文件3.用好排序&#xff0c;让文件一目了然4.分类整理&#xff0c;让文件听你的话5.按照实际情况作调整三、合理归档&#xff0c;…

Android 面试中Binder必问,是否了解过?

1.简单介绍下binder binder是一种进程间通讯的机制 进程间通讯需要了解用户空间和内核空间 每个进程拥有自己的独立虚拟机&#xff0c;系统为他们分配的地址空间都是互相隔离的。 如两个进程需要进行通讯&#xff0c;则需要使用到内核空间做载体&#xff0c;内核空间是所有进…