前言
halo,包子们下午好
最近世界杯不是很火呀
很多小伙伴应该都知道球赛反正买,别墅靠大海!
今天就给大家实现一个类似世界杯的足球小游戏,咱就说真的堪比国足了!
哈哈哈~ 好啦
直接开整!!!
相关文件
关注小编,私信小编领取哟!
当然别忘了一件三连哟~~
开发工具
Python版本:3.7.8
相关模块:
pygame模块;
random模块;
pyttsx3模块;
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
开始界面
游戏界面
原理介绍
这是一个双人小游戏,分为红蓝两方。当然每队都有三个球员和一个守门员,每个玩家可以控制一个球员,其他三个是电脑控制。红方球员是通过“W”,“A”,“S”,“D”控制球员的移动,“T”踢球。蓝方球员是通过“↑”,“↓”,“←”,“→”控制球员的移动,“K”踢球。安空格键是开始游戏,下面是整个球场的效果图。
大概了解了游戏规则之后,我们就可以开始写这个游戏啦~首先,进行一下游戏初始化操作:
代码展示
游戏初始化
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()
部分代码展示
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
def draw_background(screen):
rc = (255, 255, 255)
rp = (600, 400)
rr = 80
width = 5
screen.fill((0,100,0))
pygame.draw.circle(screen, rc