无法在屏幕上移动精灵
Cant move sprite on screen
精灵只是卡在左上角。按“wasd”时它会轻微移动。
我正在尝试制作 Mayhem/xPilot 的简单克隆。 2 人游戏,其中有 2 枚火箭可以互相击落。
我对编码还很陌生,python,如果你能 ELI5,我将不胜感激。
还有其他人遇到过这个问题吗?
import pygame as pg
import sys
x, y = 0, 0
class Rocket(pg.sprite.Sprite):
def __init__(self, picture_path, x, y):
super().__init__()
self.image = pg.image.load(picture_path)
self.image = pg.transform.scale(self.image, (120, 100))
self.rect = self.image.get_rect()
self.rect.x = 900
self.rect.y = 400
self.pressed_w = False
self.pressed_a = False
self.pressed_s = False
self.pressed_d = False
self.speed = 3
self.gravity = False
def update(self):
self.rect.x = 0
self.rect.y = 0
if self.pressed_a and self.rect.left > 0:
self.rect.x = self.speed
if self.pressed_d and self.rect.right < width:
self.rect.x = self.speed
if self.pressed_s and self.rect.bottom < height:
self.rect.y = self.speed
if self.pressed_w and self.rect.top > 0:
self.rect.y = -self.speed
if self.gravity and self.rect.bottom < height:
self.rect.y = self.speed
self.rect.x += x
self.rect.y += y
pg.init()
clock = pg.time.Clock()
width = 1920
height = 1080
screen = pg.display.set_mode((width, height))
background = pg.image.load("bg.jpg")
#rocket
player_rect = Rocket("rocket.png", x, y)
rocket_group = pg.sprite.Group()
rocket_group.add(player_rect)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_w:
player_rect.pressed_w = True
player_rect.gravity = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_s:
player_rect.pressed_s = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player_rect.pressed_d = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
player_rect.pressed_a = True
if event.type == pg.KEYUP:
if event.key == pg.K_w:
player_rect.pressed_w = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_s:
player_rect.pressed_s = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_d:
player_rect.pressed_d = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_a:
player_rect.pressed_a = False
player_rect.gravity = True
pg.display.flip()
screen.blit(background, (0, 0))
rocket_group.draw(screen)
rocket_group.update()
clock.tick(120)
问题出在 update
方法中。该方法必须更改 rect
属性的坐标。但是,您的方法连续设置 self.rect.x = 0
和 self.rect.y = 0
.
更改 update
:
class Rocket(pg.sprite.Sprite):
# [...]
def update(self):
if self.pressed_a:
self.rect.x -= self.speed
if self.pressed_d
self.rect.x += self.speed
if self.pressed_w
self.rect.y -= self.speed
if self.pressed_s
self.rect.y += self.speed
if self.gravity
self.rect.y += self.speed
if self.rect.left < 0: self.rect.left = 0
if self.rect.right > width: self.rect.right = width
if self.rect.top < 0: self.rect.top = 0
if self.rect.bottom > height: self.rect.bottom = height
精灵只是卡在左上角。按“wasd”时它会轻微移动。 我正在尝试制作 Mayhem/xPilot 的简单克隆。 2 人游戏,其中有 2 枚火箭可以互相击落。
我对编码还很陌生,python,如果你能 ELI5,我将不胜感激。 还有其他人遇到过这个问题吗?
import pygame as pg
import sys
x, y = 0, 0
class Rocket(pg.sprite.Sprite):
def __init__(self, picture_path, x, y):
super().__init__()
self.image = pg.image.load(picture_path)
self.image = pg.transform.scale(self.image, (120, 100))
self.rect = self.image.get_rect()
self.rect.x = 900
self.rect.y = 400
self.pressed_w = False
self.pressed_a = False
self.pressed_s = False
self.pressed_d = False
self.speed = 3
self.gravity = False
def update(self):
self.rect.x = 0
self.rect.y = 0
if self.pressed_a and self.rect.left > 0:
self.rect.x = self.speed
if self.pressed_d and self.rect.right < width:
self.rect.x = self.speed
if self.pressed_s and self.rect.bottom < height:
self.rect.y = self.speed
if self.pressed_w and self.rect.top > 0:
self.rect.y = -self.speed
if self.gravity and self.rect.bottom < height:
self.rect.y = self.speed
self.rect.x += x
self.rect.y += y
pg.init()
clock = pg.time.Clock()
width = 1920
height = 1080
screen = pg.display.set_mode((width, height))
background = pg.image.load("bg.jpg")
#rocket
player_rect = Rocket("rocket.png", x, y)
rocket_group = pg.sprite.Group()
rocket_group.add(player_rect)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_w:
player_rect.pressed_w = True
player_rect.gravity = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_s:
player_rect.pressed_s = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player_rect.pressed_d = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
player_rect.pressed_a = True
if event.type == pg.KEYUP:
if event.key == pg.K_w:
player_rect.pressed_w = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_s:
player_rect.pressed_s = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_d:
player_rect.pressed_d = False
player_rect.gravity = True
if event.type == pg.KEYUP:
if event.key == pg.K_a:
player_rect.pressed_a = False
player_rect.gravity = True
pg.display.flip()
screen.blit(background, (0, 0))
rocket_group.draw(screen)
rocket_group.update()
clock.tick(120)
问题出在 update
方法中。该方法必须更改 rect
属性的坐标。但是,您的方法连续设置 self.rect.x = 0
和 self.rect.y = 0
.
更改 update
:
class Rocket(pg.sprite.Sprite):
# [...]
def update(self):
if self.pressed_a:
self.rect.x -= self.speed
if self.pressed_d
self.rect.x += self.speed
if self.pressed_w
self.rect.y -= self.speed
if self.pressed_s
self.rect.y += self.speed
if self.gravity
self.rect.y += self.speed
if self.rect.left < 0: self.rect.left = 0
if self.rect.right > width: self.rect.right = width
if self.rect.top < 0: self.rect.top = 0
if self.rect.bottom > height: self.rect.bottom = height