x在pygame中子弹的位置

pgky5nke  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(346)

这个问题在这里已经有答案了

我怎么能用空格键射子弹(1个答案)
如何一次阻止多发子弹(1个答案)
pygame:太空入侵者射击问题[重复](1个答案)
14小时前关门了。
代码:

import pygame
import sys
from pygame.locals import *

count = 0
moving = False

def blast1():
    blast.y -= bl

def player_animation():
    global player_speed
    player.x = player_speed

pygame.init()
running = True

clock = pygame.time.Clock()

bl = 1
player_speed = 1

player = pygame.Rect(250, 450, 50, 50)
blast = pygame.Rect(player.x, player.y , 10, 10)

screen = pygame.display.set_mode((500, 500))
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player_animation()
    blast1()

    screen.fill((255, 255, 255)) #color
    pygame.draw.ellipse(screen, [0, 0, 255], player)
    pygame.draw.ellipse(screen, [0, 255, 0], blast)

    keys=pygame.key.get_pressed()
    if keys[K_LEFT] and player.x !=  -4 :
        player_speed -= 5

    if keys[K_RIGHT] and player.x !=  451:
        player_speed += 5
    pygame.display.flip()

    clock.tick(30)
blast.x = player.x
pygame.quit()

我的目标很简单。我想创造一个你可以左右移动的球。然后我要创建一个从“玩家”的位置发射的子弹。我有点麻烦,我不知道为什么,但当我宣布子弹的位置 blast = pygame.Rect(player.x, player.y , 10, 10) 只有y位置起作用。不知何故,x的位置是在屏幕的中间,而不是在播放器上。我怎样才能解决这个问题?

ctehm74n

ctehm74n1#

线路

blast = pygame.Rect(player.x, player.y , 10, 10)

分配 Rectblast ,此时玩家的位置。此爆炸将保持静止,直到您编辑其位置。你只会改变 y 此处位置:

blast.y -= bl

但你不更新 x 发射子弹时的位置。你可以这样做:

def launch_blast(): # call this function when you create the bullet.
                    # At this point in the program, just call it at the beginning.
    blast.x = player.x + player.width / 2 - blast.width / 2

def blast1():
    blast.y -= bl

要启动多个项目符号,可以使用以下代码:

import pygame
from pygame.locals import *
pygame.init()

class Bullet:
    def __init__(self):
        self.rect = Rect(player.rect.x - 5 + player.rect.width / 2, player.rect.y , 10, 10)

    def move(self):
        self.rect.y -= 10 # move up
        pygame.draw.ellipse(screen, [0, 255, 0], self.rect) # draw on screen

class Player:
    def __init__(self):
        self.rect = pygame.Rect(250, 450, 50, 50)

    def move(self):
        # move
        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            self.rect.x -= 5
        if keys[K_RIGHT]:
            self.rect.x += 5

        # don't go out of the screen
        self.rect.x = min(max(self.rect.x, 0), 500 - self.rect.width)

        # draw on screen
        pygame.draw.ellipse(screen, [0, 0, 255], self.rect)

player = Player()
bullets = []

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        if event.type == KEYDOWN and event.key == K_SPACE:
            bullets.append(Bullet()) # generate a new bullet when space pressed

    screen.fill((255, 255, 255)) # white

    for bullet in bullets: # draw each bullet generated
        bullet.move()
        if bullet.rect.x < bullet.rect.height: # if out of the screen,
            bullets.remove(bullet) # then remove it
    player.move() # draw the player

    pygame.display.flip()
    clock.tick(30)

相关问题