def game_init(): global screen, font, timer global paddle_group, block_group, ball_group global paddle, block_image, block, ball pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Block Breaker Game") font = pygame.font.Font(None, 36) pygame.mouse.set_visible(False) timer = pygame.time.Clock() paddle_group = pygame.sprite.Group() block_group = pygame.sprite.Group() ball_group = pygame.sprite.Group() paddle = MySprite() paddle.load("paddle.png") paddle.position = 400, 540 paddle_group.add(paddle) ball = MySprite() ball.load("ball.png") ball.position = 400, 300 ball_group.add(ball)
def __init__(self, tank_file="tank.png", turret_file="turret.png"): MySprite.__init__(self) self.load(tank_file, 50, 60, 4) self.speed = 0.0 self.scratch = None self.float_pos = Point(0, 0) self.velocity = Point(0, 0) self.turret = MySprite() self.turret.load(turret_file, 32, 64, 4) self.fire_timer = 0
class Tank(MySprite): """docstring for ClassName""" def __init__(self, tank_file="tank.png", turret_file="turret.png"): MySprite.__init__(self) self.load(tank_file, 50, 60, 4) self.speed = 0.0 self.scratch = None self.float_pos = Point(0, 0) self.velocity = Point(0, 0) self.turret = MySprite() self.turret.load(turret_file, 32, 64, 4) self.fire_timer = 0 def update(self, ticks): MySprite.update(self, ticks, 100) self.rotation = wrap_angle(self.rotation) self.scratch = pygame.transform.rotate(self.image, -self.rotation) angle = wrap_angle(self.rotation - 90) self.velocity = angular_velocity(angle) self.float_pos.x += self.velocity.x self.float_pos.y += self.velocity.y if self.float_pos.x < -50: self.float_pos.x = 800 elif self.float_pos.x > 800: self.float_pos.x = -50 if self.float_pos.y < -60: self.float_pos.y = 600 elif self.float_pos.y > 600: self.float_pos.y = -60 self.X = int(self.float_pos.x) self.Y = int(self.float_pos.y) self.turret.position = (self.X, self.Y) self.turret.last_frame = 0 self.turret.update(ticks, 100) self.turret.rotation = wrap_angle(self.turret.rotation) angle = self.turret.rotation + 90 self.turret.scratch = pygame.transform.rotate(self.turret.image, -angle) def draw(self, surface): width, height = self.scratch.get_size() center = Point(width / 2, height / 2) surface.blit(self.scratch, (self.X - center.x, self.Y - center.y)) width, height = self.turret.scratch.get_size() center = Point(width / 2, height / 2) surface.blit(self.turret.scratch, (self.turret.X - center.x, self.turret.Y - center.y)) def __str__(self): return MySprite.__str__(self) + "," + str(self.velocity)
def __init__(self): self.currenLuigi = 1 self.done = False self.isAlive=True self.window_size = 800,600 self.window = pygame.display.set_mode(self.window_size) self.clock = pygame.time.Clock() self.fps = 7 pygame.display.set_caption('Epic Game!') MyFont = pygame.font.Font(None, 70) MyFont2 = pygame.font.Font(None, 50) self.isDeadText = MyFont.render(':( You Have Died!', True, red) self.textReload = MyFont2.render('Press R To Retry, Or Q/ESC To Quit!', True, (200, 0, 200)) self.foxGroup = pygame.sprite.Group() fox = MySprite() fox.setImage('images/fox.png') fox.setPos(100, 100) self.foxGroup.add(fox) self.Luigi_group = pygame.sprite.Group() self.Luigi = MySprite() self.Luigi.setImage('images/Luigi1.png') self.luigi_start = (200, 200) self.Luigi.setPos(*self.luigi_start) # *luigi_start == luigi_start[0], luigi_start[1] self.Luigi_group.add(self.Luigi) self.currentLuigi = 1 self.sound = pygame.mixer.Sound('sounds/MarioClear.wav')
def game_init(): global screen, backbuffer, font, timer, player_group, player, enemy_tank, bullets, crosshair, crosshair_group pygame.init() screen = pygame.display.set_mode((800, 600)) backbuffer = pygame.Surface((800, 600)) font = pygame.font.Font(None, 30) timer = pygame.time.Clock() pygame.mouse.set_visible(False) crosshair = MySprite() crosshair.load("crosshair.png") crosshair_group = pygame.sprite.GroupSingle() crosshair_group.add(crosshair) player = Tank() player.float_pos = Point(400, 300) enemy_tank = EnemyTank() enemy_tank.float_pos = Point(random.randint(50, 760), 50) enemy_tank.rotation = 135 bullets = list()
def game_init(): global screen, backbuffer, font, timer, oil_group, cursor, cursor_group pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Oil Spill Game") font = pygame.font.Font(None, 36) pygame.mouse.set_visible(False) timer = pygame.time.Clock() backbuffer = pygame.Surface((800, 600)) backbuffer.fill(darktan) oil_group = pygame.sprite.Group() cursor = MySprite() cursor.radius = 60 image = pygame.Surface((60, 60)).convert_alpha() image.fill((255, 255, 0)) pygame.draw.circle(image, (80, 80, 220, 70), (30, 30), 30, 0) pygame.draw.circle(image, (80, 80, 250, 255), (30, 30), 30, 4) cursor.set_image(image) cursor_group = pygame.sprite.Group() cursor_group.add(cursor)
def load_level(): global level, block_image, block_group, levels block_image = pygame.image.load("blocks.png").convert_alpha() block_group.empty() for bx in range(0, 12): for by in range(0, 10): block = MySprite() block.set_image(block_image, 58, 28, 4) x = 40 + bx * (block.frame_width + 1) y = 60 + by * (block.frame_height + 1) block.position = x, y num = levels[level][by * 12 + bx] block.first_frame = num - 1 block.last_frame = num - 1 if num > 0: block_group.add(block)
def RunMyGame(): #sound.play() x=15 y=15 moveX=0 moveY=0 currentLuigi=1 #Luigi = MyPlayerObject() fox = MySprite() fox.setImage('images/fox.png') fox.setPos(100,100) Luigi = MySprite() Luigi_group.add(Luigi) Luigi.setImage('images/Luigi1.png') Luigi.setPos(10,10) Luigi_group.add(Luigi) #Luigi1 = pygame.image.load('images/Luigi1.png').convert_alpha() #Luigi2 = pygame.image.load('images/Luigi2.png').convert_alpha() foxX,foxY = 100,100 fox.setPos(foxX,foxY) foxGroup = pygame.sprite.Group() foxGroup.add(fox) running = True pygame.display.set_caption('Epic Game!') #pygame.display.set_icon(surface) window.fill(blue) running=True while running: while isAlive: window.fill(blue) for event in pygame.event.get(): if event.type==MOUSEBUTTONDOWN: print('RightMouseButton Pressed!') if event.type == MOUSEMOTION: mousePos = pygame.mouse.get_pos() print('MousePos = '+str(mousePos[0])+','+str(mousePos[1])) if event.type==QUIT: pygame.quit() sys.exit() if event.type==KEYDOWN: if event.key == K_ESCAPE: print('Escape Key Pressed!') pygame.quit() sys.exit() if event.key==K_LEFT: print('Left Arrow Key Pressed!') moveX=-5 #x+=moveX if event.key==K_RIGHT: print('Right Arrow Key Pressed!') moveX=5 #x+=moveX if event.key==K_UP: print('Up Arrow Key Pressed!') moveY=-5 #y+=moveY if event.key==K_DOWN: print('Down Arrow Key Pressed!') moveY=5 #y+=moveY if event.type==KEYUP: moveX=0 moveY=0 currentLuigi=1 window.fill(blue) if currentLuigi==1: Luigi.setImage('images/Luigi1.png') #window.blit(Luigi1,(x,y))#Luigi1 if currentLuigi == 2: Luigi.setImage('images/Luigi2.png') #window.blit(Luigi2,(x,y))#Luigi2 if currentLuigi==2: currentLuigi=1 else: currentLuigi+=1 x += moveX y += moveY window.blit(Luigi.image,(x,y)) foxGroup.draw(window) Luigi.setPos(x, y) if pygame.sprite.collide_rect(Luigi,fox): deathEvent() Luigi_group.remove(Luigi) if x < 0 or y < 0 or x >790 or y > 570 or y < 0: deathEvent() Luigi_group.remove(Luigi) pygame.display.update() clock.tick(fps) pygame.display.update() clock.tick(fps) whatNext() RunMyGame()
import pygame from pygame.locals import * from MySprite import * import sys pygame.init() screen = pygame.display.set_mode((800, 600), 0, 32) pygame.display.set_caption("Sprite Animation Demo") font = pygame.font.Font(None, 18) framerate = pygame.time.Clock() dragon = MySprite(screen) dragon.load("dragon.png", 260, 150, 3) group = pygame.sprite.Group() group.add(dragon) while True: framerate.tick(20) ticks = pygame.time.get_ticks() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: pygame.quit() sys.exit() screen.fill((0, 0, 100)) group.update(ticks) group.draw(screen) print_text(font, 0, 0, "Sprite: " + str(dragon))
sprite.direction = 0 elif sprite.direction == 6: sprite.direction = 2 pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Collision Demo") font = pygame.font.Font(None, 36) time = pygame.time.Clock() player_group = pygame.sprite.Group() zombie_group = pygame.sprite.Group() health_group = pygame.sprite.Group() player = MySprite() player.load("farmer walk.png", 96, 96, 8) player.position = 80, 80 player.direction = 4 player_group.add(player) zombie_image = pygame.image.load("zombie walk.png").convert_alpha() for n in range(0, 10): zombie = MySprite() zombie.load("zombie walk.png", 96, 96, 8) zombie.position = random.randint(0, 700), random.randint(0, 500) zombie.direction = random.randint(0, 3) * 2 zombie_group.add(zombie) health = MySprite() health.load("health.png", 32, 32, 1)
class GameSkeleton: moveX = 0 moveY =0 def __init__(self): self.currenLuigi = 1 self.done = False self.isAlive=True self.window_size = 800,600 self.window = pygame.display.set_mode(self.window_size) self.clock = pygame.time.Clock() self.fps = 7 pygame.display.set_caption('Epic Game!') MyFont = pygame.font.Font(None, 70) MyFont2 = pygame.font.Font(None, 50) self.isDeadText = MyFont.render(':( You Have Died!', True, red) self.textReload = MyFont2.render('Press R To Retry, Or Q/ESC To Quit!', True, (200, 0, 200)) self.foxGroup = pygame.sprite.Group() fox = MySprite() fox.setImage('images/fox.png') fox.setPos(100, 100) self.foxGroup.add(fox) self.Luigi_group = pygame.sprite.Group() self.Luigi = MySprite() self.Luigi.setImage('images/Luigi1.png') self.luigi_start = (200, 200) self.Luigi.setPos(*self.luigi_start) # *luigi_start == luigi_start[0], luigi_start[1] self.Luigi_group.add(self.Luigi) self.currentLuigi = 1 self.sound = pygame.mixer.Sound('sounds/MarioClear.wav') def event_loop(self): for event in pygame.event.get(): if event.type==QUIT: self.done = True elif event.type==MOUSEBUTTONDOWN: print('SomeMouseButton Pressed!') elif event.type == MOUSEMOTION: mousePos = event.pos #pygame.mouse.get_pos() print('Mouse: '+str(mousePos[0])+','+str(mousePos[1])) elif event.type == KEYDOWN: if event.key in (K_q, K_ESCAPE): self.done = True elif event.key == K_LEFT: print('Left Arrow Key Pressed!') self.moveX = -5 elif event.key == K_RIGHT: print('Right Arrow Key Pressed!') self.moveX = 5 elif event.key == K_UP: print('Up Arrow Key Pressed!') self.moveY = -5 print(self.Luigi.rect.y) elif event.key == K_DOWN: print('Down Arrow Key Pressed!') self.moveY = 5 elif event.key== K_r: print('R Key Pressed!') self.isAlive=True elif event.type == KEYUP: self.currentLuigi = 1 def update(self): x = self.Luigi.rect.centerx y = self.Luigi.rect.centery if self.moveX or self.moveY: self.Luigi.setPos(x + self.moveX, y + self.moveY) if self.currentLuigi == 1: self.Luigi.setImage('images/Luigi1.png') self.currentLuigi = 2 else: self.Luigi.setImage('images/Luigi2.png') self.currentLuigi = 1 death = False if x < 0 or x > 790 or y > 570 or y < 0 or self.Luigi.rect.y < 0: death = True for fox in self.foxGroup: if pygame.sprite.collide_rect(self.Luigi, fox): death = True break if death: self.isAlive = False self.Luigi.setPos(*self.luigi_start) def show_death(self): self.window.fill(blue) self.window.blit(self.isDeadText, (125, 225)) self.window.blit(self.textReload, (70, 300)) def draw(self): if not self.isAlive: self.show_death() else: self.window.fill(blue) self.window.blit(self.Luigi.image, self.Luigi.rect) self.foxGroup.draw(self.window) def run(self): self.sound.play() while not self.done: self.event_loop() self.update() self.draw() pygame.display.update() self.clock.tick(self.fps)