def fire(self): bullet = Actor('bullet', pos=self.pos) ang = math.radians(self.angle) bullet.exact_pos = bullet.start_pos = Vector2(self.pos) bullet.velocity = Vector2(math.sin(ang), math.cos(ang)).normalize() * 1000.0 return bullet
def __init__(self, centreX, centreY, creationTime): self.centre = [centreX, centreY] self.timeFromCreation = creationTime self.rainbowActor = Actor('rainbow', (centreX, centreY - rainbowHalfSize/2)) points = [] for i in range(0, 180+20, 20): points.append((self.centre[0] + rainbowHalfSize*math.cos(math.pi*i/180)*0.75, self.centre[1] - rainbowHalfSize*math.sin(math.pi*i/180))) self.lineString = LineString(points)
def draw_players_hands(): for p, player in enumerate(game.game.players): color = 'red' if player == game.game.current_player else 'black' text = 'P{} {}'.format(p, 'wins' if game.game.winner == player else '') screen.draw.text(text, (0, 300+p*130), fontsize=100, color=color) for c, card in enumerate(player.hand): if player == game.player: sprite = card.sprite else: sprite = Actor('back') sprite.pos = (130+c*80, 330+p*130) sprite.draw()
def set_up_imperial(): imperial = [[0] * 2 for i in range(3)] imperial[0][0] = Actor('stormtrooper') imperial[0][1] = stormtrooper1 imperial[1][0] = Actor('stormtrooper') imperial[1][1] = stormtrooper2 imperial[2][0] = Actor('vader') imperial[2][1] = vader return imperial
def set_up_rebel(): rebels = [[0] * 2 for i in range(3)] rebels[0][0] = Actor('rebels') rebels[0][1] = rebel_trooper1 rebels[1][0] = Actor('rebels') rebels[1][1] = rebel_trooper2 rebels[2][0] = Actor('luke') rebels[2][1] = luke_skywalker return rebels
class Projektil: _speed = 0 def __init__(self, imagepath, pos, angle, speed): self._actor = Actor(imagepath) self._actor.pos = pos self._actor.angle = angle self._speed = speed def draw(self): self._actor.draw() def update(self): self._actor.y += sin((self._actor.angle - 90) / 180 * pi) * self._speed self._actor.x -= cos((self._actor.angle - 90) / 180 * pi) * self._speed
class Collectable: def __init__(self, startX, startY): self.centre = [startX, startY] self.creationTime = 0 self.actor = Actor(collectableNames[randint(0, 5)], (startX, startY)) def draw(self, screenPosition): self.actor.y = self.centre[1] - screenPosition self.actor.draw() def update(self): self.creationTime += 1 def isRainbow(self): if self.actor.image == 'collectable_rainbow': return True return False
def __init__(self, startX, startY): self.centre = [startX, startY] self.speedX = randint(-startX, 800 - startX) / 100 self.speedY = randint(-6, -4) self.accelerationDown = 0.1 self.actors = [ Actor(name, (startX, startY)) for name in flyingCollectableNames ] self.indexActor = 0
class FallingRainbow: def __init__(self, centreX, centreY): self.centre = [centreX, centreY] self.rainbowActor = Actor('falling_rainbow', (centreX, centreY - rainbowHalfSize/2)) self.speedY = 0 def draw(self, screenPosition): self.rainbowActor.y = self.centre[1] - rainbowHalfSize/2 - screenPosition self.rainbowActor.draw() def update(self, allEnemies, allRainbows, allCollectables): self.speedY += rainbowAccelerationDown self.centre[1] += self.speedY centerCollision = [self.centre[0], self.centre[1] + self.speedY * 2] halfSizeCollision = [rainbowHalfSize, (rainbowHalfSize + self.speedY) / 2] for i in reversed(range(len(allEnemies.enemies))): if RectanglesIntersect(centerCollision, halfSizeCollision, allEnemies.enemies[i].centre, [allEnemies.enemyHalfSize(), allEnemies.enemyHalfSize()]): allEnemies.killEnemy(i, allCollectables) for i in reversed(range(len(allRainbows.rainbows))): if RectanglesIntersect(centerCollision, halfSizeCollision, allRainbows.rainbows[i].centre, [rainbowHalfSize, rainbowHalfSize/2]): allRainbows.rainbowFall(i)
class Rainbow: def __init__(self, centreX, centreY, creationTime): self.centre = [centreX, centreY] self.timeFromCreation = creationTime self.rainbowActor = Actor('rainbow', (centreX, centreY - rainbowHalfSize/2)) points = [] for i in range(0, 180+20, 20): points.append((self.centre[0] + rainbowHalfSize*math.cos(math.pi*i/180)*0.75, self.centre[1] - rainbowHalfSize*math.sin(math.pi*i/180))) self.lineString = LineString(points) def draw(self, screenPosition): if self.timeFromCreation >= 0: self.rainbowActor.y = self.centre[1] - rainbowHalfSize/2 - screenPosition self.rainbowActor.draw() def update(self, allEnemies, allCollectables): if self.timeFromCreation == 0: for i in reversed(range(len(allEnemies.enemies))): if self.lineString.intersects(allEnemies.enemies[i].lineString): allEnemies.killEnemy(i, allCollectables) self.timeFromCreation += 1
def __init__(self): self.platformActors = [ Actor(platformNames[platforms[i][0]], (platforms[i][1], platforms[i][2])) for i in range(len(platforms)) ] self.platformLineStrings = [] for i in range(len(platforms)): points = [(platformLines[platforms[i][0]][j][0] + platforms[i][1], platformLines[platforms[i][0]][j][1] + platforms[i][2]) for j in range(len(platformLines[platforms[i][0]]))] self.platformLineStrings.append(LineString(points))
def __init__(self, centreX, centreY, indexEnemy, directionX): self.centre = [centreX, centreY] self.centredLineString = LineString([(-enemyHalfSize, -enemyHalfSize), (-enemyHalfSize, enemyHalfSize), (enemyHalfSize, enemyHalfSize), (enemyHalfSize, -enemyHalfSize)]) self.lineString = translate(self.centredLineString, self.centre[0], self.centre[1]) self.speedX = directionX * enemyLateralSpeed self.speedY = 0 if indexEnemy == 2: self.speedY = enemyFlyingVerticalSpeed self.index = indexEnemy self.actors = [Actor(name) for name in enemyNames[indexEnemy]] self.active = False
def __init__(self): self.centre = [400, 500] self.centredLineString = LineString([ (-playerHalfSizeX, -playerHalfSizeY * 0), (-playerHalfSizeX, playerHalfSizeY), (playerHalfSizeX, playerHalfSizeY), (playerHalfSizeX, -playerHalfSizeY * 0) ]) self.lineString = translate(self.centredLineString, self.centre[0], self.centre[1]) self.polygon = Polygon(self.lineString) self.speedY = 0 self.walking = False self.directionX = -1 self.jumping = False self.actors = [Actor(name) for name in playerImageNames] self.numberOfRainbows = 1 self.lastRainbowShot = playerMinTimeBetweenRainbows self.active = True self.lives = 3
import pgzrun from pgzero.builtins import Actor, keyboard import random import time import handle_scores import os # Change these to change the physical size of the window. I make no guarantees that this will not break the game in # certain resolutions. WIDTH = 600 HEIGHT = 500 char_image = Actor("penguin_resized_larger") seed = None seeded = False while not seeded: seed = input("Input an integer seed to generate a maze or input '1' for a random seed: ") if seed.isnumeric(): if seed == "1": seed = random.randint(0, 999999999) seeded = True else: seed = int(seed) seeded = True else: print("That is not a valid seed. Try again.") sized = False while not sized: num_col = input("Input a integer number of columns for the maze: ") if num_col.isnumeric():
def __init__(self, startX, startY): self.centre = [startX, startY] self.creationTime = 0 self.actor = Actor(collectableNames[randint(0, 5)], (startX, startY))
def __init__(self, color, card_type): self._validate(color, card_type) self.color = color self.card_type = card_type self.temp_color = None self.sprite = Actor('{}_{}'.format(color, card_type))
else: game_data.log = "Player {} picked up".format(player) game.play(player=player_id, card=None) def print_hand(self): print('Your hand: {}'.format( ' '.join(str(card) for card in self.player.hand) )) num_players = 2 game = AIUnoGame(num_players) WIDTH = 1100 HEIGHT = 650 deck_img = Actor('back') color_imgs = {color: Actor(color) for color in COLORS} def game_loop(): while game.game.is_active: sleep(1) next(game) game_loop_thread = Thread(target=game_loop) game_loop_thread.start() def draw_deck(): deck_img.pos = (130, 70) deck_img.draw() current_card = game.game.current_card current_card.sprite.pos = (210, 70)
def __init__(self, imagepath, pos, angle, speed): self._actor = Actor(imagepath) self._actor.pos = pos self._actor.angle = angle self._speed = speed
def __init__(self, centreX, centreY): self.centre = [centreX, centreY] self.rainbowActor = Actor('falling_rainbow', (centreX, centreY - rainbowHalfSize/2)) self.speedY = 0
def game_end(): time.sleep(1) t2 = time.time() t3 = t2 - t1 if game.end is True: print('\n\nLevel 1 Complete!') else: pass print( f'\n\n\n\nFinal Stats:\n\nScore: {game.score}\nTies Hit: {game.hitsHit}\nDeaths: {game.deaths}\nTies Let Through: {game.tiesLet}\nGame Total Time: {t3}' ) sys.exit() # actors explosion = Actor('explosion', (-WIDTH, -HEIGHT)) explosion.inGame = False explosion.times = 0 # tie fighters tie = Actor('tiefighter', (tiestart, 0)) tie.y = tie.height / 2 tie2 = Actor('tiefighter', (tiestart, 0)) tie2.y = tie2.height / 2 tie3 = Actor('tiefighter', (tiestart, 0)) tie3.y = tie3.height / 2 # standard laser, universal laser = Actor('laser', (-WIDTH, -HEIGHT)) laser.active = False laser2 = Actor('laser', (-WIDTH, -HEIGHT)) laser2.active = False laser3 = Actor('laser', (-WIDTH, -HEIGHT))
def white_board(board, board_x, board_y): for x in range(board_x): for y in range(board_y): # print("Assigning: Board X:" + str(x) + " Y: " + str(y)) board[x][y] = Actor('blank_token') board[x][y].pos = (x * 68) + 54, (y * 68) + 54
initials = '' level = 1 score = 0 lives = 3 player = None leader_board = {} game = GameState() game.player = Player(pos=(WIDTH / 2, HEIGHT / 2)) stars = create_star_scape(WIDTH, HEIGHT) max_distance = min(WIDTH, HEIGHT) * .95 life_pos = 10 life_icons = [] for _ in range(3): icon = Actor('player', topleft=(life_pos, 10)) life_pos += 32 life_icons.append(icon) def create_asteroids(): game.asteroids = [] for i in range(2 + game.level): game.asteroids.append(Asteroid((WIDTH, HEIGHT))) def make_vulnerable(): game.player.invulnerable = False clock.unschedule(blink) game.player.show = True def make_invulnerable(): game.player.invulnerable = True
import pgzero from pgzero.builtins import Actor, animate, keyboard import time import random playerimage = "diamond_s" ballimage = "dodge-ball-brown" bgimage = "background1" player = Actor(playerimage) ball = Actor(ballimage) bg = Actor("background1") WIDTH = 490 HEIGHT = 450 fail = False ball.center = WIDTH / 2, HEIGHT / 2 def draw(): global fail localtime = time.localtime(time.time()) hour = localtime[3] if hour > 5 and hour < 12: screen.fill("lightblue") if hour > 17 and hour < 24: screen.fill("darkblue") if fail: screen.fill("red")