微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使物体保持向上移动

如何解决如何使物体保持向上移动

我正在用Python开发游戏。我在这场比赛中有一艘太空飞船。飞船可以左右移动,应该从其X位置发射子弹,并且应该发射子弹。我可以移动飞船,并且可以将其放在屏幕上。问题是我无法从飞船的x位置发射子弹,也无法使子弹向上移动。有人可以帮我吗?如果可能,请也发布简单的代码

这是代码

import pygame
import sys
pygame.init()
screen_length = 512
screen_width = 288
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width,screen_length))
bg = pygame.image.load(r'C:\Users\Anonymous\Downloads\space game folder\space background3.png').convert()
bg = pygame.transform.scale2x(bg)
spaceship = pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\spaceship.png').convert()
missile = pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\bullet2.png').convert()
x = 130
y = 480
z = 460
veLocity = 30
spaceship_rect = spaceship.get_rect(center=(x,y))
spaceship_x_pos = spaceship_rect.left + 170
bullet_speed = 10
missile_rect = missile.get_rect(center=(round(spaceship_x_pos // 2),z))
spaceship_direction = 10
while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            pygame.display.quit()
            sys.exit(0)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and x < 288:
        x += veLocity
    if keys[pygame.K_LEFT] and x > 0:
        x -= veLocity
    if keys[pygame.K_SPACE]:
        bg.blit(missile,missile_rect)
screen.fill(0)
screen.blit(bg,(0,0))
screen.blit(spaceship,(x,y))
pygame.display.update()
clock.tick(120)

解决方法

您的代码存在问题,就是您的缩进不正确。

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            pygame.display.quit()
            sys.exit(0)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and x < 288:
        x += velocity
    if keys[pygame.K_LEFT] and x > 0:
        x -= velocity
    if keys[pygame.K_SPACE]:
        bg.blit(missile,missile_rect)
screen.fill(0) # THIS
screen.blit(bg,(0,0)) #THIS
screen.blit(spaceship,(x,y)) #THIS
pygame.display.update() #THIS
clock.tick(120) #THIS

我在此评论的行必须像这样缩进:

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            pygame.display.quit()
            sys.exit(0)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and x < 288:
        x += velocity
    if keys[pygame.K_LEFT] and x > 0:
        x -= velocity
    if keys[pygame.K_SPACE]:
        bg.blit(missile,missile_rect)
    screen.fill(0)
    screen.blit(bg,0))
    screen.blit(spaceship,y))
    pygame.display.update()
    clock.tick(120)

那是因为您想在游戏运行时每帧都执行这些操作。还有一件事,您要在screen.fill(0)行中用黑色填充屏幕,这是不必要的,因为您已经在渲染背景图像,因此基本上看不到它。话虽如此,我还是建议您采用面向对象的方法,因为没有它,您将走得太远。另外,将速度设置为0.5左右。当前为30,这意味着每帧30像素,这不是您想要的。

现在要发射子弹。您现在正在执行的操作将无法正常工作,因为如果按下空间,您只会“发抖”导弹。下面我为您编写了一个单独的代码,该代码仅发射导弹,因此希望它清晰易懂,您可以使用自己的代码实现。

import pygame
import sys
pygame.init()

d = pygame.display.set_mode((1200,600))

# WE WILL USE FIRING BOOLEAN TO KEEP TRACK OF IF WE ARE FIRING MISSILES
firing = False
missile = pygame.Surface((10,50))

while True:

    d.fill((255,255,255))
    for event in pygame.event.get():
        if event.type  == pygame.QUIT:
            pygame.quit()

    # Assign initial positions only if it isnt firing
    if not firing:
        missilex = 600  # This is the initial position of the missile. This will be
        missiley = 500  # your player position in your game

    if pygame.key.get_pressed()[pygame.K_SPACE]: # if space pressed set firing to true
        firing = True

    # if firing is true,we want to display the missile and move it.
    if firing:
        d.blit(missile,(missilex,missiley))
        missiley -= 1

    # if missile is off the screen,set firing to false so it resets to initial position (600,500).
    if missiley < -20:
        firing = False

    pygame.display.update()
,

除非我不是您的全部代码,否则您的处理方式对我来说似乎不正确。但是,您使用相同的变量来表示子弹和飞船的x和y坐标。您想让子弹从太空飞船独立移动。因此,如果您在代码中更改变量y,也会移动空间,并且与x变量相同。

您需要做的是创建2个单独的类。一个给你子弹,另一个给太空船。这是其实现的简单示例,以后可以通过添加所需的任何功能来加以改进。

class Spaceship:
    image = pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\spaceship.png').convert()

    def __init__(self,x,y,vel):
        self.x = x
        self.y = y
        self.vel = vel

    def draw(self,screen):
        screen.blit(Spaceship.image,(self.x,self.y))

class Bullet:
    image = pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\bullet2.png').convert()
    bullet_list = []  # holds the list of bullets in the screen

    def __init__(self,vel):
        self.x = x
        self.y = y
        self.vel = vel
        Bullet.bullet_list.append(self)

    def move(self):
        self.y -= self.vel
        if self.y < 0:  # remove bullet if it goes beyond screen
            Bullet.bullet_list.remove(self)

    def draw(self,screen):
        screen.blit(Bullet.image,self.y))

spaceship = Spaceship(130,480,30)

# set up screen and bg as before
while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            pygame.display.quit()
            sys.exit(0)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and x < 288:
        spaceship.x += spaceship.vel
    if keys[pygame.K_LEFT] and x > 0:
        spaceship.x -= spaceship.vel
    if keys[pygame.K_SPACE]:  # create a bullet if space is pressed
        Bullet(spaceship.x,spaceship.y,10)

    screen.blit(bg,0))
    spaceship.draw()
    for bullet in Bullet.bullet_list():
        bullet.move()  # actually moves the bullet on each iteration of the loop
        bullet.draw()
    pygame.display.update()
    clock.tick(120)

如果要创建游戏,请注意将来的重要事项,使所有内容都面向对象。相信我,它将使您的生活更加轻松。让我知道您是否有任何问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。