def __init__(self, w, h): GWTCanvas.__init__(self, w, h) self.width = w self.height = h self.model = Model(w, h) self.controller = Controller(self.model) images = [ './images/ship1.png', './images/ship2.png', './images/asteroid.png' ] loadImages(images, self) self.sinkEvents(Event.KEYEVENTS)
def pygame_testai(ais): """ 输入ai列表, 跑一个pygame的测试游戏看看 """ from snake_game import Game from game_controller import Controller game = Game() c = Controller(game) m = c.map() for ai in ais: ai.setmap(m) result = c.add(ai.name, ai.type) ai.seq = result['seq'] ai.id = result['id'] clock = Clock(3) s = Shower(m) while True: clock.tick() info = c.info() for ai in ais: d = ai.onprocess(info) c.turn(ai.id, d, game.round) game.step() s.flip(info)
def __init__(self, w, h): GWTCanvas.__init__(self, w, h) self.width = w self.height = h self.model = Model(w, h) self.controller = Controller(self.model) images = ['./images/ship1.png', './images/ship2.png', './images/asteroid.png'] loadImages(images, self) self.sinkEvents(Event.KEYEVENTS)
class GameCanvas(GWTCanvas): def __init__(self, w, h): GWTCanvas.__init__(self, w, h) self.width = w self.height = h self.model = Model(w, h) self.controller = Controller(self.model) images = ['./images/ship1.png', './images/ship2.png', './images/asteroid.png'] loadImages(images, self) self.sinkEvents(Event.KEYEVENTS) def onImagesLoaded(self, imagesHandles): self.ship = imagesHandles[0] self.ship_thrust = imagesHandles[1] self.asteroid = imagesHandles[2] self.resize(self.width, self.height) self.controller.start_game(self) def addTo(self, panel): panel.add(self) self.top = DOM.getAbsoluteTop(self.getElement()) self.left = DOM.getAbsoluteLeft(self.getElement()) def setKey(self, k, set): DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) if k == 38: self.controller.key_up = set elif k == 40: self.controller.key_down = set elif k == 37: self.controller.key_left = set elif k == 39: self.controller.key_right = set elif k == 32: self.controller.key_fire = set def onKeyDown(self, sender, keyCode, modifiers = None): self.setKey(keyCode, True) def onKeyUp(self, sender, keyCode, modifiers = None): self.setKey(keyCode, False) def draw_asteroid(self, asteroid): self.saveContext() self.translate(asteroid.x, asteroid.y) self.rotate(asteroid.rot) self.scale(asteroid.scale,asteroid.scale) self.drawImage(self.asteroid, -(ASTEROID_SIZE/2), -(ASTEROID_SIZE/2)) self.restoreContext() def draw_shot(self, shot): self.setFillStyle(SHOT_COLOR) self.fillRect(int(shot.x - 1), int(shot.y - 1), 3, 3) def draw_ship(self, ship): self.saveContext() self.translate(ship.x, ship.y) self.rotate(ship.rot) if self.controller.key_up: img = self.ship_thrust else: img = self.ship self.drawImage(img, -15, -12) self.restoreContext() def draw(self): self.setFillStyle(Color.Color('#000')) self.fillRect(0,0,self.width,self.height) for a in self.model.asteroids: self.draw_asteroid(a) for s in self.model.shots: self.draw_shot(s) self.draw_ship(self.model.ship)
class GameCanvas(GWTCanvas): def __init__(self, w, h): GWTCanvas.__init__(self, w, h) self.width = w self.height = h self.model = Model(w, h) self.controller = Controller(self.model) images = [ './images/ship1.png', './images/ship2.png', './images/asteroid.png' ] loadImages(images, self) self.sinkEvents(Event.KEYEVENTS) def onImagesLoaded(self, imagesHandles): self.ship = imagesHandles[0] self.ship_thrust = imagesHandles[1] self.asteroid = imagesHandles[2] self.resize(self.width, self.height) self.controller.start_game(self) def addTo(self, panel): panel.add(self) self.top = DOM.getAbsoluteTop(self.getElement()) self.left = DOM.getAbsoluteLeft(self.getElement()) def setKey(self, k, set): DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) if k == 38: self.controller.key_up = set elif k == 40: self.controller.key_down = set elif k == 37: self.controller.key_left = set elif k == 39: self.controller.key_right = set elif k == 32: self.controller.key_fire = set def onKeyDown(self, sender, keyCode, modifiers=None): self.setKey(keyCode, True) def onKeyUp(self, sender, keyCode, modifiers=None): self.setKey(keyCode, False) def draw_asteroid(self, asteroid): self.saveContext() self.translate(asteroid.x, asteroid.y) self.rotate(asteroid.rot) self.scale(asteroid.scale, asteroid.scale) self.drawImage(self.asteroid, -(ASTEROID_SIZE / 2), -(ASTEROID_SIZE / 2)) self.restoreContext() def draw_shot(self, shot): self.setFillStyle(SHOT_COLOR) self.fillRect(int(shot.x - 1), int(shot.y - 1), 3, 3) def draw_ship(self, ship): self.saveContext() self.translate(ship.x, ship.y) self.rotate(ship.rot) if self.controller.key_up: img = self.ship_thrust else: img = self.ship self.drawImage(img, -15, -12) self.restoreContext() def draw(self): self.setFillStyle(Color.Color('#000')) self.fillRect(0, 0, self.width, self.height) for a in self.model.asteroids: self.draw_asteroid(a) for s in self.model.shots: self.draw_shot(s) self.draw_ship(self.model.ship)
from games.connect4_models import Connect4Game from game_controller import Controller from views.sockets.server import TextServer from games.tic_tac_toe_models import TicTacToeGame from views.views import TextView from AI.strategies import MinMax if __name__ == '__main__': # controller_factory = ControllerFactory() view1 = TextView() view2 = TextServer() player1_strategy = MinMax() player2_strategy = view2 game = TicTacToeGame(player1_strategy, player2_strategy) controller = Controller(game, view1, view2) controller.play()
import pygame import random import math import sys from pygame.locals import * import time from game_controller import Controller import event import os x = 50 y = 50 os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x, y) controllerObj = Controller() game_clock = pygame.time.Clock() delta_time = 1 pygame.init() screen = pygame.display.set_mode((config.game_width, config.game_height)) pygame.key.set_repeat(100, 200) game_run = True font = pygame.font.SysFont(None, 36) def msTime():