簡體   English   中英

精靈無法移動 python pygame

[英]sprite can't move 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)

    

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


問題是Ship object 在其初始 position 的每一幀中重新創建。 您必須在主應用程序循環之前創建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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM