我是编程和Python以及Pygame的新手。因此,我还不熟悉Pygame中的精灵。我试图制作一个游戏,只要按下空格键就会跳块 - 类似于马里奥。
我的代码没有按照需要工作,因为每当按下空格键时,块会逐渐向上移动(我添加了一个重力组件),而不是"跳跃"。
import pygame
pygame.init()
game_display = pygame.display.set_mode((800, 800))
# fixed variables at the start
x_pos = 400
y_pos = 400
current_speed = 15
def jump_coords(y_position, speed):
if speed >= 0:
#to move up, reduce the y-coordinate
y_position -= speed
return y_position
game_exit = False
# main loop
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
y_pos = jump_coords(y_pos, current_speed)
# 1 represents gravity value
current_speed -= 1
rect_one = pygame.Rect(x_pos, y_pos, 10, 10)
pygame.draw.rect(game_display, (255, 0, 0), rect_one)
pygame.display.update()
我知道我必须以某种方式让y_pos在speed >= 0
的while循环中不断更新,但我不确定如何实现它。
答案 0 :(得分:1)
我对您的代码进行了最小的更改,以使块反弹:
import pygame
pygame.init()
game_display = pygame.display.set_mode((800, 800))
# fixed variables at the start
x_pos = 400
y_pos = 400
x_old = x_pos
y_old = y_pos
current_speed = 15
def jump_coords(y_position, speed):
# to move up, reduce the y-coordinate
y_position -= speed
if y_position > 400:
y_position = 400
global jump_flag
jump_flag = False
global current_speed
current_speed = 15
return y_position
game_exit = False
jump_flag = False
# main loop
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
jump_flag = True
elif event.key == pygame.K_ESCAPE:
exit(0)
if jump_flag:
x_old = x_pos
y_old = y_pos
y_pos = jump_coords(y_pos, current_speed)
# 1 represents gravity value
current_speed -= 1
rect_old = pygame.Rect(x_old, y_old, 10, 10)
pygame.draw.rect(game_display, (0, 0, 0), rect_old)
rect_one = pygame.Rect(x_pos, y_pos, 10, 10)
pygame.draw.rect(game_display, (255, 0, 0), rect_one)
pygame.display.update()
最重要的变化是取消检查速度大于零。如果该块将要回落,速度必须变为负值。下一个更改是保存旧的x和y坐标,以便我们可以在旧位置上绘制一个黑色方块。我还可以通过按Escape键退出程序。
答案 1 :(得分:0)
我是从零开始做的,我希望这不是太令人生畏!
namespace CreateTask
{
public ref class AsyncTask sealed : INotifyPropertyChanged
{
public:
AsyncTask();
void InitTask();
void ShowN();
IAsyncOperation<float64>^ GetPrimesAsync(float64 x, float64 y);
private:
float64 _n;
bool _isPropertyChangedObserved;
event PropertyChangedEventHandler^ _privatePropertyChanged;
protected:
void OnPropertyChanged(Platform::String^ propertyName)
{
if (_isPropertyChangedObserved)
{
PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
}
}
public:
property float64 N
{
float64 get()
{
return _n;
}
void set(float64 value)
{
_n = value;
OnPropertyChanged("N");
}
}
virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
{
virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e)
{
_isPropertyChangedObserved = true;
return _privatePropertyChanged += e;
}
virtual void remove(Windows::Foundation::EventRegistrationToken t)
{
_privatePropertyChanged -= t;
}
protected:
virtual void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
{
if (_isPropertyChangedObserved)
{
_privatePropertyChanged(sender, e);
}
}
}
};
}
this code is AsyncTask.Cpp
AsyncTask::AsyncTask()
{
InitTask();
}
void AsyncTask::InitTask()
{
create_task(GetPrimesAsync(111.1, 222.2)).then(
[this](float64 z)
{
N = z;
});
}
IAsyncOperation<float64>^ AsyncTask::GetPrimesAsync(float64 x, float64 y)
{
return create_async([this, x, y]() -> float64
{
// Ensure that the input values are in range.
float64 z;
z = x + y;
return z;
});
}
void AsyncTask::ShowN()
{
MessageDialog^ msg = ref new MessageDialog(N.ToString());
msg->ShowAsync();
}
this MainPage.xaml.h code
namespace CreateTask
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public ref class MainPage sealed
{
public:
MainPage();
private:
AsyncTask^ asyncTask;
private:
void Click_Button(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
};
}
this MainPage.xaml.cpp code
MainPage::MainPage()
{
InitializeComponent();
asyncTask = ref new AsyncTask();
MessageDialog^ msg = ref new MessageDialog(asyncTask->N.ToString());
msg->ShowAsync();
}
void CreateTask::MainPage::Click_Button(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
MessageDialog^ msg = ref new MessageDialog(asyncTask->N.ToString());
msg->ShowAsync();
}
希望它有所帮助!