简体   繁体   中英

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. 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:

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:

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. 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.

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()
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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