【Python游戏】一个csdn小编用Python语言写了一个足球游戏,成功模拟世界杯决赛现场
创始人
2024-03-27 04:08:34
0

前言

halo,包子们下午好
最近世界杯不是很火呀
很多小伙伴应该都知道球赛反正买,别墅靠大海!
今天就给大家实现一个类似世界杯的足球小游戏,咱就说真的堪比国足了!
哈哈哈~ 好啦
直接开整!!!

相关文件

关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
公众号:Python日志

源码点击蓝色字体领取
Python零基础入门到精通视频合集

【整整800集】Python爬虫项目零基础入门合集,细狗都学会了,你还不会?

开发工具

Python版本:3.7.8
相关模块:
pygame模块;
random模块;
pyttsx3模块;
以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

效果展示

游戏玩法

这是一个双人小游戏,分为红蓝两方。当然每队都有三个球员和一个守门员,每个玩家可以控制一个球员,其他三个是电脑控制。红方球员是通过“W”,“A”,“S”,“D”控制球员的移动,“T”踢球。蓝方球员是通过“↑”,“↓”,“←”,“→”控制球员的移动,“K”踢球。安空格键是开始游戏,下面是整个球场的效果图。
大概了解了游戏规则之后,我们就可以开始写这个游戏啦

开始界面

在这里插入图片描述
在这里插入图片描述

游戏界面

在这里插入图片描述

代码展示

导入模块

from __future__ import unicode_literals
import itertools, sys, time, random, math, pygame
from pygame.locals import *
from MyLibrary import *
from football import *
from IO import*

坐标

def bias(x, y):x = x + 16y = y + 24x_cent = 0y_cent = 0if (x >= 100 and x <= 700):x_cent = -1 * x + 100elif (x > 700):x_cent = -600else:x_cent = 0if (y >= 75 and y <= 525):y_cent = -1 * y + 75elif (y > 525):y_cent = -450else:y_cent = 0return x_cent, y_cent

玩家类

class MySprite(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self) #extend the base Sprite classself.master_image = Noneself.frame = 0self.old_frame = -1self.frame_width = 1self.frame_height = 1self.first_frame = 0self.last_frame = 0self.columns = 1self.last_time = 0self.direction = list([0,0])self.velocity = Point(0.0,0.0) self.moving = Falseself.state = Noneself.tag = None#X propertydef _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, width, height, columns):self.master_image = pygame.image.load(filename).convert_alpha()self.frame_width = widthself.frame_height = heightself.rect = Rect(0,0,width,height)self.columns = columns#try to auto-calculate total framesrect = self.master_image.get_rect()self.last_frame = (rect.width // width) * (rect.height // height) - 1def update(self, current_time, rate=30):#update animation frame numberif current_time > self.last_time + rate:self.frame += 1if self.frame > self.last_frame:self.frame = self.first_frameself.last_time = current_time#build current frame only if it changedif self.frame != self.old_frame:frame_x = (self.frame % self.columns) * self.frame_widthframe_y = (self.frame // self.columns) * self.frame_heightrect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)self.image = self.master_image.subsurface(rect)self.old_frame = self.framedef __str__(self):return str(self.frame) + "," + str(self.first_frame) + \"," + str(self.last_frame) + "," + str(self.frame_width) + \"," + str(self.frame_height) + "," + str(self.columns) + \"," + str(self.rect)

机器人A

class robot(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self) #extend the base Sprite classself.master_image = Noneself.frame = 0self.goal  = Noneself.old_frame = -1self.ball = Noneself.leader = Noneself.frame_width = 1self.frame_height = 1self.goal_pos = Noneself.first_frame = 0self.last_frame = 0self.columns = 1self.last_time = 0self.direction = list([0,0])self.speed = 3self.moving = Falseself.state = "chase"self.tag = Noneself.other_group = None#X propertydef _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, width, height, columns):self.master_image = pygame.image.load(filename).convert_alpha()self.frame_width = widthself.frame_height = heightself.rect = Rect(0,0,width,height)self.columns = columns#try to auto-calculate total framesrect = self.master_image.get_rect()self.last_frame = (rect.width // width) * (rect.height // height) - 1def update(self, current_time, rate=30):#update animation frame number# print("tag :"+str(self.tag))# print(self.direction)# print(self.speed)self.behavior()if self.state == "back":self.state ="chase"if self.direction == [0,0]:self.still()returnself.which_frame()if current_time > self.last_time + rate:self.frame += 1if self.frame > self.last_frame:self.frame = self.first_frameself.last_time = current_time#build current frame only if it changedif self.frame != self.old_frame:frame_x = (self.frame % self.columns) * self.frame_widthframe_y = (self.frame // self.columns) * self.frame_heightrect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)self.image = self.master_image.subsurface(rect)self.old_frame = self.framedef __str__(self):return str(self.frame) + "," + str(self.first_frame) + \"," + str(self.last_frame) + "," + str(self.frame_width) + \"," + str(self.frame_height) + "," + str(self.columns) + \"," + str(self.rect)def still(self):self.frame= self.last_frame = self.first_frame def chase(self):self.direction = [self.ball.Y - self.Y ,self.ball.X- self.X]def catch(self):t = self.detected()if t == 2:self.direction = [self.goal[1] -self.Y+random.randint(-10,10),self.goal[0]- self.X+random.randint(-10,10)]self.ball.kick_off()self.state = "chase"elif t == 1:self.direction = [self.leader.Y -self.Y+random.randint(-10,10),self.leader.X - self.X +random.randint(-10,10)]self.ball.kick_off()self.state = "chase"else:self.direction = [self.goal_pos[1] -self.Y,self.goal_pos[0]- self.X]#     if detected() == 1:#         kick_off()# def kick_off(self):#     if random.randint(1,10) < 5:#         self.direction = [self.leader.Y -self.Y,self.leader.X - self.X ]#     self.ball.kick_off(self)#     self.state = "chase"def distant(self,p):return ((self.X - p.X)**2 + (self.Y - p.Y)**2)**(1/2)def detected(self):if self.X < 400 and self.tag  == 2 and self.X > 100:return 2;if self.X > 780 and self.tag == 1 and self.X < 1100:return 2for x in self.other_group:if self.distant(x) < 100:return 1;return 3;def which_frame(self):which_column = dict_[tuple(self.direction)]self.first_frame = which_column * self.columnsself.last_frame = self.first_frame + self.columns - 1if self.frame < self.first_frame:self.frame = self.first_framedef run(self):if self.direction == [0,0]:returnif self.X >=0 and self.X < 70 and self.Y >=260 and self.Y <265:if self.direction[0] == 1:self.direction[0] = 0if self.ball.Y >400:self.direction[1] = 1;if self.X >= 70 and self.X < 75 and self.Y >=260 and self.Y <265:if self.direction == [1,-1]:if self.ball.Y > 400:self.direction = [1,0]else:self.direction = [0,-1]if self.X >70 and self.X <75 and self.Y >=265 and self.Y <492:if self.direction[1] == -1:self.direction[1] =0if self.X >= 70 and self.X < 75 and self.Y >=492 and self.Y <497:if self.direction == [-1,-1]:if self.ball.Y > 400:self.direction = [0,-1]else: self.direction = [-1,0]if self.X >=0  and self.X < 70 and self.Y >=492 and self.Y <497:if self.direction[0] == -1:self.direction[0] = 0if self.ball.Y<400:self.direction[1] = 1;if self.X >=1080 and self.X <= 1200 and self.Y >=260 and self.Y <265:if self.direction[0] == 1:self.direction[0] = 0if self.ball.Y >400:self.direction[1] = -1;if self.X > 1075 and self.X <= 1080 and self.Y >=260 and self.Y <265:if self.direction == [1,1]:if self.ball.Y > 400:self.direction = [1,0]else:self.direction = [0,1]if self.X > 1075 and self.X <= 1080 and self.Y >=265 and self.Y <  498:if self.direction[1] == 1:self.direction[1] =0if self.X > 1075 and self.X<= 1080 and self.Y >=498 and self.Y <502:if self.direction == [-1,1]:if self.ball.Y > 400:self.direction = [0,1]else: self.direction = [-1,0]if self.X >=1080 and self.X <= 1200 and self.Y >=498 and self.Y <502:if self.direction[0] == -1:self.direction[0] = 0if self.ball.Y<400:self.direction[1] = -1;self.Y += self.direction[0]*self.speedself.X += self.direction[1]*self.speed# print(str(self.X)+"   "+str(self.Y))def back(self):self.direction = [0,600 - self.X]def behavior(self):if self.state =="back":self.back()elif self.state == "chase":self.chase()elif self.state == "catch":self.catch()if self.direction[0]>0:self.direction[0]=1elif self.direction[0]<0:self.direction[0] = -1if self.direction[1]>0:self.direction[1] =1elif self.direction[1]<0:self.direction[1] =-1# print(self.tag)# print(self.direction)self.ch_sp()self.run()def ch_sp(self):if self.tag == 1:if self.X <600:self.speed = 2else:self.speed = 1else:if self.X > 600:self.speed = 2else :self.speed =1

机器人B

class robot_B(robot):def __init__(self):robot.__init__(self)self.wonder = Noneself.speed =1def wondering(self):if self.X > self.wonder+1 or self.X  550:self.direction = [270-self.Y,0]def behavior(self):if self.state =="back":self.back()elif (self.ball.X > 600 and self.tag == 1) or (self.ball.X < 600 and self.tag == 2):self.wondering()else:if self.state == "chase":self.chase()elif self.state == "catch":self.catch()if self.direction[0]>0:self.direction[0]=1elif self.direction[0]<0:self.direction[0] = -1if self.direction[1]>0:self.direction[1] =1elif self.direction[1]<0:self.direction[1] =-1self.run()def catch(self):if random.randint(1,10)<5:self.direction = [self.goal[1] -self.Y,self.goal[0]- self.X]self.ball.kick_off()self.state = "chase"else:self.direction = [self.leader.Y -self.Y,self.leader.X - self.X ]self.ball.kick_off()self.state = "chase"

球类

# -*- coding: utf-8 -*-
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.frame# print(self.frame)def 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):# print(str(self.X)+" "+str(self.Y))# print(temp)self.speed *= 0.8

总结

部分代码展示在上面了,因为源码太多就不一一给大家展示了
在这里插入图片描述
源码领取看相关文件哟

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...