oop pygame player_x变量更改,但在显示中没有发生任何变化
嗨,整个编程非常陌生,所以这可能是一个非常愚蠢的问题。目前正在尝试通过制作游戏来了解有关OOP的更多信息。
虽然player_x的价值正在变化,但由于某些原因,它没有像我预期的那样移动。
main.py
import pygame, settings, obstacles, npc, player
from utils import *
pygame.init()
window = pygame.display.set_mode((settings.WIDTH, settings.HEIGHT))
FPS = pygame.time.Clock()
player = player.Player(window)
Loop
run = True
while run:
FPS.tick(settings.FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
settings.PLAYER_X -= 5
print(settings.PLAYER_X)
pygame.display.update()
player.py
import pygame, settings
pygame.init()
class Player():
def __init__(self, window) -> None:
pygame.draw.rect(window, settings.OLIVE, (settings.PLAYER_X, settings.PLAYER_Y, settings.PLAYER_WIDTH, settings.PLAYER_HEIGHT)) # (x, y, width, height)
def player_control(self):
pass
settings.py
WIDTH = 1440
HEIGHT = 720
FPS = 60
PLAYER_X = 100
PLAYER_Y = 200
PLAYER_WIDTH = 50
PLAYER_HEIGHT = 50
OLIVE = (136, 196, 23)
Hi am pretty new to programming in general so this might be a pretty dumb problem. Am currently trying to learn more about OOP by making a game.
while the value of player_x is changing for some reasons its not moving as I expected.
main.py
import pygame, settings, obstacles, npc, player
from utils import *
pygame.init()
window = pygame.display.set_mode((settings.WIDTH, settings.HEIGHT))
FPS = pygame.time.Clock()
player = player.Player(window)
Loop
run = True
while run:
FPS.tick(settings.FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
settings.PLAYER_X -= 5
print(settings.PLAYER_X)
pygame.display.update()
player.py
import pygame, settings
pygame.init()
class Player():
def __init__(self, window) -> None:
pygame.draw.rect(window, settings.OLIVE, (settings.PLAYER_X, settings.PLAYER_Y, settings.PLAYER_WIDTH, settings.PLAYER_HEIGHT)) # (x, y, width, height)
def player_control(self):
pass
settings.py
WIDTH = 1440
HEIGHT = 720
FPS = 60
PLAYER_X = 100
PLAYER_Y = 200
PLAYER_WIDTH = 50
PLAYER_HEIGHT = 50
OLIVE = (136, 196, 23)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要移动
player
,您需要:player
,player
,使用更新的player_x
,您需要在代码中进行一些修改:
创建一个函数以在
player.py.py.py
中渲染并在循环中调用,而不是渲染player
在__ INIT __
中,只能称为一次。参考
settings.player_x
在__ INT __ INT __
inplayer.py.py
中仅一次,然后更新player 按下键时对象。
在每个循环的末尾,渲染
player
对象。player.py
main.py
To move your
player
, you need to:player
in every while-loopplayer
with the updatedPLAYER_X
And to achieve it, you need a few modification in your code:
Create a function to render in your
player.py
and call it in the loop instead of render aplayer
in__init__
, which will only be called once.Reference
settings.PLAYER_X
only once in the__init__
inplayer.py
and update the variable of theplayer
object when key are pressed.At the end of every loop, render the
player
object.player.py
main.py