def loadresources(self):
"""painting on the surface (once) and create sprites"""
# make an interesting background
draw_examples(self.background) # background artwork
try: # ----------- load sprite images -----------
tile = pygame.image.load(os.path.join("data", "startile-300px.png"))
PygView.images.append(pygame.image.load(os.path.join("data", "babytux.png"))) # index 0
PygView.images.append(pygame.image.load(os.path.join("data", "player_red2.png")))
PygView.images.append(pygame.image.load(os.path.join("data", "babytux_neg.png")))
PygView.images.append(pygame.image.load(os.path.join("data", "player_blue2.png"))) # index 3
# load other resources here
except:
print("pygame error:", pygame.get_error())
print("please make sure there is a subfolder 'data' with the resource files in it")
pygame.quit()
sys.exit()
# fill background with tiles
self.background = fill_surface_with_tiles(tile, self.width, self.height)
# write text
# ------- write text over everything -----------------
write(self.background, "Press b to add another ball", x=self.width//2, y=250, center=True)
write(self.background, "Press c to add another bullet", x=self.width//2, y=275, center=True)
write(self.background, "Press w,a,s,d and q,e to steer tux", x=self.width//2, y=300, center=True)
write(self.background, "Press space to fire from tux", x=self.width//2, y=325, center=True)
self.paintgrid()
# ------- create (pygame) Sprites Groups and Sprites -------------
self.allgroup = pygame.sprite.LayeredUpdates() # for drawing
self.ballgroup = pygame.sprite.Group() # for collision detection etc.
self.bargroup = pygame.sprite.Group()
self.bulletgroup = pygame.sprite.Group()
self.fire_at_player_group = pygame.sprite.Group()
self.tuxgroup = pygame.sprite.Group()
self.neutralgroup = pygame.sprite.Group()
self.fleegroup = pygame.sprite.Group()
self.missilegroup = pygame.sprite.Group()
# ----- assign Sprite class to sprite Groups -------
Tux.groups = self.allgroup, self.tuxgroup
Basebar.groups = self.bargroup
Ball.groups = self.allgroup, self.ballgroup, self.fire_at_player_group # each Ball object belong to those groups
Bullet.groups = self.allgroup, self.bulletgroup
Doenertier.groups = self.allgroup, self.neutralgroup, self.fleegroup
Missile.groups = self.allgroup, self.missilegroup, self.bulletgroup
self.doenertier1 = Doenertier()
self.ball1 = Ball(x=100, y=100, radius = 10) # creating a Ball Sprite
self.ball2 = Ball(x=200, y=100, radius = 20) # create another Ball Sprite
self.tux1 = Tux(x=400, y=200, dx=0, dy=0, layer=5, imagenr = 0) # over balls layer
self.tux2 = Tux(x=200, y=400, dx=0, dy=0, layer=5, imagenr = 1)
print(len(self.joysticks))
if len(self.joysticks) > 2:
self.tux3 = Tux(x=300, y=300, dx=0, dy=0, layer=5, imagenr = 2)
if len(self.joysticks) > 3:
self.tux4 = Tux(x=100, y=500, dx=0, dy=0, layer=5, imagenr = 3)
评论列表
文章目录