Python Pygame-外星人入侵
我在做外星人入侵游戏,我的子弹只是出现和消失。我检查了我的代码很多次,我不知道为什么它没有在屏幕上移动。谁能帮帮我吗。我不想尝试通过查看代码中的缺陷来学习的另一种方法,这样我就不会再次重复同样的错误Python Pygame-外星人入侵,python,pygame,Python,Pygame,我在做外星人入侵游戏,我的子弹只是出现和消失。我检查了我的代码很多次,我不知道为什么它没有在屏幕上移动。谁能帮帮我吗。我不想尝试通过查看代码中的缺陷来学习的另一种方法,这样我就不会再次重复同样的错误 import sys import pygame pygame.init() # Setting up a window screen = pygame.display.set_mode((1200, 800)) screen_rect = screen.get_rect() # Captio
import sys
import pygame
pygame.init()
# Setting up a window
screen = pygame.display.set_mode((1200, 800))
screen_rect = screen.get_rect()
# Caption
pygame.display.set_caption("space shooter".title())
# Setting up the icon
icon = pygame.image.load("undertake.png").convert_alpha()
pygame.display.set_icon(icon)
# Identifying a Background
bg = pygame.image.load("bg.png").convert_alpha()
# Adding the jet
jet = pygame.image.load("jet.png").convert_alpha()
jet_rect = jet.get_rect()
jet_rect.centerx = screen_rect.centerx
jet_rect.bottom = screen_rect.bottom
# Adding bullets to the left of the jet
bullet = pygame.image.load("pixel_laser_red.png").convert_alpha()
bullet_rect = bullet.get_rect()
bullet_state = "ready"
# Moving the jet
def move_jet(x):
jet_rect.centerx += x
# Firing the bullet
def fire_bullet(x, y):
bullet_state = "fire"
screen.blit(bullet, (x, y))
# Adding Boundaries
def boundaries():
if jet_rect.left >= 1200:
jet_rect.right = 0
elif jet_rect.right <= 0:
jet_rect.left = 1200
# Game Loop
while True:
screen.blit(bg, (0, 0))
screen.blit(jet, jet_rect)
# EVENTS
for event in pygame.event.get():
# Quitting
if event.type == pygame.QUIT:
sys.exit()
# KeyStrokes
pressed = pygame.key.get_pressed()
jet_xincrement = 0
if pressed[pygame.K_RIGHT]:
jet_xincrement += 3
if pressed[pygame.K_LEFT]:
jet_xincrement -= 3
if pressed[pygame.K_SPACE]:
bullet_x = jet_rect.centerx
bullet_y = jet_rect.top
fire_bullet(bullet_x - 28, bullet_y + 7)
if bullet_state == "fire" :
bullet_y -= 10
boundaries()
move_jet(jet_xincrement)
pygame.display.flip()
导入系统
导入pygame
pygame.init()
#设置窗口
screen=pygame.display.set_模式((1200800))
screen\u rect=screen.get\u rect()
#标题
pygame.display.set_标题(“spaceshooter.title())
#设置图标
icon=pygame.image.load(“consude.png”).convert_alpha()
pygame.display.set_图标(图标)
#识别背景
bg=pygame.image.load(“bg.png”).convert_alpha()
#添加喷气式飞机
jet=pygame.image.load(“jet.png”).convert_alpha()
jet_rect=jet.get_rect()
jet_rect.centerx=屏幕_rect.centerx
jet_rect.bottom=屏幕_rect.bottom
#在飞机左侧添加子弹
bullet=pygame.image.load(“pixel\u laser\u red.png”).convert\u alpha()
bullet\u rect=bullet.get\u rect()
bullet_state=“就绪”
#移动喷气式飞机
def移动喷嘴(x):
jet_rect.centerx+=x
#开枪
def火焰弹(x,y):
bullet_state=“火灾”
屏幕光点(项目符号,(x,y))
#添加边界
定义边界():
如果jet_rect.left>=1200:
jet_rect.right=0
elif jet_rect.right事实上,当空格被按下时,子弹刚好被抽出。你必须在每一帧中连续地画出子弹 功能
fire_bullet
仅设置项目符号的状态和位置。变量位于全局命名空间中。因此,您必须使用来设置它们:
bullet\u state=“就绪”
bullet_x=0
bullet_y=0
#开枪
def火焰弹(x,y):
全球子弹头状态,子弹头x,子弹头y
bullet_state=“火灾”
子弹头x=x
子弹头y=y
当按下空格键时,将调用fire\u bullet
。参数是喷气机的当前位置。当bullet\u状态
为“fire”
时,必须在主应用程序循环中绘制项目符号:
为True时:
# [...]
如果按下[pygame.K_SPACE]:
发射子弹(jet-rect.centerx-28,jet-rect.top+7)
如果bullet_state==“fire”:
子弹头y-=10
屏幕光点(子弹,(子弹x,子弹y))
# [...]
非常感谢,伙计:)