1

好的,所以我现在已经尝试修复我的 Pygame 代码 5 天,这样我就可以像平台精灵一样绘制怪物精灵了。

我的代码找不到错误,我的朋友也找不到。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame, random, sys, os
from pygame.locals import *

pygame.init() #pygame initieres

#skærm indstillinger
window_size = [800, 600]
window_mode = 0
window_color_rate = 32
screen = pygame.display.set_mode(window_size, window_mode, window_color_rate)
background_image = 'background.jpg'
background = (pygame.transform.scale(pygame.image.load(background_image).convert(), (800, 600)))
pygame.display.set_caption('I Wanna Be the Reindeer')
clock = pygame.time.Clock()

class Platform(pygame.sprite.Sprite):
    def __init__(self, color, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)
        platform_image = 'platform.jpg'
        self.image = pygame.transform.scale(pygame.image.load(platform_image).convert(), ([width, height]))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

class Monster(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)
        monster_image = 'platform.jpg'
        self.image = pygame.transform.scale(pygame.image.load(monster_image).convert(), ([width, height]))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

class Goal(pygame.sprite.Sprite):
    def __init__(self, color, x, y, width, height):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

class Player(pygame.sprite.Sprite):
    #variabler spilleren ændre
    change_x = 0
    change_y = 0
    jump_ready = False
    frame_since_collision = 0
    frame_since_jump = 0
    levelpoints = 0
    player_right = 'mobright.png'
    player_left = 'mobleft.png'

    def __init__(self, color, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(pygame.image.load(Player.player_right).convert_alpha(), [30, 30])
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def changespeed_x(self, x):
        self.change_x = x

    def changespeed_y(self, y):
        self.change_y = y

    def jump(self):
        self.jump_ready = True
        self.frame_since_jump = 0

    def update(self, platforms):
        old_x = self.rect.x
        new_x = old_x + self.change_x
        self.rect.x = new_x

        old_y = self.rect.y
        new_y = old_y + self.change_y
        self.rect.y = new_y

        collide = pygame.sprite.spritecollide(self, platforms, False)
        if collide:
            self.rect.x = old_x

        platform_hit_list = pygame.sprite.spritecollide(self, platforms, False)
        for platform in platform_hit_list:
            self.rect.x = old_x
            self.rect.y = old_y
            self.change_y = 0
            self.frame_since_collision = 0

        if self.frame_since_collision < 1 and self.frame_since_jump < 6:
            self.frame_since_jump = 5
            self.change_y -= 10

        self.frame_since_collision += 1
        self.frame_since_jump += 1

    def gravity(self):
        self.change_y += 0.35

        if self.rect.y >= 570 and self.change_y >= 0:
            self.change_y = 0
            self.rect.y = 570
            self.frame_since_collision = 0

    def goalcheck(self, goals):
        goal_hit_list = pygame.sprite.spritecollide(self, goals, True)
        for goals in goal_hit_list:
            Player.levelpoints += 1

'''class Monster(pygame.sprite.Sprite):
    global walk_quota
    walk_quota = 0
    def __init__(self, x, y, walk_distance, walk_interval):
        pygame.sprite.Sprite.__init__(self)
        monster_image = 'monsterright.png'
        self.image = pygame.transform.scale(pygame.image.load(monster_image).convert_alpha(), [35, 35])
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.walk_distance = walk_distance
        self.walk_interval = walk_interval

    def walking(self):
        global walk_quota
        if self.walk_distance > walk_quota:
            self.rect.x += self.walk_interval
            walk_quota += self.walk_interval
            print walk_quota
'''

    #spilleren skabes
player = Player([255, 255, 0], 20, 15)
player.rect.x = 700
player.rect.y = 550

#monster funktioner
#def monsterwalk():
#   for monster in monster_list:
#       monster.walking()

#gruppe indstillinger
all_sprites_list = pygame.sprite.Group()
platform_list = pygame.sprite.Group()
goal_list = pygame.sprite.Group()
monster_list = pygame.sprite.Group()
all_sprites_list.add(player)
all_sprites_list.add(platform_list)
all_sprites_list.add(goal_list)
all_sprites_list.add(monster_list)

#level 1
def level1():
    if Player.levelpoints == 0:
        goal = goal_list.add(Goal((0, 255, 0), 50, 80, 20, 20))
        monster = monster_list.add(Monster(400, 600, 200, 200))
        for i in range(1, 6):
            platform = platform_list.add(
                [Platform((0, 0, 255), 50, 100, i * 20, 20)])
        for i in range(1, 6):
            platform = platform_list.add(
                [Platform((0, 0, 255), 150, 200, i * 20, 20)])
        for i in range(1, 6):
            platform = platform_list.add(
                [Platform((0, 0, 255), 250, 300, i * 20, 20)])
        for i in range(1, 6):
            platform = platform_list.add(
                [Platform((0, 0, 255), 350, 400, i * 20, 20)])
        for i in range(1, 6):
            platform = platform_list.add(
                [Platform((0, 0, 255), 450, 500, i * 20, 20)])
        Player.levelpoints += 1

#level2
def level2():
    if Player.levelpoints == 2:
        platform_list.empty()
        platform = platform_list.add(
        [Platform((0, 0, 255), 600, 200, 100, 20)], 
        [Platform((0, 0, 255), 600, 300, 100, 20)], 
        [Platform((0, 0, 255), 600, 400, 100, 20)])
        Player.levelpoints += 1

#*------Main loop------*
while True: 
    #styring af spiller
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == K_LEFT:
                player.changespeed_x(-4)
                player.image = pygame.transform.scale(pygame.image.load(Player.player_left).convert_alpha(), [30, 30])
            if event.key == K_RIGHT:
                player.changespeed_x(4)
                player.image = pygame.transform.scale(pygame.image.load(Player.player_right).convert_alpha(), [30, 30])
            if event.key == K_DOWN:
                player.changespeed_y(6)
            if event.key == K_UP:
                player.jump()
        if event.type == KEYUP:
            if event.key == K_LEFT:
                player.changespeed_x(0)
            if event.key == K_RIGHT:
                player.changespeed_x(0)

    if player.rect.x >= 800:
        player.rect.x = -15

    if player.rect.x <= -20:
        player.rect.x = 799

    #initiering af levels
    level1()
    level2()

    #funktioner til opdating af objekter
    player.gravity()
    player.update(platform_list)
    player.goalcheck(goal_list)
    platform_list.update()
    monster_list.update()
    #monsterwalk()

    #sprites og billeder der skal tegnes på skærmen
    screen.fill((0, 0, 0))
    screen.blit(background, [0, 0])
    all_sprites_list.draw(screen)
    platform_list.draw(screen)
    monster_list.draw(screen)
    goal_list.draw(screen)

    clock.tick(500)
    print Player.levelpoints
    pygame.display.update()

就像现在一样,我可以让我的球员、目标和平台展示,但不能展示怪物。

注意:怪物只使用平台图像作为占位符。

4

1 回答 1

3

您只需将其绘制到屏幕外即可。

屏幕是800x600,您在 处绘制它400, 600,因此它位于屏幕下边缘的正下方:

在此处输入图像描述

400, 600 - height_of_monster_image例如,改为尝试。

于 2013-02-26T16:43:07.523 回答