简体   繁体   English

我想知道为什么当我移动播放器时,我的播放器和气球都是精灵

[英]I'm wondering why my player and my balloon which are both sprites is stuttering when I move the player

My frames drop and the game basically stops whenever I use the WASD keys, which are my movement controls.每当我使用 WASD 键(这是我的移动控件)时,我的帧数就会下降,游戏基本上会停止。 Its supposed to make the water balloon move but when I stop moving so does the water balloon.它应该使水气球移动,但当我停止移动时,水气球也会移动。 They're both sprites.他们都是精灵。 I might have got them mixed up since there are two sprite families.我可能把它们弄混了,因为有两个精灵家族。

main.py:主要.py:

import pygame
from player import Player
from projectile import WaterBaloon

# Start the game
pygame.init()
game_width = 1000
game_height = 650
screen = pygame.display.set_mode((game_width, game_height))
clock = pygame.time.Clock()
running = True

bg_image = pygame.image.load("../assets/BG_Sand.png")

#make all the sprite groups
playerGroup = pygame.sprite.Group()
projectilesGroup = pygame.sprite.Group()

#put every sprite class in a group
Player.containers = playerGroup
WaterBaloon.containers = projectilesGroup
#tell the sprites where they are gonna be drawn at
mr_player = Player(screen, game_width/2, game_height/2)

# ***************** Loop Land Below *****************
# Everything under 'while running' will be repeated over and over again
while running:
    # Makes the game stop if the player clicks the X or presses esc
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
            
        #player input that detects keys (to move)       
        keys = pygame.key.get_pressed()
        if keys[pygame.K_d]:
            mr_player.move(1, 0)
        if keys[pygame.K_a]:
            mr_player.move(-1, 0)
        if keys[pygame.K_w]:
            mr_player.move(0, -1)
        if keys[pygame.K_s]:
            mr_player.move(0, 1)
        if pygame.mouse.get_pressed()[0]:
            mr_player.shoot()
#blit the bg image
        screen.blit((bg_image),(0, 0))
#update the player
        mr_player.update()
#update all the projectiles
        for projectile in projectilesGroup:
            projectile.update()
            
        # Tell pygame to update the screen
        pygame.display.flip()
        clock.tick(60)
        pygame.display.set_caption("ATTACK OF THE ROBOTS fps: " + str(clock.get_fps()))

player.py:播放器.py:

import pygame
import toolbox
import projectile
#Players Brain
class Player(pygame.sprite.Sprite):
#player constructor function
    def __init__(self, screen, x, y):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.screen = screen
        self.x = x
        self.y = y
        self.image = pygame.image.load("../assets/player_03.png")
        #rectangle for player sprite
        self.rect = self.image.get_rect()
        self.rect.center = (self.x, self.y)
        #end of rectangle for player sprite
        self.speed = 8
        self.angle = 0
        
    #player update function
    def update(self):
        self.rect.center = (self.x, self.y)

        mouse_x, mouse_y = pygame.mouse.get_pos()
        self.angle = toolbox.angleBetweenPoints(self.x, self.y, mouse_x, mouse_y)
            
        #get the rotated version of player
        image_to_draw, image_rect = toolbox.getRotatedImage(self.image, self.rect, self.angle)
        
        
        self.screen.blit(image_to_draw, image_rect)
        
    #tells the computer how to make the player move
    def move(self, x_movement, y_movement):
        self.x += self.speed * x_movement
        self.y += self.speed * y_movement

#shoot function to make a WaterBaloon
    def shoot(self):
        projectile.WaterBaloon(self.screen, self.x, self.y, self.angle)

You're only processing one event per frame.每帧只处理一个事件。 That's because you've put everything you do inside the frame in the event handling loop.那是因为你已经把你所做的一切都放在了事件处理循环的框架内。 That is likely to break down when you start getting events (such as key press and release events) at a higher rate.当您开始以更高的速率获取事件(例如按键和释放事件)时,这可能会崩溃。 If events are not all being processed, your program may not update the screen properly.如果未处理所有事件,则您的程序可能无法正确更新屏幕。

You probably want your main loop to only do event processing stuff in the for event in pygame.event.get(): loop.您可能希望您的主循环仅for event in pygame.event.get():进行事件处理。 Everything else should be unindented by one level, so that it runs within the outer, while running: loop, after all pending events have been handled.其他所有内容都应该取消一级缩进,以便在处理完所有未决事件之后, while running:循环。

Try something like this:尝试这样的事情:

while running:
    # Makes the game stop if the player clicks the X or presses esc
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
            
    #player input that detects keys (to move)    # unindent here (and all the lines below)
    keys = pygame.key.get_pressed()
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM