pygame我的播放器使移动时一切消失

时间:2018-11-04 22:32:40

标签: python class pygame

项目游戏

import pygame
import os 
import random from pygame.locals import * # Constants
import math
import sys
import random

pygame.init()  

screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))     # fill the background white 
White = (255,255,255)

----------------------------------------------------- ----------------------

background = pygame.image.load('stage.png').convert()
screen.blit(background, (0, 0))


class Player(pygame.sprite.Sprite):

def __init__(self):

    self.rect = pygame.draw.rect(screen, (0,0,128), (50,560,50,25)) #(colour)(x-position,y-position,width,height)

    self.dist = 100

def draw_rect(self,x,y):    # This is my code which should make the player move
    screen.blit(background, (0, 0)) #If this isn't included then when the rectangle moves it's old positon will still be on the screen
    self.rect = self.rect.move(x*self.dist, y*self.dist); pygame.draw.rect(screen, (0, 0, 128), self.rect)
    pygame.display.update()

def handle_keys(self): # code to make the character move when the arrow keys are pressed
    for e in pygame.event.get():
        if e.type == KEYDOWN:
            key = e.key
            if key == K_LEFT:
                self.draw_rect(-0.5,0)
            elif key == K_RIGHT:
                self.draw_rect(0.5,0)
            elif key == K_UP:
                self.draw_rect(0,-0.5)
            elif key == K_DOWN:
                self.draw_rect(0,0.5)
            elif key == K_SPACE:
                self.draw_rect(2,-3)
            if self.rect.right > 1400:
                self.rect.right = 1400
            if self.rect.left < 0:
                self.rect.left = 0
            if self.rect.bottom > 500:
                self.rect.bottom = 500
            if self.rect.top < 0:
                self.rect.top = 0




player = Player()

----------------------------------------------------- -------------------------

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine

def __init__(self):
    x = random.randint(50,450)

    self.rect = pygame.draw.rect(screen, (128,0,0), (300,x,50,25))  

enemy = Enemy()


pygame.display.flip()   #updates the whole screen

def main():  #my main loop 
    while True:
        player.handle_keys()

    for event in pygame.event.get
        if event.type == pygame.QUIT:
            running = False
if __name__ == '__main__': main()

玩家确实移动了,但是当按下按键并且玩家移动时,敌人消失了,所以我需要帮助修复它

仅当按住箭头键时,播放器不会移动

1 个答案:

答案 0 :(得分:0)

您正在使用播放器的handle_keys方法检查按键事件。任何按键事件仅注册一次,直到下一次按键。您想使用pygame.key.get_pressed()来返回给定时刻所有按下的键的字典。我已经为您修复了该问题,并整理了一下代码。您可能要考虑使用延迟或pygame的内置Clock类来控制帧频。如果您注意到了,由于您更新帧的速度很快,我不得不将您的移动更改为很小的数字(从0.5更改为0.05)。添加time.sleep()或pygame时钟会解决此问题。

import pygame
import os 
import random 
from pygame.locals import * # Constants
import math
import sys
import random

pygame.init()  

screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))     # fill the background white 
White = (255,255,255)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        self.rect = pygame.draw.rect(screen, (0,0,128), (50,560,50,25)) #(colour)(x-position,y-position,width,height)
        self.dist = 100

    def draw_rect(self,x,y):    # This is my code which should make the player move
        screen.blit(background, (0, 0)) #If this isn't included then when the rectangle moves it's old positon will still be on the screen
        self.rect = self.rect.move(x*self.dist, y*self.dist); pygame.draw.rect(screen, (0, 0, 128), self.rect)
        pygame.display.update()

    def handle_keys(self): # code to make the character move when the arrow keys are pressed
        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            self.draw_rect(-0.05,0)
        elif keys[K_RIGHT]:
            self.draw_rect(0.05,0)
        elif keys[K_UP]:
            self.draw_rect(0,-0.05)
        elif keys[K_DOWN]:
            self.draw_rect(0,0.05)
        elif keys[K_SPACE]:
            self.draw_rect(2,-3)
        if self.rect.right > 1400:
            self.rect.right = 1400
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.bottom > 500:
            self.rect.bottom = 500
        if self.rect.top < 0:
            self.rect.top = 0

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
    def __init__(self):
        x = random.randint(50,450)
        self.rect = pygame.draw.rect(screen, (128,0,0), (300,x,50,25))  

player = Player()
enemy = Enemy()

def main():  #my main loop 
    running = True
    while running:
        player.handle_keys()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        pygame.display.flip()   #updates the whole screen

if __name__ == '__main__': main()