【Python游戏】一个csdn小编用Python语言写了一个足球游戏,成功模拟世界杯决赛现场

时间:2022-12-10 08:08:50

前言

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

相关文件

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

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

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

开发工具

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

环境搭建

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

效果展示

游戏玩法

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

开始界面

【Python游戏】一个csdn小编用Python语言写了一个足球游戏,成功模拟世界杯决赛现场
【Python游戏】一个csdn小编用Python语言写了一个足球游戏,成功模拟世界杯决赛现场

游戏界面

【Python游戏】一个csdn小编用Python语言写了一个足球游戏,成功模拟世界杯决赛现场

代码展示

导入模块

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

玩家类

class MySprite(pygame.sprite.Sprite):
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #extend the base Sprite class
        self.master_image = None
        self.frame = 0
        self.old_frame = -1
        self.frame_width = 1
        self.frame_height = 1
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0
        self.direction = list([0,0])
        self.velocity = Point(0.0,0.0) 
        self.moving = False
        self.state = None
        self.tag = None
    #X property
    def _getx(self): return self.rect.x
    def _setx(self,value): self.rect.x = value
    X = property(_getx,_setx)

    #Y property
    def _gety(self): return self.rect.y
    def _sety(self,value): self.rect.y = value
    Y = property(_gety,_sety)

    #position property
    def _getpos(self): return self.rect.topleft
    def _setpos(self,pos): self.rect.topleft = pos
    position = property(_getpos,_setpos)
        

    def load(self, filename, width, height, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()
        self.frame_width = width
        self.frame_height = height
        self.rect = Rect(0,0,width,height)
        self.columns = columns
        #try to auto-calculate total frames
        rect = self.master_image.get_rect()
        self.last_frame = (rect.width // width) * (rect.height // height) - 1

    def update(self, current_time, rate=30):
        #update animation frame number
        if current_time > self.last_time + rate:
            self.frame += 1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time

        #build current frame only if it changed
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width
            frame_y = (self.frame // self.columns) * self.frame_height
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
            self.image = self.master_image.subsurface(rect)
            self.old_frame = self.frame

    def __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 class
        self.master_image = None
        self.frame = 0
        self.goal  = None
        self.old_frame = -1
        self.ball = None
        self.leader = None
        self.frame_width = 1
        self.frame_height = 1
        self.goal_pos = None
        self.first_frame = 0
        self.last_frame = 0
        self.columns = 1
        self.last_time = 0
        self.direction = list([0,0])
        self.speed = 3
        self.moving = False
        self.state = "chase"
        self.tag = None
        self.other_group = None
    #X property
    def _getx(self): return self.rect.x
    def _setx(self,value): self.rect.x = value
    X = property(_getx,_setx)

    #Y property
    def _gety(self): return self.rect.y
    def _sety(self,value): self.rect.y = value
    Y = property(_gety,_sety)

    #position property
    def _getpos(self): return self.rect.topleft
    def _setpos(self,pos): self.rect.topleft = pos
    position = property(_getpos,_setpos)
        

    def load(self, filename, width, height, columns):
        self.master_image = pygame.image.load(filename).convert_alpha()
        self.frame_width = width
        self.frame_height = height
        self.rect = Rect(0,0,width,height)
        self.columns = columns
        #try to auto-calculate total frames
        rect = self.master_image.get_rect()
        self.last_frame = (rect.width // width) * (rect.height // height) - 1

    def 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()
            return
        self.which_frame()
        if current_time > self.last_time + rate:
            self.frame += 1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time

        #build current frame only if it changed
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns) * self.frame_width
            frame_y = (self.frame // self.columns) * self.frame_height
            rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
            self.image = self.master_image.subsurface(rect)
            self.old_frame = self.frame

    def __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 2
        for 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.columns
        self.last_frame = self.first_frame + self.columns - 1
        if self.frame < self.first_frame:
            self.frame = self.first_frame
    def run(self):
        if self.direction == [0,0]:
            return
        if self.X >=0 and self.X < 70 and self.Y >=260 and self.Y <265:
            if self.direction[0] == 1:
                self.direction[0] = 0
            if 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] =0
        if 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] = 0
                if 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] = 0
            if 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] =0
        if 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] = 0
                if self.ball.Y<400:
                    self.direction[1] = -1;
        self.Y += self.direction[0]*self.speed
        self.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]=1
        elif self.direction[0]<0:
            self.direction[0] = -1
        if self.direction[1]>0:
            self.direction[1] =1
        elif 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 = 2
            else:self.speed = 1
        else:
            if self.X > 600:
                self.speed = 2
            else :
                self.speed =1

机器人B

class robot_B(robot):
    def __init__(self):
        robot.__init__(self)
        self.wonder = None
        self.speed =1
    def wondering(self):
        if self.X > self.wonder+1 or self.X <self.wonder -1 :
            self.direction = [560-self.Y,self.wonder- self.X]
        elif self.Y <280:
            self.direction = [560-self.Y,0]
        elif self.Y > 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]=1
        elif self.direction[0]<0:
            self.direction[0] = -1
        if self.direction[1]>0:
            self.direction[1] =1
        elif self.direction[1]<0:
            self.direction[1] =-1
        self.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 = None
        self.frame = 0
        self.old_frame = 0
        self.first_frame = 0
        self.last_frame = 2
        self.direction = list([0,0])
        self.speed = 0;
        self.fetch = False;
        self.f = 1.7
        self.last_time = 0;
        self.player = None
        self.cal = 0
    def _getx(self): return self.rect.x
    def _setx(self,value):self.rect.x = value
    X = property(_getx,_setx)

    #Y property
    def _gety(self):return self.rect.y
    def _sety(self,value):self.rect.y = value
    Y = property(_gety,_sety)

    #position property
    def _getpos(self): return self.rect.topleft
    def _setpos(self,pos): self.rect.topleft = pos
    position = 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().height
        self.frame_width = self.image_list[0].get_rect().width
        self.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):
            return
        if current_time > self.last_time + (4-self.speed//4)*20:
        	self.frame += 1
        	self.frame %= 3
        	self.last_time = current_time
        if 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.player
        if(player.direction[1] >0):
        	self.X = self.player.X + self.player.frame_width*3/4
        else :
        	self.X = self.player.X - self.player.frame_width/3
        self.Y = self.player.Y + self.player.frame_height -self.frame_height;
    def kick_off(self):
        self.speed = 12
        self.direction[0] = self.player.direction[1]
        self.direction[1]  =self.player.direction[0]
        self.player = None
        self.fetch =False
        self.cal = 0
    def check_bound(self,width,height):
        temp = self.X,self.Y
        if self.X < 0:
            self.X =0
            self.direction[0] = abs(self.direction[0])
        if self.Y < 0:
            self.Y = 0
            self.direction[1] = abs(self.direction[1])
        if self.X >width-34:
            self.X= width-34
            self.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-17
            self.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-17
            self.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 = 510
            self.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

总结

部分代码展示在上面了,因为源码太多就不一一给大家展示了
【Python游戏】一个csdn小编用Python语言写了一个足球游戏,成功模拟世界杯决赛现场
源码领取看相关文件哟