Атрибуты picture
являются атрибутами класса (определены вне метода __init__
), но к ним можно получить доступ, как и к любым другим атрибутам, добавив к ним self.
. Кроме того, вы, скорее всего, хотите скопировать изображения на поверхность screen
, поэтому просто вызовите метод blit
этой поверхности:
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
Обратите внимание, что атрибуты класса являются общими для всех экземпляров, поэтому, если вы измените одну поверхность / изображение, они будут изменены для всех спрайтов.
Есть еще несколько проблем:
- Не работает обработка событий.
- Спрайты не будут двигаться непрерывно. Определите некоторые
speed
атрибуты и добавьте их в позиции каждого кадра.
- Вы почти всегда должны вызывать методы
convert
или convert_alpha
, чтобы улучшить блиц-характеристики поверхностей.
Вот исправленная версия (кроме прыжков) с некоторыми комментариями:
import pygame
import random
import sys
pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock() # A clock to limit the frame rate.
pygame.display.set_caption("this game")
class Background:
picture = pygame.image.load("C:/images/dunes.jpg").convert()
picture = pygame.transform.scale(picture, (1280, 720))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def draw(self):
# Blit the picture onto the screen surface.
# `self.picture` not just `picture`.
screen.blit(self.picture, (self.xpos, self.ypos))
class Monster:
picture = pygame.image.load("C:/pics/hammerhood.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
# If you want to move continuously you need to set these
# attributes to the desired speed and then add them to
# self.xpos and self.ypos in an update method that should
# be called once each frame.
self.speed_x = 0
self.speed_y = 0
def update(self):
# Call this method each frame to update the positions.
self.xpos += self.speed_x
self.ypos += self.speed_y
# Not necessary anymore.
# def move_left(self):
# self.xpos -= 5 # -= not = -5 (augmented assignment).
#
# def move_right(self):
# self.xpos += 5 # += not = +5 (augmented assignment).
def jump(self):
# What do you want to do here?
for x in range(1, 10):
self.ypos -= 1 # -= not =-
# pygame.display.show() # There's no show method.
for x in range(1, 10):
self.ypos += 1
# pygame.display.show()
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
class Enemy: # Use upper camelcase names for classes.
picture = pygame.image.load("C:/pics/dangler_fish.png").convert_alpha()
picture = pygame.transform.scale(picture, (200, 200))
def __init__(self, x, y):
self.xpos = x
self.ypos = y
def teleport(self):
self.xpos = random.randint(1, 1280)
self.pos= random.randint(1, 720)
def draw(self):
screen.blit(self.picture, (self.xpos, self.ypos))
# Create the instances before the while loop.
ice = Background(0, 0) # I pass 0, 0 so that it fills the whole screen.
hammerhood = Monster(200, 500)
fish = Enemy(0, 0)
while True:
# Handle events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Check if the `event.type` is KEYDOWN first.
elif event.type == pygame.KEYDOWN:
# Then check which `event.key` was pressed.
if event.key == pygame.K_d:
hammerhood.speed_x = 5
elif event.key == pygame.K_a:
hammerhood.speed_x = -5
elif event.key == pygame.K_w:
hammerhood.jump()
elif event.type == pygame.KEYUP:
# Stop moving when the keys are released.
if event.key == pygame.K_d and hammerhood.speed_x > 0:
hammerhood.speed_x = 0
elif event.key == pygame.K_a and hammerhood.speed_x < 0:
hammerhood.speed_x = 0
# Update the game.
hammerhood.update()
fish.teleport()
# Draw everything.
ice.draw() # Blit the background to clear the screen.
hammerhood.draw()
fish.draw()
pygame.display.flip()
clock.tick(60) # Limit the frame rate to 60 FPS.