雪碧复制自己..?

时间:2016-08-19 01:14:05

标签: python pygame

在python中进行一些测试时,Player类中的一个精灵随机开始重复自我..我无法弄清楚..(我意识到这很简单)

以下是代码:

import pygame
from pygame.locals import *
import time

pygame.font.init()
myfont = pygame.font.SysFont("mirc", 25)

screen = pygame.display.set_mode((720, 480))
clock = pygame.time.Clock()

health = 10

keys = {"w":False, "a":False, "s":False, "d":False, " ":False}

class Thing:
    def __init__(self, picture):
        self.texture = pygame.image.load(picture).convert()
        self.position = [360, 240]

    def draw(self, screen):
        screen.blit(self.texture, tuple([int(e) for e in self.position]))

    def move(self, direction, distance):
        if direction == "UP":
            self.position[1] -= distance
        elif direction == "DOWN":
            self.position[1] += distance
        elif direction == "RIGHT":
            self.position[0] += distance
        elif direction == "LEFT":
            self.position[0] -= distance

class Enemy:
    def __init__(self, picture):
        self.texture = pygame.image.load(picture).convert()
        self.position = [15, 110]

    def draw(self, screen):
        screen.blit(self.texture, tuple([int(e) for e in self.position]))

    def move(self, distance):
        if player.direction == "UP":
            self.position[1] -= distance
        elif player.direction == "DOWN":
            self.position[0] -= distance
        elif player.direction == "RIGHT":
            self.position[1] += distance
        elif player.direction == "LEFT":
            self.position[1] += distance

done = False
black = (0, 0, 0)

player = Thing("2.png")
enemy = Enemy("1.png")

while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            done = True
        if event.type == KEYDOWN:
            if event.key == K_w:
                keys["w"] = True
            if event.key == K_a:
                keys["a"] = True
            if event.key == K_s:
                keys["s"] = True
            if event.key == K_d:
                keys["d"] = True
            if event.key == K_SPACE:
                keys[" "] = True

        if event.type == KEYUP:
            if event.key == K_w:
                keys["w"] = False
            if event.key == K_a:
                keys["a"] = False
            if event.key == K_s:
                keys["s"] = False
            if event.key == K_d:
                keys["d"] = False
            if event.key == K_SPACE:
                keys[" "] = False

    #Consistent movement
    if keys["w"]:
        player.move("UP", 2)
    if keys["a"]:
        player.move("LEFT", 2)
    if keys["s"]:
        player.move("DOWN", 2)
    if keys["d"]:
        player.move("RIGHT", 2)
    #if keys[" "]:
        #Shoot..

    #Walls - Player
    if player.position[1] <= -2:
        player.position[1] += 2
    if player.position[1] >= 433:
        player.position[1] -= 2
    if player.position[0] <= -2:
        player.position[0] += 2
    if player.position[0] >= 690:
        player.position[0] -= 2
    #Walls - Enemy
    if enemy.position[1] <= -2:
        enemy.position[1] += 2
    if enemy.position[1] >= 433:
        enemy.position[1] -= 2
    if enemy.position[0] <= -2:
        enemy.position[0] += 2
    if enemy.position[0] >= 690:
        enemy.position[0] -= 2

    #Text
    label = myfont.render("Health: " + str(health), 50, (255, 255, 255))
    screen.blit(label, (5, 5))

    player.draw(screen)
    pygame.display.flip()
pygame.quit()

1 个答案:

答案 0 :(得分:0)

原来我的答案很接近 - 你的问题是你只是简单地绘制图像 - 即使你已经宣布black = (0, 0, 0),你也永远不会填满屏幕。在开始绘制其他所有内容之前,您需要先放置screen.fill(black)

对于记录,这是最小,完整且可验证的示例:

import pygame
from pygame.locals import *
import time

pygame.font.init()
myfont = pygame.font.SysFont("mirc", 25)

screen = pygame.display.set_mode((720, 480))
clock = pygame.time.Clock()


class Thing:
    def __init__(self, picture):
        self.texture = pygame.image.load(picture).convert()
        self.position = [360, 240]

    def draw(self, screen):
        screen.blit(self.texture, self.position)


done = False
black = (0, 0, 0)

player = Thing(os.path.expanduser("~/Downloads/disapproval.png"))

x_direction = 1
y_direction = 0

while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            done = True

    if player.position[0] < 400:
        x_direction = 1
        y_direction = 0
    elif player.position[0] >= 400 and player.position[1] > 100:
        x_direction = 0
        y_direction = -1
    else:
        x_direction = 0
        y_direction = 0

    player.position[0] += x_direction
    player.position[1] += y_direction

    # Uncomment this line, and you'll fix your problem
    #screen.fill(black)
    player.draw(screen)
    pygame.display.flip()
pygame.quit()

<强> disapproval.png
disapproval