如何使您的角色移动而无需反复点击按钮? [英] How to make your character move without tapping repeatedly on buttons?

查看:79
本文介绍了如何使您的角色移动而无需反复点击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究赛车游戏.我一直在使用此代码,但我的播放器仅移动一次.我该如何解决这个问题?

I'm working on a racing game. I have been using this code but my player keeps moving once only. How do I solve this problem?

change = 7
dist = 7
change_r = 0
change_l = 0
dist_u = 0
dist_d = 0
pygame.display.update()  


for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
           change_r = True

        elif event.key == pygame.K_LEFT:
            change_l = True


        elif event.key == pygame.K_UP:
            dist_u = True     

        elif event.key == pygame.K_DOWN:
            dist_d = True



    if event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            change_r = False
            change_l = False
            dist_u = False
            dist_d = False
    clock.tick(60)     

    if change_r == True:
        x += change
    if change_l == True:
        x -= change
    if dist_u == True:
        y -= dist
    if dist_d == True:
        y += dist

推荐答案

使用Pygame制作可移动形状/精灵的方法有多种.您在问题中发布的代码似乎过于复杂.我将为您提供两种更简单易用的不同方式.使用精灵类,并使用函数.

There are different ways you could go about making move-able shapes/sprites using Pygame. The code you posted in your question seems overly complex. I'll give you two different ways that are simpler, and easier to use. Using a sprite class, and using a function.

似乎我没有在回答您的问题,但是如果我只是告诉您所发布代码中要解决的问题,那将是故意引导您走上一条会导致很多问题的道路未来的复杂性.

It may not seem like I'm answering your question, but if I just told you what to fix in your code you posted, I'd just be knowingly steering you down a road that will lead to many problems, and much complexity in the future.

要制作赛车Sprite,可以使用下面的Sprite类.

To make a your race car sprite, you could use a sprite class like the one below.

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.x = WIDTH / 2
        self.rect.y = HEIGHT / 2
        self.vx = 0
        self.vy = 0

    def update(self):
        self.vx = 0
        self.vy = 0
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            self.vx = -5
        elif key[pygame.K_RIGHT]:
            self.vx = 5
        if key[pygame.K_UP]:
            self.vy = -5
        elif key[pygame.K_DOWN]:
            self.vy = 5
        self.rect.x += self.vx
        self.rect.y += self.vy

由于您的课程是从Pygame的sprite类继承的,因此您必须为图像命名 self.image ,并且必须为图像命名矩形 self.rect.您还可以看到,该类有两个主要方法.一种用于创建sprite(__init__),另一种用于更新sprite(update)

Since your class is inheriting from Pygame's sprite class, You must name your image self.image and you must name your rectangle for the image self.rect. As you can also see, the class has two main methods. One for creating the sprite(__init__) and one for updating the sprite(update)

要使用您的课程,请创建一个Pygame精灵组来容纳您所有的精灵,然后将玩家对象添加到该组中:

To use your class, make a Pygame sprite group to hold all your sprites, and then add your player object to the group:

sprites = pygame.sprite.Group()
player = Player()
sprtites.add(player)

并在游戏循环中实际将您的精灵渲染到屏幕调用sprites.update()sprites.draw()上,您可以在其中更新屏幕:

And to actual render your sprites to the screen call sprites.update() and sprites.draw() in your game loop, where you update the screen:

sprites.update()
window_name.fill((200, 200, 200))
sprites.draw(window_name)
pygame.display.flip()

我强烈建议使用sprite类的原因是,它将使您的代码看起来更加简洁,并且易于维护.您甚至可以将每个Sprite类移动到它们自己的单独文件中.

The reason i highly recommended using sprite classes, is that it will make your code look much cleaner, and be much easier to maintain. You could even move each sprite class to their own separate file.

但是,在完全采用上述方法之前,您应该先阅读 pygame.Rect 对象和 pygame.sprite 对象,因为您将使用它们.

Before diving fully into the method above however, you should read up on pygame.Rect objects and pygame.sprite objects, as you'll be using them.

如果您不想进入Sprite类,则可以使用类似于以下功能的游戏实体来创建游戏实体.

If you prefer not to get into sprite classes, you can create your game entities using a function similar to the one below.

def create_car(surface, x, y, w, h, color):
    rect = pygame.Rect(x, y, w, h)
    pygame.draw.rect(surface, color, rect)
    return rect

如果您仍然想使用精灵,但不想创建一个类,只需稍微修改上面的函数即可:

If you would still like to use a sprites, but don't want to make a class just modify the function above slightly:

def create_car(surface, x, y, color, path_to_img):
    img = pygame.image.load(path_to_img)
    rect = img.get_rect()
    surface.blit(img, (x, y))

这里是一个示例,说明我将如何使用上述功能制作可移动的矩形/精灵:

Here is an example of how i would use the functions above to make a movable rectangle/sprite:

import pygame

WIDTH = 640
HEIGHT = 480
display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Moving Player Test")
clock = pygame.time.Clock()
FPS = 60

def create_car(surface, x, y, w, h, color):
    rect = pygame.Rect(x, y, w, h)
    pygame.draw.rect(surface, color, rect)
    return rect

running = True
vx = 0
vy = 0
player_x = WIDTH / 2 # middle of screen width
player_y = HEIGHT / 2 # middle of screen height
player_speed = 5
while running:
    clock.tick(FPS)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
            pygame.quit()
            quit()
        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_LEFT:
                vx = -player_speed
            elif e.key == pygame.K_RIGHT:
                vx = player_speed
            if e.key == pygame.K_UP:
                vy = -player_speed
            elif e.key == pygame.K_DOWN:
                vy = player_speed
        if e.type == pygame.KEYUP:
            if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT or\
               e.key == pygame.K_UP or e.key == pygame.K_DOWN:
                vx = 0
                vy = 0

    player_x += vx
    player_y += vy
    display.fill((200, 200, 200))
    ####make the player#####
    player = create_car(display, player_x, player_y, 10, 10, (255, 0, 0))
    pygame.display.flip()

正如您可能在上面看到的那样,可以简化用于移动赛车的代码.

As you may see above, your code for moving your race car can be simplified.

我应该注意,我在上面概述的每种方法中都假设了一些事情.

I should note, I'm assuming a few things with each method outlined above.

  1. 表示您使用圆形,正方形或某种类型的pygame形状对象.

  1. That your either using a circle, square, or some type of pygame shape object.

或者您使用精灵.

如果您当前未使用上述任何方法,建议您这样做.一旦开始构建更大,更复杂的游戏,这样做将使您的代码更易于维护.

If your not currently using any of the methods outlined above, I suggest that you do. Doing so will make your code much eaiser to maintain once you begin to build larger and more complex games.

这篇关于如何使您的角色移动而无需反复点击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆