首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在pygame中绘制形状后将其删除?

如何在pygame中绘制形状后将其删除?
EN

Stack Overflow用户
提问于 2021-01-11 21:37:21
回答 1查看 84关注 0票数 0

我正在用pygame做一个游戏。我使用“激光”作为玩家发射的投射物。我目前使用的系统是,当特定条件为真时,我将激光的颜色更改为红色。但是,这不适用于collidirect。当“激光”与实体碰撞时,Collidirect会立即“激活”。激光是在我的代码的最后一行绘制的。

代码如下:

代码语言:javascript
运行
复制
import pygame
import sys
# Creating a loop to keep program running
    while True:
       
        # --- Event Processing and controls
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    spaceship_x_change = 10
                elif event.key == pygame.K_LEFT:
                    spaceship_x_change = -10
                elif event.key == pygame.K_UP:
                    red = (255, 0, 0)
            elif event.type == pygame.KEYUP:
                red = (0, 0, 0)
                spaceship_x_change = 0

        spaceship_x += spaceship_x_change

        # Preventing the ship from going off the screen
        if spaceship_x > display_width - 140:
            spaceship_x -= 10
        if spaceship_x < 1:
            spaceship_x += 10

        pygame.draw.rect(game_display, red, [spaceship_x + 69, 70, 4, 310])
EN

回答 1

Stack Overflow用户

发布于 2021-01-11 22:22:37

这只是一个理论上的例子,因为代码片段本身并不起作用,所以我将向你展示一个如何实现移除对象的例子。

因此,如果你想在飞船到达屏幕边缘时将其移除,你可以按照下面的思路做一些事情。

代码语言:javascript
运行
复制
import pygame
import sys
# Creating a loop to keep program running

draw_spaceship = True

    while True:
       
        # --- Event Processing and controls
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    spaceship_x_change = 10
                elif event.key == pygame.K_LEFT:
                    spaceship_x_change = -10
                elif event.key == pygame.K_UP:
                    red = (255, 0, 0)
            elif event.type == pygame.KEYUP:
                red = (0, 0, 0)
                spaceship_x_change = 0

        spaceship_x += spaceship_x_change

        # Preventing the ship from going off the screen
        if spaceship_x > display_width - 140:
            draw_spaceship = False
        elif spaceship_x < 1:
            draw_spaceship = False
        else:
            # spaceship is within the screen bounds
            draw_spaceship = True

        if draw_spaceship:
            # this doesnt actually draw the spaceship,
            # but you would put the equivalent code here that did that.
            pygame.draw.rect(game_display, red, [spaceship_x + 69, 70, 4, 310])

所以你需要一个标志或一些你想要检查的条件,它将决定你是否绘制一个对象。

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

https://stackoverflow.com/questions/65667898

复制
相关文章

相似问题

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