文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-09-14 11:52

关注

文章目录

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 = 0    timer = 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+= 1        print(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.button                mouse_up_x,mouse_up_y = event.pos                if mouse_up_x > 245 and mouse_up_x < 469 and mouse_up_y> 368 and mouse_up_y < 470:                    return        screen.blit(g2,(0,0))        pygame.display.update()

游戏开始界面效果如下:

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

人物移动规则:

操作说明:

核心代码如下: 

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

3、足球规则

状态说明:

核心代码:

from __future__ import unicode_literalsimport sys, time, random, math, pygamefrom pygame.locals import *from math import powclass 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            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):            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 = 0    n2 =0    screen = pygame.display.set_mode((1200, 800))    for x in range(10000):        t = begin_a_game(n1,n2);        if t == 1:            n1 +=1        else:            n2 += 1

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

1、游戏开始界面

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

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

文末惊喜:

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

游戏源代码下载:

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


来源地址:https://blog.csdn.net/xiaoweite1/article/details/128179715

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯