pygame - How to make a scrolling screen in python with a simple camera class -
i attempting make game in python screen needs scroll player character. learned strange methods of doing things in python , have no knowledge of how blitting works. teacher didn't show more appears in code enclosing below. want screen move rizer class in center of screen. appreciated. thank you.
#contra 5 import pygame import math, random, time livewires import games, color games.init(screen_width = 1000, screen_height = 480, fps = 100) #add things stand on opposed standing on y value of #250, creating illusion of solid object. #make can't hold down jump button stay in air. #make lives. #make don't die when crouching , shooting. #put in camera purpose of scrolling screen. #put in way 2 players play @ once, able shoot, , have #bullets not doing damage teammates. #make title screen works. ##only add more stages , music once first stage , title screen done.## class wrapper(games.sprite): """ sprite wraps around screen. """ def update(self): """ wrap sprite around screen. """ if self.top > games.screen.height: self.bottom = self.bottom if self.bottom < 0: self.top = self.top if self.left > games.screen.width: self.right = self.right if self.right < 0: self.left = self.left def die(self): """ destroy self. """ self.destroy() class collider(wrapper): """ wrapper can collide object. """ def update(self): """ check overlapping sprites. """ super(collider, self).update() if self.overlapping_sprites: sprite in self.overlapping_sprites: sprite.die() self.die() def die(self): """ destroy self """ self.destroy() class rizer(collider): #player 1 #make own bullets cannot kill @ all. image = games.load_image("rizer.bmp") image2 = games.load_image("rizerl.bmp") image4 = games.load_image("rizerupr.bmp") image5 = games.load_image("rizerupl.bmp") image6 = games.load_image("rizercrouchr.bmp") image7 = games.load_image("rizercrouchl.bmp") image8 = games.load_image("rizerjump1.bmp") image9 = games.load_image("rizerjump2.bmp") image10 = games.load_image("rizerjump3.bmp") image11 = games.load_image("rizerjump4.bmp") direction = 0 jumpnumber = 0 crouch = 0 = 0 lives = 3 shot = 0 bullet_delay = 50 velocity_factor = 3 def __init__(self): super(rizer, self).__init__(image = rizer.image, x = 200, bottom = 250) self.bullet_wait = 0 def update(self): super(rizer, self).update() if games.keyboard.is_pressed(games.k_a): super(rizer, self).__init__(image = rizer.image2, x = self.x, bottom = self.bottom) self.x = self.x - 2 rizer.direction = 1 if games.keyboard.is_pressed(games.k_d): super(rizer, self).__init__(image = rizer.image, x = self.x, bottom = self.bottom) self.x = self.x + 2 rizer.direction = 0 if self.bullet_wait > 0: self.bullet_wait -= 1 if self.bottom == 250 , rizer.direction == 1: super(rizer, self).__init__(image = rizer.image2, x = self.x, bottom = self.bottom) if self.bottom == 250 , rizer.direction == 0: super(rizer, self).__init__(image = rizer.image, x = self.x, bottom = self.bottom) if self.bottom > 250: self.bottom = 250 if games.keyboard.is_pressed(games.k_j) , self.bullet_wait == 0: self.bullet_wait = rizer.bullet_delay new_bullet = bullet(self.x, self.y) games.screen.add(new_bullet) if games.keyboard.is_pressed(games.k_w) , rizer.direction == 0: super(rizer, self).__init__(image = rizer.image4, x = self.x, bottom = self.bottom) = 1 if games.keyboard.is_pressed(games.k_w) , rizer.direction == 1: super(rizer, self).__init__(image = rizer.image5, x = self.x, bottom = self.bottom) = 2 if games.keyboard.is_pressed(games.k_s) , rizer.direction == 0: super(rizer, self).__init__(image = rizer.image6, x = self.x, bottom = self.bottom) crouch = 2 if games.keyboard.is_pressed(games.k_s) , rizer.direction == 1: super(rizer, self).__init__(image = rizer.image7, x = self.x, bottom = self.bottom) crouch = 1 #jumping portion animation #jump timer clock = pygame.time.clock() fps, timer = 60,0 flag = false #the actual jump portion if games.keyboard.is_pressed(games.k_k): flag = true timer = 1 * fps self.y = self.y - 2 super(rizer, self).__init__(image = rizer.image8, x = self.x, bottom = self.bottom) super(rizer, self).__init__(image = rizer.image9, x = self.x, bottom = self.bottom) super(rizer, self).__init__(image = rizer.image10, x = self.x, bottom = self.bottom) super(rizer, self).__init__(image = rizer.image11, x = self.x, bottom = self.bottom) #make come down after 1 second of jumping. if timer: timer -= 1 else: flag = false self.y = self.y + 2 #make sure can't jump high if self.y < 100: self.y = 100 if self.x < 35: self.x = 35 #kill self if rizer touches object. if self.overlapping_sprites: self.destroy() #return sprite standing form after jump if self.bottom < 250: super(rizer, self).__init__(image = rizer.image11, x = self.x, bottom = self.bottom) class bean(collider): #player 2 image = games.load_image("bean.bmp") image2 = games.load_image("beanl.bmp") def __init__(self): super(bean, self).__init__(image = bean.image, x = 2000, bottom = 250) def update(self): if games.keyboard.is_pressed(games.k_left): super(bean, self).__init__(image = bean.image2, x = self.x, bottom = self.bottom) self.x = self.x - 2 if games.keyboard.is_pressed(games.k_right): super(bean, self).__init__(image = bean.image, x = self.x, bottom = self.bottom) self.x = self.x + 2 class runner(collider): image = games.load_image("runner.bmp") lives = 0 def __init__(self): super(runner, self).__init__(image = runner.image, x = 600, bottom = 250) def update(self): self.x = self.x - 1 class shooter(collider): enemy gun image = games.load_image("shooter.bmp") lives = 0 def __init__(self): super(shooter, self).__init__(image = shooter.image, x = 800, bottom = 250) if games.keyboard.is_pressed(games.k_r): new_ebullet = ebullet(shooter.x, shooter.y) games.screen.add(new_ebullet) def update(self): if self.overlapping_sprites: self.destroy() class ebullet(collider): #the bullet enemy shoots image = games.load_image("ebullet.bmp") sound = games.load_sound("shot.ogg") buffer = 100 velocity_factor = 7 lifetime = 60 lives = 0 def __init__(self, shooter_x, shooter_y,): """ initialize missile sprite. """ bullet.sound.play() x = 730 y = 270 dx = -self.velocity_factor dy = 0 super(ebullet, self).__init__(image = ebullet.image, x = x, y = y, dx = dx, dy = dy) self.lifetime = bullet.lifetime class spreadp(games.sprite): image = games.load_image("spower.bmp") shot = 0 def __init__(self): super(spreadp, self).__init__(image = spreadp.image, x = 680, bottom = 240) if self.overlapping_sprites: shot = 1 self.destroy() class rizerlives(games.sprite): image = games.load_image("rizerlife.bmp") def __init__(self): super(rizerlives, self).__init__(image = rizerlives.image, x = 10, bottom = 25) class rizerlivesa(games.sprite): image = games.load_image("rizerlife.bmp") def __init__(self): super(rizerlivesa, self).__init__(image = rizerlivesa.image, x = 25, bottom = 25) class bullet(collider): """ bullet launched player character. """ image = games.load_image("bullet.bmp") image2 = games.load_image("spreader.bmp") image3 = games.load_image("explosion1.bmp") sound = games.load_sound("shot.ogg") buffer = 100 velocity_factor = 10 lifetime = 60 lives = 0 def __init__(self, rizer_x, rizer_y,): """ initialize missile sprite. """ bullet.sound.play() # calculate missile's starting position if rizer.direction == 0 , rizer.crouch == 0 , rizer.up == 0: x = rizer_x + 60 y = rizer_y - 20 dx = self.velocity_factor dy = 0 if rizer.direction == 1 , rizer.crouch == 0 , rizer.up == 0: x = rizer_x - 60 y = rizer_y - 20 dx = -self.velocity_factor dy = 0 if rizer.crouch == 2 , rizer.up == 0: x = rizer_x + 120 y = rizer_y + 40 dx = self.velocity_factor dy = 0 if rizer.crouch == 1 , rizer.up == 0: x = rizer_x - 120 y = rizer_y + 40 dx = -self.velocity_factor dy = 0 if rizer.up == 1 or rizer.up == 2: x = rizer_x y = rizer_y - 60 dx = 0 dy = -self.velocity_factor if spreadp.shot == 0: super(bullet, self).__init__(image = bullet.image, x = x, y = y, dx = dx, dy = dy) if spreadp.shot == 1: super(bullet, self).__init__(image = bullet.image2, x = x, y = y, dx = dx, dy = dy) if spreadp.shot == 2: super(bullet, self).__init__(image = bullet.image3, x = x, y = y, dx = dx, dy = dy) self.lifetime = bullet.lifetime def update(self): """ move bullet. """ super(bullet, self).update() # if lifetime up, destroy bullet self.lifetime -= 1 if self.lifetime == 0: self.destroy() class cursor(games.sprite): cursorspot = 1 image = games.load_image("cursor.bmp",transparent = false) def __init__(self): super(cursor, self).__init__(image = cursor.image, x = 210, bottom = 345) def update(self): if games.keyboard.is_pressed(games.k_1): self.y = 335 cursorspot = 1 if games.keyboard.is_pressed(games.k_2): self.y = 365 cursorspot = 2 if games.keyboard.is_pressed(games.k_space): self.destroy() def main(): #title = games.load_image("contratitle.bmp",transparent = false) #games.screen.background = title #cursor = cursor() #games.screen.add(cursor) screen = games.load_image("jungle.bmp", transparent = false) games.screen.background = screen file2 = 'contra.mp3' pygame.mixer.music.load(file2) pygame.mixer.music.play() rizer = rizer() games.screen.add(rizer) runner = runner() games.screen.add(runner) shooter = shooter() games.screen.add(shooter) spreadpower = spreadp() #games.screen.add(spreadpower) rizerlives = rizerlives() games.screen.add(rizerlives) rizerlivesa = rizerlivesa() games.screen.add(rizerlivesa) #bean = bean() #games.screen.add(bean) games.screen.mainloop() main()
create lists contain different groups of sprites, i.e: allspritelist, allbutplayerlist...
then can have similar method in classes represent objects aren't player , call them iteratively.
for sprite in allspritesexceptplayer: sprite.move_left()
let me know if need specifics. pygame has helpful group class worth looking into.
Comments
Post a Comment