资源简介
使用python pygame实现的简单的网络游戏客户端和服务器端
代码片段和文件信息
# coding=utf8
‘‘‘
Game client
‘‘‘
import threading
import time
from random import randint as rint
import pygame
import queue
from pygame.locals import *
from twisted.internet import reactor
from twisted.internet.protocol import Protocol ClientFactory
ctrl_q = queue.Queue()
pos_q = queue.LifoQueue()
enemy_q = queue.Queue()
‘‘‘
The game
‘‘‘
class MySprite(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.master_image = None
self.frame = 0
self.old_frame = -1
self.frame_width = 1
self.frame_height = 1
self.first_frame = 0
self.last_frame = 0
self.columns = 1
self.last_time = 0
self.direction = 0
self.velocity = Point(0.0 0.0)
# X property
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 width height columns):
self.master_image = pygame.image.load(filename).convert_alpha()
self.frame_width = width
self.frame_height = height
self.rect = Rect(0 0 width height)
self.columns = columns
# try to auto-calculate total frames
rect = self.master_image.get_rect()
self.last_frame = (rect.width // width) * (rect.height // height) - 1
def update(self current_time rate=30):
# update animation frame number
if current_time > self.last_time + rate:
self.frame += 1
if self.frame > self.last_frame:
self.frame = self.first_frame
self.last_time = current_time
# build current frame only if it changed
if self.frame != self.old_frame:
frame_x = (self.frame % self.columns) * self.frame_width
frame_y = (self.frame // self.columns) * self.frame_height
rect = Rect(frame_x frame_y self.frame_width self.frame_height)
self.image = self.master_image.subsurface(rect)
self.old_frame = self.frame
def __str__(self):
return str(self.frame) + ““ + str(self.first_frame) + \
““ + str(self.last_frame) + ““ + str(self.frame_width) + \
““ + str(self.frame_height) + ““ + str(self.columns) + \
““ + str(self.rect)
# Point class
class Point(object):
def __init__(self x y):
self.__x = x
self.__y = y
# X property
def getx(self): return self.__x
def setx(self x): self.__x
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 23650 2018-01-05 18:08 ChopperGunClient.py
文件 22325 2018-01-05 18:12 ChopperGunServer.py
文件 37 2017-12-21 17:20 Game_Client_Start.bat
文件 37 2017-12-21 17:20 Game_Server_Start.bat
目录 0 2017-12-20 17:19 resources\
目录 0 2017-12-20 18:30 resources\images\
文件 209 2017-12-20 18:27 resources\images\bullet2.png
文件 17179 2017-12-20 18:28 resources\images\enemychopper.png
文件 38767 2017-12-20 18:28 resources\images\flame.png
文件 1864 2017-12-20 18:29 resources\images\missile.png
文件 17850 2017-12-21 11:13 resources\images\pla
文件 650315 2017-12-13 19:44 resources\images\skybg.jpg
文件 814860 2017-12-20 18:30 resources\images\skybg.png
文件 21351 2017-12-20 18:29 resources\images\warship.png
文件 96780 2017-12-20 18:29 resources\images\warship_flame.png
文件 3529 2017-12-20 18:29 resources\images\warship_missile.png
- 上一篇:数据挖掘—数据.rar
- 下一篇:MLP/RNN/LSTM模型进行IMDb情感分析
相关资源
- 笨办法学Python(第四版)
- Python Cookbook(第3版)高清中文完整版
- Twisted-19.10.0-cp38-cp38-win_amd64.whl
- Python在经济计量统计和数据分析上的
- 卷积神经网络的Python实现【试读】1
- 10行Python代码实现目标检测
- Python基础教程(第3版).rar
- deep_learning_with_python.pdf(Jason Brownlee)
- Flask Web开发:基于Python的Web应用开发
- Python测试驱动开发 使用Django、Seleni
- Python实验指导书2018版
- [Python程序设计基础][李东方][电子教案
- IronPython In Action
- Apache 2.4.37_Win32
- Python数据挖掘入门与实践-中文高清晰
- 利用 Python 进行数据分析 原书第二版
- IronPython-2.7 for VS2010
- numpy-1.8.1-win32-superpack-python2.7
- python machine learning(2nd
- 利用Python进行数据分析中文第二版-
- python有限元框架FENICS教程及
- 从python开始学编程 完整高清版
- python基础教程 第三版+源码高清
- python微信
- opencv3+python人脸检测和识别- 完整实战
- Python3网络爬虫数据采集
- Python雷电飞机大战小游戏
- Deep Learning with Python by Francois Chollet (
- 利用Python进行数据分析第二版(英文
- 人脸识别python代码187268
评论
共有 条评论