世界杯太精彩了,带大家用Python做个足球游戏,边玩游戏边看比赛
【摘要】 Python零基础快速制作足球游戏(附源代码)前言卡塔尔世界杯正是进行得火热,十六强队伍已经诞生,后面就是越来越紧张的争夺八强的淘汰赛。目前爆冷的赛果让球迷一度情绪失落,比如:日本2-1战胜西班牙,韩国2-1战胜葡萄牙。这正是足球的魅力所在,结果只会给更努力的一方,过去的成绩在比赛不在起决定性的作用,亚洲强队越战越强,期望国足能在下届世界杯有出场的机会。没能看到国足在这届世界杯的球场奔驰,...
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、人物移动规则说明,可支持两位玩家
人物移动规则:
- 守门员:就在球门边上来回走;
- 负责上半场的球员:在上半场出现球的时候就往球的位置移动,如果捕获到了球,则往对方球门移动并随机射门,否则随机移动;
- 负责下半场的球员:在下半场出现球的时候就往球的位置移动,如果捕获到了球,则往对方球门移动并随机射门,否则随机移动;
- 负责全场的球员:往球的位置移动,如果捕获到了球,则往对方球门移动并随机射门。
操作说明:
- 一号玩家,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 = 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_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
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的比分战胜日本队。
游戏源代码下载:
推荐
华为开发者空间发布
让每位开发者拥有一台云主机
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)