为什么Pygame Blit不能在课堂上工作

时间:2018-11-10 11:58:15

标签: python pygame

我正在尝试使用带有类的pygame创建自己的游戏,但我的问题是我无法理解为什么我的程序无法正常工作

import pygame
import time
import random
import sys

pygame.init()
screen = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("this game")


class Background:

    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    picture = pygame.image.load("C:/images/dunes.jpg")
    picture = pygame.transform.scale(picture, (1280, 720))

    def draw(self):
        pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))



class Monster:
    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    def move_left(self):
        self.xpos =- 5

    def move_right(self):
        self.xpos =+ 5

    def jump(self):
        for x in range(1, 10):
            self.ypos =-1
            pygame.display.show()

        for x in range(1, 10):
            self.ypos =+1
            pygame.display.show()

    picture = pygame.image.load("C:/pics/hammerhood.png")
    picture = pygame.transform.scale(picture, (200, 200))

    def draw(self):
        pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))




class enemy:
    def __init__(self, x, y):
        self.xpos = x
        self.ypos = y

    picture = pygame.image.load("C:/pics/dangler_fish.png")
    picture = pygame.transform.scale(picture, (200, 200))


    def teleport(self):
        self.xpos = random.randint(1, 1280)
        self.pos= random.randint(1, 720)

    def draw(self):
        pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))






while True:
    ice = Background(720, 360)
    hammerhood = Monster(200, 500)
    fish = enemy(0, 0)
    fish.teleport()


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if pygame.event == pygame.K_d:
            hammerhood.move_right()

        if pygame.event == pygame.K_a:
            hammerhood.move_left()

        if pygame.event == pygame.K_w:
            hammerhood.jump()

    hammerhood.draw()
    ice.draw()
    fish.draw()

在抽奖中我对49行说的是

pygame.surface.Surface.blit(picture, (self.xpos, self.ypos))
NameError: name 'picture' is not defined

我已经尝试了所有方法,还有另一个项目是我从互联网上复制的,这与在班级里给图片上色是完全一样的,但是在这个项目中,它不起作用

2 个答案:

答案 0 :(得分:1)

您可以在班级顶层定义图片,这使其成为班级属性。要通过某种方法访问它,您必须使用self. picture

答案 1 :(得分:0)

picture属性是类属性(在__init__方法之外定义),但是可以像其他任何属性一样通过在其前面加上self.来访问它们。另外,您很可能希望将图片放大到screen表面上,因此只需调用此表面的blit方法即可:

def draw(self):
    screen.blit(self.picture, (self.xpos, self.ypos))

请注意,类属性在所有实例之间共享,因此,如果您修改一个表面/图像,则所有精灵都将被更改。

还有更多问题:

  • 事件处理不起作用。
  • 子画面不会连续移动。定义一些speed属性,并将它们添加到每一帧的位置。
  • 您几乎应该始终调用convertconvert_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.
相关问题