精灵不能移动python pygame

时间:2020-07-17 06:58:41

标签: python pygame

我刚刚完成了python崩溃课程的第12章,我想为我的python pygame代码添加更多功能。我已经成功添加了旋转功能,使我的飞船面对鼠标,但是我的飞船似乎有点卡住了,不想移动。请帮忙!

这是我的代码

外星人入侵.py

def main():
    pygame.init()
    setting = Settings()
    screen = pygame.display.set_mode((setting.screen_width, setting.screen_height))
    pygame.display.set_caption("Alien Invasion")
    bullets = Group()

    while True:
        mouse_position = pygame.mouse.get_pos()
        ship = Ship(screen,mouse_position)
        gf.check_events(screen, ship, bullets)
        ship.ship_movement()
        gf.update_bullet(bullets)
        gf.update_screen(setting, screen, ship, bullets)

    

ship.py

import pygame
import math
class Ship():
    def __init__(self, screen,mouse_position):
        # initialize the ship and set its starting position
        self.screen = screen

        # Load the ship image
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect() # getting the image rectangle
        self.screen_rect = screen.get_rect() # getting the screen rectangle

        # start each new ship at the bottom center of the screen
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
    
        # if True then it'll move right/left
        self.move_right = False
        self.move_left = False
        self.move_up = False
        self.move_down = False
        # set speed
        self.set_speed = 1.5
        # store the decimal value for the ship's center
        self.center_x = float(self.rect.centerx)
        self.center_y = float(self.rect.bottom)
        
        # player rotation
        self.mouse_position = mouse_position
        self.angle = math.atan2(self.mouse_position[1] - (self.rect.centery), self.mouse_position[0] - (self.rect.centerx))
        self.player_rotation = pygame.transform.rotate(self.image, -self.angle * 57.29 - 90)
        self.new_playerpos = self.rect.centerx - self.player_rotation.get_rect().width / 2, self.rect.centery - self.player_rotation.get_rect().height / 2
        

    def ship_movement(self):
        if self.move_right and self.rect.right < self.screen_rect.right:
            self.center_x += self.set_speed

        if self.move_left and self.rect.left > 0:
            self.center_x -= self.set_speed

        if self.move_up and self.rect.top > self.screen_rect.top:
            self.center_y -= self.set_speed

        if self.move_down and self.rect.bottom < self.screen_rect.bottom:
            self.center_y += self.set_speed

        self.rect.centery = self.center_y - 30
        self.rect.centerx = self.center_x
        
            
    def blit_screen(self):
        # Draw the ship at it's current location
        self.screen.blit(self.player_rotation, self.new_playerpos)

        # self.screen.blit(self.image, self.rect)

游戏功能。py


import pygame
from pygame.locals import *
import sys
from bullet import Bullet

def check_keydown_events(event,screen, ship, bullets):
    if event.key == K_RIGHT or event.key == K_d:
        ship.move_right = True
    elif event.key == K_LEFT or event.key == K_a:
        ship.move_left = True
    elif event.key == K_UP or event.key == K_w:
        ship.move_up = True
    elif event.key == K_DOWN or event.key == K_s:
        ship.move_down = True
    elif event.key == K_SPACE:
        fire_bullet(screen, ship, bullets)
        
def check_keyup_events(event, ship):
    if event.key == K_RIGHT or event.key == K_d:
        ship.move_right = False
    elif event.key == K_LEFT or event.key == K_a:
        ship.move_left = False
    elif event.key == K_UP or event.key == K_w:
        ship.move_up = False
    elif event.key == K_DOWN or event.key == K_s:
        ship.move_down = False

def fire_bullet(screen, ship, bullets):
    # create new bullet and add it to the bullet group
    new_bullet = Bullet(screen, ship)
    if len(bullets) < new_bullet.bullet_limit:
        bullets.add(new_bullet)

def check_events(screen, ship, bullets):
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

        elif event.type == KEYDOWN:
            check_keydown_events(event,screen, ship, bullets)   

        elif event.type == KEYUP:
            check_keyup_events(event, ship)

def update_bullet(bullets):
    # removing bullet that has already disapeared
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

def update_screen(setting, screen, ship, bullets):
    screen.fill(setting.bg_color)
    
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    ship.blit_screen()
    pygame.display.flip()


1 个答案:

答案 0 :(得分:0)

问题在于Ship对象在其初始位置的每个帧中都被重新创建。您必须在主应用程序循环之前创建Ship的实例:

def main():
    # [...]

    ship = Ship(screen,mouse_position) # <--- INSERT

    while True:
        
        # ship = Ship(screen,mouse_position) <--- DELETE
        
        # [...]

必须通过方法ship_movement而不是构造函数来更新船的旋转角度:

def main():
    # [...]

    while True:
        # [...]

        mouse_position = pygame.mouse.get_pos()
        ship.ship_movement(mouse_position)

        # [...] 
class Ship():
    # [...]

    def ship_movement(self, mouse_position):
        if self.move_right and self.rect.right < self.screen_rect.right:
            self.center_x += self.set_speed

        if self.move_left and self.rect.left > 0:
            self.center_x -= self.set_speed

        if self.move_up and self.rect.top > self.screen_rect.top:
            self.center_y -= self.set_speed

        if self.move_down and self.rect.bottom < self.screen_rect.bottom:
            self.center_y += self.set_speed

        self.rect.centery = self.center_y - 30
        self.rect.centerx = self.center_x

        # player rotation
        self.mouse_position = mouse_position
        self.angle = math.atan2(self.mouse_position[1] - (self.rect.centery), self.mouse_position[0] - (self.rect.centerx))
        self.player_rotation = pygame.transform.rotate(self.image, -self.angle * 57.29 - 90)
        self.new_playerpos = self.rect.centerx - self.player_rotation.get_rect().width / 2, self.rect.centery - self.player_rotation.get_rect().height / 2