我在pygame中制作了一个非常基本的游戏,其中唯一可行的动作是向左移动,向右移动并向上射击子弹。我的问题是,当我开枪时,我的玩家精灵在子弹向上移动时保持不动。我该如何解决这个问题?我对pygame很新,所以我会感激任何帮助。
#importing needed libraries
import pygame,sys
from pygame.locals import *
#Class for the Player
class Player():
def __init__(self,surf,xpos,ypos):
self.image=pygame.image.load("cat1.png").convert_alpha()
self.x=xpos
self.y=ypos
self.surface=surf
def keys(self):
dist=10
key=pygame.key.get_pressed()
if key[pygame.K_RIGHT]:
if self.x<500:
self.x+=dist
elif key[pygame.K_LEFT]:
if self.x!=0:
self.x-=dist
def draw(self,surface):
self.surface.blit(self.image,(self.x,self.y))
#Class for the bullet which inherits the Player Class
class Weapon(Player):
def __init__(self,surf,xpos,ypos,bg,wxpos,wypos):
Player.__init__(self,surf,xpos,ypos)
self.wimage=pygame.image.load("bullet.png").convert_alpha()
self.wx=wxpos
self.wy=wypos
self.background=bg
def Shoot(self):
dist=10
while self.wy>0:
self.surface.blit(self.background,(0,0))
self.surface.blit(self.image,(self.x,self.y))
self.surface.blit(self.wimage,(self.wx,self.wy))
self.wy-=dist
pygame.display.update()
#initialising pygame
pygame.init()
FPS=30
fpsClock=pygame.time.Clock()
#creating display window
DISPLAYSURF=pygame.display.set_mode((577,472),0,32)
pygame.display.set_caption('Animation')
WHITE = (255, 255, 255)
background=pygame.image.load("background.png")
DISPLAYSURF.fill(WHITE)
#creating player
player=Player(DISPLAYSURF,50,360)
#main game loop
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONDOWN:
weapon=Weapon(DISPLAYSURF,player.x,player.y,background,player.x+25,player.y)
player.draw(DISPLAYSURF)
weapon.Shoot()
player.keys()
DISPLAYSURF.blit(background,(0,0))
player.draw(DISPLAYSURF)
pygame.display.update()
fpsClock.tick(FPS)
答案 0 :(得分:0)
Shoot
必须与类似于draw()
和update()
的播放器一样,只移动子弹self.dist
像素。如果你在mainloop中调用它,那么你可以移动子弹,你可以移动玩家。
这是一个简单的例子。详细了解pygame.sprite.Sprite(),pygame.sprite.Group()和pygame.Rect()
请参阅Program Arcade Games With Python And Pygame
import pygame
import sys
# --- constants ----
FPS = 30
WHITE = (255, 255, 255)
# --- classes ---
class Player():
def __init__(self, surface, x, y):
self.surface = surface
self.image = pygame.image.load("cat1.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dist = 10
def keys(self):
key = pygame.key.get_pressed()
if key[pygame.K_RIGHT]:
if self.rect.x < 500:
self.rect.x += self.dist
elif key[pygame.K_LEFT]:
if self.rect.x >= 0:
self.rect.x -= self.dist
def draw(self):
self.surface.blit(self.image, self.rect)
class Bullet():
def __init__(self, surface, x, y):
self.surface = surface
self.image = pygame.image.load("bullet.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dist = 10
self.dead = False
def draw(self):
self.surface.blit(self.image, self.rect)
def update(self):
if self.rect.y > 0:
self.rect.y -= self.dist
else:
self.dead = True
# --- main ---
pygame.init()
display = pygame.display.set_mode((577,472),0,32)
pygame.display.set_caption('Animation')
background = pygame.image.load("background.png")
player = Player(display, 50, 360)
fps_clock = pygame.time.Clock()
bullets = []
while True:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
b = Bullet(display, player.rect.x, player.rect.y)
bullets.append(b)
# --- updates ---
player.keys()
# moves bullets
for b in bullets:
b.update()
# remove "dead" bullets
bullets = [b for b in bullets if not b.dead]
# --- draws ---
display.blit(background, (0,0))
player.draw()
for b in bullets:
b.draw()
pygame.display.update()
# --- FPS ---
fps_clock.tick(FPS)