首页 \ 问答 \ 没有绘制Pygame精灵(Pygame sprites not being drawn)

没有绘制Pygame精灵(Pygame sprites not being drawn)

我正在玩Python,试图写一个(非常)简单的太空入侵者游戏 - 但是我的子弹精灵并没有被绘制出来。 我现在正在使用相同的图形 - 只要我有其他一切正常工作,我就会对图形进行美化。 这是我的代码:

# !/usr/bin/python

import pygame

bulletDelay = 40

class Bullet(object):
    def __init__(self, xpos, ypos, filename):
        self.image = pygame.image.load(filename)
        self.rect = self.image.get_rect()
        self.x = xpos
        self.y = ypos

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))


class Player(object):
    def __init__(self, screen):
        self.image = pygame.image.load("spaceship.bmp")       # load the spaceship image
        self.rect = self.image.get_rect()                     # get the size of the spaceship
        size = screen.get_rect()
        self.x = (size.width * 0.5) - (self.rect.width * 0.5) # draw the spaceship in the horizontal middle
        self.y = size.height - self.rect.height               # draw the spaceship at the bottom

    def current_position(self):
        return self.x

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))            # blit to the player position


pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
player = Player(screen)                                       # create the player sprite
missiles = []                                                 # create missile array
running = True
counter = bulletDelay

while running: # the event loop
    counter=counter+1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False
    key = pygame.key.get_pressed()
    dist = 1                   # distance moved for each key press
    if key[pygame.K_RIGHT]:    # right key
        player.x += dist
    elif key[pygame.K_LEFT]:   # left key
        player.x -= dist
    elif key[pygame.K_SPACE]:  # fire key
        if counter > bulletDelay:
            missiles.append(Bullet(player.current_position(),1,"spaceship.bmp"))
            counter=0

    for m in missiles:
        if m.y < (screen.get_rect()).height and m.y > 0:
            m.draw(screen)
            m.y += 1
        else:
            missiles.pop(0)

    screen.fill((255, 255, 255))  # fill the screen with white
    player.draw(screen)           # draw the spaceship to the screen
    pygame.display.update()       # update the screen
    clock.tick(40)

有没有人有任何建议为什么我的子弹没有被绘制?

手指交叉,你可以提供帮助,并提前感谢你。


I'm playing with Python, trying to write a (very) simple space invaders game - but my bullet sprites aren't being drawn. I'm using the same graphic for everything at the moment - I'll prettify the graphics just as soon as I have everything else working. This is my code:

# !/usr/bin/python

import pygame

bulletDelay = 40

class Bullet(object):
    def __init__(self, xpos, ypos, filename):
        self.image = pygame.image.load(filename)
        self.rect = self.image.get_rect()
        self.x = xpos
        self.y = ypos

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))


class Player(object):
    def __init__(self, screen):
        self.image = pygame.image.load("spaceship.bmp")       # load the spaceship image
        self.rect = self.image.get_rect()                     # get the size of the spaceship
        size = screen.get_rect()
        self.x = (size.width * 0.5) - (self.rect.width * 0.5) # draw the spaceship in the horizontal middle
        self.y = size.height - self.rect.height               # draw the spaceship at the bottom

    def current_position(self):
        return self.x

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))            # blit to the player position


pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
player = Player(screen)                                       # create the player sprite
missiles = []                                                 # create missile array
running = True
counter = bulletDelay

while running: # the event loop
    counter=counter+1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False
    key = pygame.key.get_pressed()
    dist = 1                   # distance moved for each key press
    if key[pygame.K_RIGHT]:    # right key
        player.x += dist
    elif key[pygame.K_LEFT]:   # left key
        player.x -= dist
    elif key[pygame.K_SPACE]:  # fire key
        if counter > bulletDelay:
            missiles.append(Bullet(player.current_position(),1,"spaceship.bmp"))
            counter=0

    for m in missiles:
        if m.y < (screen.get_rect()).height and m.y > 0:
            m.draw(screen)
            m.y += 1
        else:
            missiles.pop(0)

    screen.fill((255, 255, 255))  # fill the screen with white
    player.draw(screen)           # draw the spaceship to the screen
    pygame.display.update()       # update the screen
    clock.tick(40)

Does anyone have any suggestions why my bullets aren't getting drawn?

Fingers crossed that you can help, and thank you in advance.


原文:https://stackoverflow.com/questions/40723058
更新时间:2023-01-10 14:01

最满意答案

我假设“当前选择的选项”是$ _POST [“online_lot_category”]中指定的选项 - 如果未设置,则默认为“All Lots”

如果要在下拉菜单中选择一个选项,则只需编写

<option value="somevalue" selected>text</option>

所以无论你是否打字

<option value="somevalue" selected="">text</option>

要么

<option value="somevalue" selected="selected">text</option>

浏览器会将该选项解释为已选中。 所以,相反,你应该使用这个:

...
foreach ( $terms as $term ) {
    if ( $_POST["online_lot_category"] == $term->slug ){
        $selected_option = ' selected';
    } else {
        $selected_option = '';
    }
    echo '<option value="' . $term->slug . '"' . $selected_option . '>' . $term->name . '</option>';
}
...

I'm assuming that "the currently selected option" is the option specified in $_POST["online_lot_category"] - and if this isn't set, then the default is "All Lots"

When you want to select an option in a dropdown menu, you only have to write

<option value="somevalue" selected>text</option>

So regardless of whether you type

<option value="somevalue" selected="">text</option>

or

<option value="somevalue" selected="selected">text</option>

the browser will interpret the option as being selected. So, instead, you should use this:

...
foreach ( $terms as $term ) {
    if ( $_POST["online_lot_category"] == $term->slug ){
        $selected_option = ' selected';
    } else {
        $selected_option = '';
    }
    echo '<option value="' . $term->slug . '"' . $selected_option . '>' . $term->name . '</option>';
}
...

相关问答

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)