Python 如何使对象不断向上移动

Python 如何使对象不断向上移动,python,pygame,Python,Pygame,我正在用Python做一个游戏。我在这个游戏中有一艘宇宙飞船。宇宙飞船可以向左和向右移动,它应该从x位置发射子弹,并且应该发射子弹。我可以移动宇宙飞船,我可以把它放在屏幕上。问题是我无法从宇宙飞船的x位置发射子弹,也无法使子弹向上移动。有人能帮我吗?请张贴简单的代码,如果可能的话 代码如下: import pygame import sys pygame.init() screen_length = 512 screen_width = 288 clock = pygame.time.Clock

我正在用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)
导入pygame
导入系统
pygame.init()
屏幕长度=512
屏幕宽度=288
clock=pygame.time.clock()
screen=pygame.display.set_模式((屏幕宽度、屏幕长度))
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()
导弹=pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\bullet2.png').convert()
x=130
y=480
z=460
速度=30
spaceship_rect=spaceship.get_rect(中心=(x,y))
太空船位置=太空船右左+170
子弹头速度=10
导弹发射=导弹发射。获得发射(中心=(圆形(宇宙飞船发射位置//2),z))
宇宙飞船_方向=10
尽管如此:
对于pygame.event.get()中的事件:
如果event==pygame.QUIT:
pygame.display.quit()
系统出口(0)
keys=pygame.key.get_pressed()
如果键[pygame.K_RIGHT]和x<288:
x+=速度
如果键[pygame.K_LEFT]和x>0:
x-=速度
如果键[pygame.K_SPACE]:
背景布利特(导弹,导弹直瞄)
屏幕填充(0)
屏幕光点(背景,(0,0))
屏幕。blit(宇宙飞船,(x,y))
pygame.display.update()
时钟滴答(120)

我觉得你的做法不对,除非ofc不是你的全部准则。但你用的是同一个变量来表示子弹和宇宙飞船的x和y坐标。你想让子弹独立于太空船移动。因此,如果你在代码中更改变量y,这也会使空间飞船向上移动,x变量也是如此

您需要做的是创建两个独立的类。一个给你子弹,另一个给宇宙飞船。下面是一个简单的实现示例,您可以通过添加以后需要的任何特性来改进它

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, x, y, 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.x, 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, 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)
级宇宙飞船:
image=pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\spaceship.png').convert()
定义初始值(self,x,y,vel):
self.x=x
self.y=y
self.vel=vel
def提取(自身,屏幕):
屏幕.blit(Spaceship.image,(self.x,self.y))
类别项目符号:
image=pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\bullet2.png').convert()
bullet_list=[]保存屏幕中的项目符号列表
定义初始值(self,x,y,vel):
self.x=x
self.y=y
self.vel=vel
Bullet.Bullet\u list.append(self)
def移动(自我):
self.y-=self.vel
如果self.y<0:#如果项目符号超出屏幕,则将其移除
Bullet.Bullet\u列表。删除(自身)
def提取(自身,屏幕):
屏幕.blit(Bullet.image,(self.x,self.y))
宇宙飞船=宇宙飞船(130480,30)
#像以前一样设置屏幕和背景
尽管如此:
对于pygame.event.get()中的事件:
如果event==pygame.QUIT:
pygame.display.quit()
系统出口(0)
keys=pygame.key.get_pressed()
如果键[pygame.K_RIGHT]和x<288:
spaceship.x+=spaceship.vel
如果键[pygame.K_LEFT]和x>0:
spaceship.x-=spaceship.vel
if键[pygame.K_SPACE]:#如果按空格键,则创建一个项目符号
子弹(x号宇宙飞船,y号宇宙飞船,10号)
屏幕光点(背景,(0,0))
太空船
对于项目符号中的项目符号。项目符号列表():
bullet.move()#在循环的每次迭代中实际移动该项目符号
bullet.draw()
pygame.display.update()
时钟滴答(120)

未来的重要提示如果你想创建游戏,让一切都面向对象。相信我,这会让你的生活变得更加轻松。如果您有任何问题,请告诉我。

我认为您处理问题的方式不对,除非ofc不是您的全部代码。但你用的是同一个变量来表示子弹和宇宙飞船的x和y坐标。你想让子弹独立于太空船移动。因此,如果你在代码中更改变量y,这也会使空间飞船向上移动,x变量也是如此

您需要做的是创建两个独立的类。一个给你子弹,另一个给宇宙飞船。下面是一个简单的实现示例,您可以通过添加以后需要的任何特性来改进它

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, x, y, 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.x, 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, 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)
级宇宙飞船:
image=pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\spaceship.png').convert()
定义初始值(self,x,y,vel):
self.x=x
self.y=y
self.vel=vel
def提取(自身,屏幕):
屏幕.blit(Spaceship.image,(self.x,self.y))
类别项目符号:
image=pygame.image.load(r'C:\Users\Soorya\Anonymous\space game folder\bullet2.png').convert()
bullet_list=[]保存屏幕中的项目符号列表
定义初始值(self,x,y,vel):
self.x=x
self.y=y
self.vel=vel
Bullet.Bullet\u list.append(self)
def移动(自我):
self.y-=self.vel
如果self.y<0:#如果项目符号超出屏幕,则将其移除
Bullet.Bullet\u列表。删除(自身)
def提取(自身,屏幕):
屏幕.blit(Bullet.image,(self.x,self.y))
宇宙飞船=宇宙飞船(130480,30)
#像以前一样设置屏幕和背景
尽管如此:
对于pygame.event.get()中的事件:
如果event==pygame.QUIT:
pygame.display.quit()
系统出口(0)
keys=pygame.key.get_pressed()
如果键[pygame.K_RIGHT]和x<288:
s
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)
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()