首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以鼠标为增量移动玩家:pygame

以鼠标为增量移动玩家:pygame
EN

Stack Overflow用户
提问于 2019-03-21 04:42:53
回答 2查看 0关注 0票数 0

我使用tkinter而不是pygame,但我正在做一个pygame项目,我需要一些帮助让玩家以增量移动鼠标而不是传送到那里。我知道我可以用矢量来做,但我需要一些帮助。我目前的代码:

代码语言:javascript
复制
import pygame
import random
import sys
import time
import math
import os
WHITE    = (255, 255, 255)
DARKGRAY = ( 70,  70,  70)
BLACK    = (  0,   0,   0)
RED      = (255,   0,   0)
GREEN    = (  0, 255,   0)
BLUE     = (  0,   0, 255)
YELLOW   = (255, 255,   0)
ORANGE   = (255, 128,   0)eeeeeee
PURPLE   = (255,   0, 255)
LIGHT_ORANGE   = (255,165,0)
pygame.init()
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
def text_objects(text, font, col):
    textSurface = font.render(text, True, col)
    return textSurface, textSurface.get_rect()
def center_display_subtitle(text, x, y, coll):
    largeText = pygame.font.Font('freesansbold.ttf',45)
    TextSurf, TextRect = text_objects(text, largeText, coll)
    TextRect.center = (x, y)
    screen.blit(TextSurf, TextRect)
clock = pygame.time.Clock()
done = False
while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(BLACK)
    center_display_subtitle('Generic Game', 350, 100, RED)
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if 300+100 > mouse[0] > 300 and 300+50 > mouse[1] > 300:
        pygame.draw.rect(screen, LIGHT_ORANGE,(300,300,100,50))
        if click[0] == 1:
            done = True
    else:
        pygame.draw.rect(screen, ORANGE,(300,300,100,50))
    center_display_subtitle('Play', 350, 325, BLACK)
    clock.tick(60)
    pygame.display.flip()
done = False
screen.fill(BLACK)
center_display_subtitle('LOADING...', 350, 100, BLUE)
pygame.display.flip()
class Player(pygame.sprite.Sprite):
    def __init__(self, color, width, height):

        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.colour = color
        self.time2 = 0
        self.speed = 20
        self.set_target((0, 0))

        self.rect = self.image.get_rect()
        self.pos = pygame.math.Vector2(0, 0)
    def set_target(self, posi):
        self.target = pygame.math.Vector2(posi)
    def update(self):
        move = self.target - self.pos
        move_length = move.length()

        self.rect = self.target
all_sprites_list = pygame.sprite.Group()
player = Player(RED, 32, 32)
all_sprites_list.add(player)

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            for ship in all_sprites_list:
                ship.set_target(pygame.mouse.get_pos())
    screen.fill(BLACK)
    all_sprites_list.update()
    all_sprites_list.draw(screen)
    clock.tick(60)
    pygame.display.flip()*

代码用于主菜单和小型“迷你游戏”,玩家稍后将不得不躲避敌人或其他东西。真的,这只是一个pygame测试(就像我说我使用Tkinter),我会很高兴的帮助。

EN

回答 2

Stack Overflow用户

发布于 2019-03-21 13:02:50

票数 0
EN

Stack Overflow用户

发布于 2019-03-21 14:06:04

pygame.math.Vector2.normalize将玩家的方向向量标准化()到目标。这意味着方向向量的长度变为1:

代码语言:javascript
复制
move = move.normalize()

self.pos目标位置()移动到目标方向,距离目标(move_length)和速度(self.speed)的距离最小值:

代码语言:javascript
复制
self.pos = self.pos + move * min(self.speed, move_length)

最后更新self.rect

代码语言:javascript
复制
self.rect = self.pos 
代码语言:javascript
复制
class Player(pygame.sprite.Sprite):

    # [...]

    def update(self):
        move = self.target1 - self.pos
        move_length = move.length()
        if move_length > 0:
            move = move.normalize()
            self.pos = self.pos + move * min(self.speed, move_length)
            self.rect = self.pos

注意,我建议降低速度:

代码语言:javascript
复制
self.speed = 5

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档