Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game progress crashs #88

Open
QuBuZhou opened this issue Dec 21, 2024 · 1 comment
Open

Game progress crashs #88

QuBuZhou opened this issue Dec 21, 2024 · 1 comment

Comments

@QuBuZhou
Copy link

Hi, I want to make a project which called 'Your first 2d game' in the godot api documentation with py4godot instead of gdscript. When I try to press the button to make sprite move, the game window crashs. here is the code:
`python
from py4godot.classes.AnimatedSprite2D.AnimatedSprite2D import AnimatedSprite2D
from py4godot.classes.Input.Input import Input
from py4godot.classes.Node2D.Node2D import Node2D

from py4godot.methods import private
from py4godot.signals import signal, SignalArg
from py4godot.classes import gdclass
from py4godot.classes.core import Vector3, Vector2, StringName, NodePath
from py4godot.classes.Area2D.Area2D import Area2D

@gdclass
class Player(Area2D):
node_path: NodePath
speed: int = 400
str_up: StringName
str_down: StringName
str_left: StringName
str_right: StringName

def _ready(self) -> None:
	self.screen_size: Vector2 = self.get_viewport_rect().size
	self.velocity: Vector2 = Vector2.new3(0, 0)
	self.sprite = AnimatedSprite2D.cast(self.get_node(self.node_path))

# put initialization code here
def _process(self, delta: float) -> None:
	if Input.get_instance().is_action_pressed(self.str_right):
		self.velocity.x += 1
	if Input.get_instance().is_action_pressed(self.str_left):
		self.velocity.x -= 1
	if Input.get_instance().is_action_pressed(self.str_up):
		self.velocity.y += 1
	if Input.get_instance().is_action_pressed(self.str_down):
		self.velocity.y -= 1

	if self.velocity.length() > 0:
		self.velocity = self.velocity.normalized() * self.speed

		self.sprite.play()
	else:
		self.sprite.stop()

	self.position += self.velocity * delta
	self.position.clamp(Vector2.new3(0, 0), self.screen_size)

	if self.velocity.x != 0:
		self.sprite.animation = "walk"
		self.sprite.flip_v = False
		self.sprite.flip_h = self.velocity.x < 0
	elif self.velocity.y != 0:
		self.sprite.animation = "up"
		self.sprite.flip_v = self.velocity.y > 0

`
Is there anything wrong in the code? if yes, please point out; if not, please fix the bug.

@QuBuZhou
Copy link
Author

So after a few time I finish a small project, but there's no mistake, perhaps I gone wrong on the other hand?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant