[英]How do I assign Images to Sprites with pygame
我正在努力在下面的代碼中為每個精靈提供自己的圖像,我似乎無法找到一種方法來做到這一點,如果有人能提供幫助,將不勝感激。 謝謝。 (這是我第一次使用堆棧溢出,如果格式混亂或錯誤,請原諒)另外讓我知道是否應該放入文本映射文件。
下面的代碼:
import pygame
from pygame import *
from pygame import mixer
import pygame.freetype
import time
SCREEN_SIZE = pygame.Rect((0, 0, 800, 640))
TILE_SIZE = 32
Map_border = (1000, 500)
GRAVITY = pygame.Vector2((0, 0.4))
STONE_IMAGE = pygame.image.load("stone.png")
class CameraAwareLayeredUpdates(pygame.sprite.LayeredUpdates):
def __init__(self, target, world_size):
super().__init__()
self.target = target
self.cam = pygame.Vector2(0, 0)
self.world_size = world_size
if self.target:
self.add(target)
def update(self, *args):
super().update(*args)
if self.target:
x = -self.target.rect.center[0] + SCREEN_SIZE.width / 2
y = -self.target.rect.center[1] + SCREEN_SIZE.height / 2
self.cam += (pygame.Vector2((x, y)) - self.cam) * 0.5
self.cam.x = max(-(self.world_size.width - SCREEN_SIZE.width), min(0, self.cam.x))
self.cam.y = max(-(self.world_size.height - SCREEN_SIZE.height), min(0, self.cam.y))
def draw(self, surface):
spritedict = self.spritedict
surface_blit = surface.blit
dirty = self.lostsprites
self.lostsprites = []
dirty_append = dirty.append
init_rect = self._init_rect
for spr in self.sprites():
rec = spritedict[spr]
newrect = surface_blit(spr.image, spr.rect.move(self.cam))
if rec is init_rect:
dirty_append(newrect)
else:
if newrect.colliderect(rec):
dirty_append(newrect.union(rec))
else:
dirty_append(newrect)
dirty_append(rec)
spritedict[spr] = newrect
return dirty
def main():
Music()
Map = open("map.txt")
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE.size)
pygame.display.set_caption("Good Luck")
timer = pygame.time.Clock()
platforms = pygame.sprite.Group()
player = Player(platforms, (TILE_SIZE, TILE_SIZE))
level_width = Map_border[0] * TILE_SIZE
level_height = Map_border[1] * TILE_SIZE
entities = CameraAwareLayeredUpdates(player, pygame.Rect(0, 0, level_width, level_height))
# build the map_border
x = y = 0
for row in Map:
for col in row:
if col == "P":
PlatBasic((x, y), platforms, entities)
if col == "E":
PlatExit((x, y), platforms, entities)
if col == "S":
PlatSpeedH((x, y), platforms, entities)
if col == "M":
PlatSpeedM((x, y), platforms, entities)
if col == "L":
PlatSpeedL((x, y), platforms, entities)
if col == "N":
PlatNorm((x, y), platforms, entities)
if col == "J":
PlatJump((x, y), platforms, entities)
x += TILE_SIZE
y += TILE_SIZE
x = 0
while 1:
for e in pygame.event.get():
if e.type == QUIT:
return
if e.type == KEYDOWN and e.key == K_ESCAPE:
return
entities.update()
screen.fill((0, 0, 0))
entities.draw(screen)
pygame.display.update()
timer.tick(60)
class Entity(pygame.sprite.Sprite):
def __init__(self, color, pos, *groups):
super().__init__(*groups)
self.image = Surface((TILE_SIZE, TILE_SIZE))
self.image.fill(color)
self.rect = self.image.get_rect(topleft=pos)
class Player(Entity):
def __init__(self, platforms, pos, *groups):
super().__init__(Color("#5aa2e0"), pos)
self.vel = pygame.Vector2((0, 0))
self.onGround = False
self.platforms = platforms
self.speed = 8
self.jump_strength = 10
def update(self):
pressed = pygame.key.get_pressed()
up = pressed[K_SPACE]
left = pressed[K_LEFT]
right = pressed[K_RIGHT]
running = pressed[K_UP]
if up:
# only jump if on the ground
if self.onGround: self.vel.y = -self.jump_strength
if left:
self.vel.x = -self.speed
if right:
self.vel.x = self.speed
if running:
self.vel.x *= 1.5
if not self.onGround:
# only accelerate with gravity if in the air
self.vel += GRAVITY
# max falling speed
if self.vel.y > 100: self.vel.y = 100
if not (left or right):
self.vel.x = 0
# increment in x direction
self.rect.left += self.vel.x
# do x-axis collisions
self.collide(self.vel.x, 0, self.platforms)
# increment in y direction
self.rect.top += self.vel.y
# assuming we're in the air
self.onGround = False;
# do y-axis collisions
self.collide(0, self.vel.y, self.platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, PlatExit):
pygame.event.post(pygame.event.Event(QUIT))
if isinstance(p, PlatSpeedH):
self.speed = 50
if isinstance(p, PlatSpeedM):
self.speed = 16
if isinstance(p, PlatSpeedL):
self.speed = 5
if isinstance(p, PlatNorm):
self.speed = 8
self.jump_strength = 10
if isinstance(p, PlatJump):
self.jump_strength = 30
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.vel.y = 0
if yvel < 0:
self.rect.top = p.rect.bottom
class PlatBasic(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#10eb93"), pos, *groups)
class PlatExit(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#c40c0c"), pos, *groups)
class PlatSpeedH(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#0033FF"), pos, *groups)
class PlatSpeedM(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#0033FF"), pos, *groups)
class PlatSpeedL(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#0033FF"), pos, *groups)
class PlatNorm(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#0033FF"), pos, *groups)
class PlatJump(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#0033FF"), pos, *groups)
class PlatImmune(Entity):
def __init__(self, pos, *groups):
super().__init__(Color("#0033FF"), pos, *groups)
class Music():
mixer.init()
mixer.music.load('bensound-summer_ogg_music.ogg')
mixer.music.play()
if __name__ == "__main__":
main()
感謝您的幫助和信息,我剛剛完成了平台游戲,所以如果有人想查看它,您可以在這里找到運行它所需的一切
在您想要圖像的每個精靈的子類中,您需要添加一個變量來保存要顯示的加載圖像。 然后,當您顯示該精靈時,您只需使用該類中的特定變量。
class PlatBasic(Entity):
def __init__(self, pos, *groups):
self.image = pygame.image.load("PlatBasic.png").convert()
super().__init__(Color("#10eb93"), pos, *groups)
然后,當您希望顯示它時,您可以這樣做:
x = PlatBasic((0,0))
screen.blit(x.image,x.pos)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.