没有绘制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
最满意答案
我假设“当前选择的选项”是$ _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>'; } ...
相关问答
更多-
只需迭代选择 $('select[name^="mydropdowns"]').each(function() { if ($(this).val() != 'Test') { alert("Found."); } }); val()适用于input/select/textarea元素而不适用于option元素。 来自jQuery doc: http : //api.jquery.com/val/ .val()方法主要用于获取表单元素的值,例如input,select和textar ...
-
您的代码似乎对StaleElementException以及异常Element is not clickable at point...很敏感。 尝试下面的网页抓取代码的代码,并让我知道结果: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.select import Select from selenium.webdriver.com ...
-
选择选项下拉菜单:从下拉选项中隐藏默认值(select option dropdown: hide the default value from dropdown options)[2023-08-20]
尝试一下: select option:checked { display: none; } Try it: select option:checked { display: none; } -
如果你考虑发生什么的时间, ng-init会在ajax调用返回之前被调用。 所以,你正在设置selected = Catalogos[0] ,它还不存在,所以它是未定义的。 然后,当你的ajax调用返回并且你设置了$ scope.Catalogos时,它会把它们加载到选择列表中,但是仍然有undefined的选项。 你需要做的只是说$scope.selected = $scope.Catalogos[0]; 在你的ajax回调如下所示: function cargarCatalogo() { api ...
-
在JQuery Mobile的选择菜单中设置默认选择?(Setting a default selection in a select menu in JQuery Mobile?)[2020-01-28]
您正在尝试使用尚未加载的JavaScript处理DOM对象。 将javascript移动到您尝试操作的表单之后,或者将您的代码更多地移动到在文档加载时执行的函数中。 例如: $('#PAGE').live('pagecreate',function(event){ var myselect = $("select#foo"); myselect[0].selectedIndex = 3; myselect.selectmenu("refresh"); }); 其中PAGE是您正在加 ... -
从[2024-01-26]
你所要做的就是清空div。 您可以将innerHTML设置为空字符串以清空div。 注意: 你要做的就是在你的div中添加/附加元素。 所以你必须清除div(删除它的孩子)。 代码应该是list.innerHTML = ""; 完整的代码应如下所示: function AgeDropDown(){ var list= document.getElementById("Age"); list.innerHTML = ""; for(var i=1;i<100;i++) { ... -
使用URL填充下拉菜单(Populating dropdown menus with URL)[2022-03-16]
仅当更改选择框的值时才会触发ng-change 。 所以,尝试使用: if (params.selectedColor) { if ($scope.selectedLetter && $scope.selectedNumber) { $scope.colorSelection = colors[$scope.selectedLetter + $scope.selectedNumber]; } $scope.selectedColor = params.selected ... -
我假设“当前选择的选项”是$ _POST [“online_lot_category”]中指定的选项 - 如果未设置,则默认为“All Lots” 如果要在下拉菜单中选择一个选项,则只需编写 所以无论你是否打字 要么