首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在游戏中用move_ip移动多边形

在游戏中用move_ip移动多边形
EN

Stack Overflow用户
提问于 2015-07-18 12:00:55
回答 1查看 1.2K关注 0票数 0

我无法在游戏中用函数move_ip()移动多边形,如果我画一个矩形,代码就能正常工作,但对于多边形则不行。当我按下任何箭头键时,它不会响应。

我想我不需要宇宙飞船长方形,因为多边形是与游戏的直角,但它似乎不对,我不知道,我有一个烂摊子。

这是我的游戏代码:

代码语言:javascript
复制
import sys, os
import pygame
from pygame.locals import *
import numpy as np

if sys.platform in ["win32","win64"]: os.environ["SDL_VIDEO_CENTERED"]='1'

width_screen = 600
height_screen = 480
screen_size = (width_screen, height_screen)
positionx_screen = 0
positiony_screen = 32
title_window = 'ASTEROIDS'

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

spaceship = pygame.Rect(200, 475, 120, 20)

def quit_screen(event):
    if event.type == QUIT:
        pygame.quit()
        sys.exit()


class Spaceship():
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.colour = WHITE
        self.thickness = 0
        self.movex = 20

    def draw_nave(self):
        vertices = np.array([[screen_size[0]/2, screen_size[1] /2 - 30],[screen_size[0]/2 + 15, screen_size[1]/2],
                             [screen_size[0]/2-15, screen_size[1] / 2]])
        pygame.draw.polygon(screen_game, self.colour, vertices, self.thickness)


    def move_spaceship_right(self):
        spaceship.move_ip(self.movex, 0)


    def move_spaceship_left(self):
        spaceship.move_ip(- self.movex, 0)


def game():
    pygame.init()
    running = True
    global screen_game
    screen_game = pygame.display.set_mode(screen_size,
                                          positionx_screen, positiony_screen)
    pygame.display.set_caption(title_window)

    clock = pygame.time.Clock()

    my_spaceship = Spaceship(200, 475, 120, 20)

    screen_game.fill(BLACK)
    while running:
        screen_game.fill(BLACK)
        for event in pygame.event.get():
            quit_screen(event)
            if event.type == pygame.KEYDOWN:
                if event.key == K_RIGHT:
                    my_spaceship.move_spaceship_right()
                if event.key == K_LEFT:
                    my_spaceship.move_spaceship_left()


        my_spaceship.draw_nave()
        pygame.display.flip()
        clock.tick(60)

if __name__ == "__main__":
    try:
        game()
    except:
        pygame.quit()

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-07-18 20:47:26

问题是你的Rect与你画的多边形一点关系都没有。你每次都把它画在同一个位置。

最好只创建一次Surface,在该Surface上绘制一次多边形,并在绘制新的Surface时使用spaceship作为位置。

我建议从Sprite中子类化,在__init__函数中创建Surface,并将Rect保存在self.rect中,而不是spaceship变量。

然后要么使用draw函数Sprite,要么将Sprite放入Group中。

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

https://stackoverflow.com/questions/31490944

复制
相关文章

相似问题

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