我正在尝试在python中制作游戏,其中屏幕需要使用玩家角色滚动。我在python中学到了一些奇怪的处理方法,并且不知道blitting是如何工作的。我的老师没有向我们展示比我在下面附带的代码中出现的内容更多的东西。我希望屏幕移动,以便Rizer类始终位于屏幕的中心。任何帮助,将不胜感激。谢谢。
#Contra 5
import pygame
import math, random, time
from livewires import games, color
games.init(screen_width = 1000, screen_height = 480, fps = 100)
#Add things to stand on as opposed to only standing on the y value of
#250, thus creating the illusion of a solid object.
#Make it so you can't hold down the jump button to stay in the air.
#Make lives.
#Make it so you don't die when crouching and shooting.
#Put in a Camera for the purpose of a scrolling screen.
#Put in a way for 2 players to play at once, be able to shoot, and have their
#bullets not doing damage to teammates.
#Make it so the title screen works.
##ONLY ADD MORE STAGES AND MUSIC ONCE THE FIRST STAGE AND TITLE SCREEN IS DONE.##
class Wrapper(games.Sprite):
""" A sprite that wraps around the 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):
""" A Wrapper that can collide with another object. """
def update(self):
""" Check for overlapping sprites. """
super(Collider, self).update()
if self.overlapping_sprites:
for sprite in self.overlapping_sprites:
sprite.die()
self.die()
def die(self):
""" Destroy self """
self.destroy()
class Rizer(Collider):
#Player One
#make it so your own bullets cannot kill you at 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
up = 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 and Rizer.direction == 1:
super(Rizer, self).__init__(image = Rizer.image2,
x = self.x,
bottom = self.bottom)
if self.bottom == 250 and 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) and 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) and Rizer.direction == 0:
super(Rizer, self).__init__(image = Rizer.image4,
x = self.x,
bottom = self.bottom)
up = 1
if games.keyboard.is_pressed(games.K_w) and Rizer.direction == 1:
super(Rizer, self).__init__(image = Rizer.image5,
x = self.x,
bottom = self.bottom)
up = 2
if games.keyboard.is_pressed(games.K_s) and 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) and Rizer.direction == 1:
super(Rizer, self).__init__(image = Rizer.image7,
x = self.x,
bottom = self.bottom)
crouch = 1
#Jumping Portion with 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 it so that you come back down after 1 second of jumping.
if timer:
timer -= 1
else:
flag = False
self.y = self.y + 2
#Make sure you can't jump too high
if self.y < 100:
self.y = 100
if self.x < 35:
self.x = 35
#Kill self if Rizer touches another object.
if self.overlapping_sprites:
self.destroy()
#Return the sprite to standing form after a jump
if self.bottom < 250:
super(Rizer, self).__init__(image = Rizer.image11,
x = self.x,
bottom = self.bottom)
class Bean(Collider):
#Player Two
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):
An enemy with a 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 that the 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):
""" A bullet launched by a 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 and Rizer.crouch == 0 and Rizer.up == 0:
x = rizer_x + 60
y = rizer_y - 20
dx = self.VELOCITY_FACTOR
dy = 0
if Rizer.direction == 1 and Rizer.crouch == 0 and Rizer.up == 0:
x = rizer_x - 60
y = rizer_y - 20
dx = -self.VELOCITY_FACTOR
dy = 0
if Rizer.crouch == 2 and Rizer.up == 0:
x = rizer_x + 120
y = rizer_y + 40
dx = self.VELOCITY_FACTOR
dy = 0
if Rizer.crouch == 1 and 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 the bullet. """
super(Bullet, self).update()
# if lifetime is up, destroy the 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()
答案 0 :(得分:0)
创建包含不同精灵组的列表,即:allSpriteList,allButPlayerList ......
然后,您可以在类中使用类似的方法来表示不是播放器的所有对象并迭代地调用它们。
for sprite in allSpritesExceptPlayer:
Sprite.move_left()
如果您需要具体信息,请告诉我。 Pygame有一个有用的Group类,值得研究。