基于python快速开发足球游戏代码记录

2024-03-28 09:10

本文主要是介绍基于python快速开发足球游戏代码记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要模块:

pygame

安装步骤:python -m pip install --upgrade pip

def myinit():screen = pygame.display.set_mode((769,563))g1 = pygame.image.load("g1.jpg").convert()g2 = pygame.image.load("hh.png").convert()t = 0timer = 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+= 1print(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.buttonmouse_up_x,mouse_up_y = event.posif mouse_up_x > 245 and mouse_up_x < 469 and mouse_up_y> 368 and mouse_up_y < 470:returnscreen.blit(g2,(0,0))pygame.display.update()
# -*- 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 = ReferenceTEMP = [0,0]player.direction = list(player.direction)keys = pygame.key.get_pressed()if keys[K_ESCAPE]: sys.exit()if keys[K_UP]: TEMP[0] = -1if keys[K_RIGHT]: TEMP[1] = 1if keys[K_DOWN]: TEMP[0] = 1if keys[K_LEFT]: TEMP[1] = -1if keys[K_k] and myball.player == player: myball.kick_off()if ([0,0] == TEMP):player_moving = Falseelse:player_moving = Trueif player_moving:player.direction = TEMP which_column = dict_[tuple(player.direction)]if not game_over:# 根据角色的不同方向,使用不同的动画帧player.first_frame = which_column * player.columnsplayer.last_frame = player.first_frame + player.columns - 1if 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] = 0if player.X >=70 and player.X <=75 and player.Y >=260 and player.Y <=497:if player.direction[1] == -1:player.direction[1] =0if player.X >=0  and player.X <= 70 and player.Y >=497 and player.Y <=502:if player.direction[0] == -1:player.direction[0] = 0if player.X >=1080 and player.X <= 1200 and player.Y >=255 and player.Y <260:if player.direction[0] == 1:player.direction[0] = 0if player.X > 1075 and player.X <= 1080 and player.Y >=260 and player.Y <  503:if player.direction[1] == 1:player.direction[1] =0if player.X >=1080 and player.X <= 1200 and player.Y >=503 and player.Y <=507:if player.direction[0] == -1:player.direction[0] = 0if not player_moving:# 当停止按键(即人物停止移动的时候),停止更新动画帧player.frame = player.last_frame= player.first_frame player.moving = False;else:player.moving = True;player.velocity.x = player.direction[1] * 2player.velocity.y = player.direction[0]*   2player.velocity.x *= 1player.velocity.y *= 1if player_moving:X += player.velocity.xY += player.velocity.yif X < 0: X = 0if X > size_of_playground[0] - 48: X = size_of_playground[0] - 48if Y < 0: Y = 0if Y > size_of_playground[1] - 88: Y = size_of_playground[1] - 88player.X = X + x_biasplayer.Y = Y + y_bias# Reference = x_bias,y_bias,X,YReference[0] = x_biasReference[1]= y_biasReference[2] = XReference[3] = Ydef player1_AI(myball,player,game_over,player_moving,Reference):x_bias,y_bias,X,Y = ReferenceTEMP = [0,0]player.direction = list(player.direction)keys = pygame.key.get_pressed()if keys[K_ESCAPE]: sys.exit()if keys[K_w]: TEMP[0] = -1if keys[K_d]: TEMP[1] = 1if keys[K_s]: TEMP[0] = 1if keys[K_a]: TEMP[1] = -1if keys[K_t] and myball.player == player: myball.kick_off()if ([0,0] == TEMP):player_moving = Falseelse:player_moving = Trueif 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.columnsplayer.last_frame = player.first_frame + player.columns - 1if player.frame < player.first_frame:player.frame = player.first_frameif player.X >=0  and player.X <= 70 and player.Y >=255 and player.Y <=260:if player.direction[0] == 1:player.direction[0] = 0if player.X >=70 and player.X <=75 and player.Y >=260 and player.Y <=497:if player.direction[1] == -1:player.direction[1] =0if player.X >=0  and player.X <= 70 and player.Y >=497 and player.Y <=502:if player.direction[0] == -1:player.direction[0] = 0if player.X >=1080 and player.X <= 1200 and player.Y >=255 and player.Y <260:if player.direction[0] == 1:player.direction[0] = 0if player.X > 1075 and player.X <= 1080 and player.Y >=260 and player.Y <503:if player.direction[1] == 1:player.direction[1] =0if player.X >=1080 and player.X <= 1200 and player.Y >=503 and player.Y <507:if player.direction[0] == -1:player.direction[0] = 0if not player_moving:# 当停止按键(即人物停止移动的时候),停止更新动画帧player.frame = player.first_frame = player.last_frameplayer.moving = False;else:player.moving = True;player.velocity.x = player.direction[1] * 2player.velocity.y = player.direction[0]*  2player.velocity.x *= 1player.velocity.y *= 1if player_moving:X += player.velocity.xY += player.velocity.yif X < 0: X = 0if X > size_of_playground[0] - 48: X = size_of_playground[0] - 48if Y < 0: Y = 0if Y > size_of_playground[1] - 88: Y = size_of_playground[1] - 88player.X = X + x_biasplayer.Y = Y + y_biasReference[0] = x_biasReference[1]= y_biasReference[2] = XReference[3] = Y
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 = Noneself.frame = 0self.old_frame = 0self.first_frame = 0self.last_frame = 2self.direction = list([0,0])self.speed = 0;self.fetch = False;self.f = 1.7self.last_time = 0;self.player = Noneself.cal = 0def _getx(self): return self.rect.xdef _setx(self,value):self.rect.x = valueX = property(_getx,_setx)#Y propertydef _gety(self):return self.rect.ydef _sety(self,value):self.rect.y = valueY = property(_gety,_sety)#position propertydef _getpos(self): return self.rect.topleftdef _setpos(self,pos): self.rect.topleft = posposition = 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().heightself.frame_width = self.image_list[0].get_rect().widthself.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):returnif current_time > self.last_time + (4-self.speed//4)*20:self.frame += 1self.frame %= 3self.last_time = current_timeif self.frame != self.old_frame:self.image = self.image_list[self.frame]self.old_frame = self.framedef 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.playerif(player.direction[1] >0):self.X = self.player.X + self.player.frame_width*3/4else :self.X = self.player.X - self.player.frame_width/3self.Y = self.player.Y + self.player.frame_height -self.frame_height;def kick_off(self):self.speed = 12self.direction[0] = self.player.direction[1]self.direction[1]  =self.player.direction[0]self.player = Noneself.fetch =Falseself.cal = 0def check_bound(self,width,height):temp = self.X,self.Yif self.X < 0:self.X =0self.direction[0] = abs(self.direction[0])if self.Y < 0:self.Y = 0self.direction[1] = abs(self.direction[1])if self.X >width-34:self.X= width-34self.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-17self.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-17self.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 = 510self.direction[1] = -1*abs(self.direction[1])if((self.X,self.Y) != temp):self.speed *= 0.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 = Noneself.frame = 0self.old_frame = 0self.first_frame = 0self.last_frame = 2self.direction = list([0,0])self.speed = 0;self.fetch = False;self.f = 1.7self.last_time = 0;self.player = Noneself.cal = 0def _getx(self): return self.rect.xdef _setx(self,value):self.rect.x = valueX = property(_getx,_setx)#Y propertydef _gety(self):return self.rect.ydef _sety(self,value):self.rect.y = valueY = property(_gety,_sety)#position propertydef _getpos(self): return self.rect.topleftdef _setpos(self,pos): self.rect.topleft = posposition = 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().heightself.frame_width = self.image_list[0].get_rect().widthself.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):returnif current_time > self.last_time + (4-self.speed//4)*20:self.frame += 1self.frame %= 3self.last_time = current_timeif self.frame != self.old_frame:self.image = self.image_list[self.frame]self.old_frame = self.framedef 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.playerif(player.direction[1] >0):self.X = self.player.X + self.player.frame_width*3/4else :self.X = self.player.X - self.player.frame_width/3self.Y = self.player.Y + self.player.frame_height -self.frame_height;def kick_off(self):self.speed = 12self.direction[0] = self.player.direction[1]self.direction[1]  =self.player.direction[0]self.player = Noneself.fetch =Falseself.cal = 0def check_bound(self,width,height):temp = self.X,self.Yif self.X < 0:self.X =0self.direction[0] = abs(self.direction[0])if self.Y < 0:self.Y = 0self.direction[1] = abs(self.direction[1])if self.X >width-34:self.X= width-34self.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-17self.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-17self.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 = 510self.direction[1] = -1*abs(self.direction[1])if((self.X,self.Y) != temp):self.speed *= 0.8
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 = 0n2 =0screen = pygame.display.set_mode((1200, 800))for x in range(10000):t = begin_a_game(n1,n2);if t == 1:n1 +=1else:n2 += 1

 

这篇关于基于python快速开发足球游戏代码记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/855160

相关文章

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意