python - 我的游戏角色只有当鼠标在屏幕上移动时才会移动,而它需要鼠标

标签 python pygame

我刚刚开始学习 pygame,问题是:当鼠标在屏幕上移动时,我正在制作的游戏角色正在移动,即使我不需要使用鼠标并且我需要帮助。 我添加了一些子弹代码,这部分现在对我来说并不重要,因为如果不在屏幕上移动鼠标, spaceship 就不会移动。

import pygame
from pygame.locals import*

#define fps
clock = pygame.time.Clock()
fps=60


screen_width = 600
screen_height = 800

screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption(' space invaders')

#colors
red=(255,0,0)
green=(0,255,0)


#load image
bg = pygame.image.load("img/bg.png")

def draw_bg():
    screen.blit(bg,(0,0))




#create spaceship class
class Spaceship(pygame.sprite.Sprite):
    def __init__(self,x,y,health):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("img/spaceship.png")
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
        self.health_start = health
        self.health_remaining = health
        self.last_shot = pygame.time.get_ticks()

    def update(self):
        #set movement speed
        speed=8

        #get key press
        key=pygame.key.get_pressed()
        if key[pygame.K_LEFT] and self.rect.left>0:
            self.rect.x-=speed

        if key[pygame.K_RIGHT] and self.rect.right<screen_width:
            self.rect.x+=speed

        time_now=pygame.time.get_ticks()

        #shoot

        if key[pygame.K_SPACE]:
            bullet = Bullet(self.rect.centerx,self.rect.top)

            bullet_group.add(bullet)
            
        pygame.mouse.get_pos()



        #draw health bar
        pygame.draw.rect(screen,red,(self.rect.x,(self.rect.bottom+10),self.rect.width,15))
        
        if self.health_remaining>0:
            pygame.draw.rect(screen,green,(self.rect.x,(self.rect.bottom+10),int(self.rect.width*(self.health_remaining / self.health_start)),15))



#create Bullets class
class Bullets(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load("img/bullet.png")
        self.rect = self.image.get_rect()
        self.rect.center=[x,y]
    
    def update(self):
        self.rect.y-=5




# sprite groups   
spaceship_group = pygame.sprite.Group()
bullet_group=pygame.sprite.Group()




#create player
spaceship=Spaceship(int(screen_width / 2),screen_height-100 ,3)
spaceship_group.add(spaceship)


run = True
while run:
    clock.tick(fps)
    draw_bg()

    #events 
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False

        #update spaceship
        spaceship.update()

        update bullets
        bullet_group.update()

        
        spaceship_group.draw(screen)
        bullet_group.draw(screen)


        pygame.display.update()
        
pygame.quit



 

最佳答案

看起来像是缩进问题。

当您的代码在 for event in pygame.event.get() 循环内缩进时,它仅在 pygame 接收事件时运行。您可以通过 MOUSEMOTION 事件注意到这一点。

run = True
while run:
    clock.tick(fps)
    draw_bg()

    #events 
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            run=False

    # <---- moved this one indentation level, out of the evnet loop
    #update spaceship
    spaceship.update()

    #update bullets
    bullet_group.update()
   
    spaceship_group.draw(screen)
    bullet_group.draw(screen)

    pygame.display.update()

关于python - 我的游戏角色只有当鼠标在屏幕上移动时才会移动,而它需要鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68633168/

相关文章:

ruby - 如何创建与帧缓冲区通信的 Ruby 应用程序?

python - 为 mac 制作 pygame/SDL 安装程序

python - 如何在图片文件名和循环中使用变量来使用 pygame 打开多张图片而不会使代码困惑

python - 从 Socket.File.Read 部分读取

python - OpenCV 从源 Windows 生成错误 "RC Object"

python - 如何异步使用 Tornado 和 Redis?

python - 在 for 循环中将小矩形打印到屏幕上。 (pygame)

python - 如何减慢动画速度

python - 为椭圆体内的 3D 数组中的点赋值

python - 以错误的方式迭代和打印列表