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、足球规则
状态说明:
被球员捕获,跟着球员走;
被球员踢出去之后根据球员踢的方向和设定的初速度进行减速运动,如果碰到边界则反方向弹出。
————————————————
版权声明:本文为CSDN博主「Lansonli」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:/xiaoweite1/article/details/128179715
如果觉得《Python零基础快速制作足球游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!