SCREEN_SIZE = (640, 480)
ANT_COUNT=5
import pygame
from pygame.locals import *
from random import randint, choice
from gameobjects.vector2 import Vector2
class World(object):
def __init__(self):
self.entities = {}
self.entity_id = 0
'''self.background = pygame.surface.Surface(SCREEN_SIZE).convert() '''
def add_entity(self, entity):
self.entities[self.entity_id] = entity
entity.id = self.entity_id
self.entity_id += 1
def remove_entity(self, entity):
del self.entities[entity.id]
def get(self, entity_id):
if entity_id in self.entities:
return self.entities[entity_id]
else:
return None
def process(self, time_passed):
time_passed_seconds = time_passed / 1000.0
for entity in self.entities.values():
entity.process(time_passed_seconds)
def render(self, surface):
'''surface.blit(self.background, (0, 0))'''
background_image_filename='grass.jpg'
background=pygame.image.load(background_image_filename).convert()
surface.blit(background,(0,0))
for entity in self.entities.itervalues():
entity.render(surface)
def get_close_entity(self, name, location, range=100.):
location = Vector2(*location)
for entity in self.entities.itervalues():
if entity.name == name:
distance = location.get_distance_to(entity.location)
if distance < range:
return entity
return None
class State(object):
def __init__(s
用pygame写游戏 蚂蚁打蜘蛛侠 并升级
最新推荐文章于 2024-08-09 20:17:12 发布
这是一个使用pygame库创建的简单游戏,游戏中有蚂蚁在探索世界,当遇到蜘蛛侠时会进行攻击。蚂蚁和蜘蛛侠都有自己的状态机,根据状态执行不同的行为,如探索、追逐和战斗。游戏包含蚂蚁和蜘蛛侠的生成、移动以及状态切换等逻辑。
摘要由CSDN通过智能技术生成