[英]python pygame different ways to blit sprite.Group() on the sceen
I want to create an ship and setting 3 small ship symbol for lives so i set the different size of the image as self.ship=pygame.transform.smoothscale(temp,(200,200))
and self.image=pygame.transform.smoothscale(temp,(70,70))
The big size (200,200)
for moving ship and the small size (70,70)
for left_ship as symbol我想创建一艘船并为生命设置 3 个小船符号,所以我将图像的不同大小设置为self.ship=pygame.transform.smoothscale(temp,(200,200))
和self.image=pygame.transform.smoothscale(temp,(70,70))
移动船的大尺寸(200,200)
和(70,70)
的小尺寸(70,70)
作为符号
I use two way to do this:我使用两种方法来做到这一点:
self.ships.draw(self.setting.screen)
this way need set the ship as image
and the position as rect
这种方式需要将船设置为image
并将位置设置为rect
for left_ship in self.ships: self.setting.screen.blit(left.ship.image,left.ship.rect)
But finally when i create the whole code and run it there's no left_ship symbol on the screen,what cause it and how can i fix it??但最后当我创建整个代码并运行它时,屏幕上没有 left_ship 符号,是什么原因造成的,我该如何解决? here's the codes:这是代码:
#!/usr/bin/python
import sys
import pygame
class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=pygame.RESIZABLE
self.color=(255,255,255)
self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
self_title=pygame.display.set_caption("Muhaha")
self.ship_left = 3
class Lifes():
'''create a Group for left_ships '''
def __init__(self,setting):
self.setting=setting
self.left_ships()
def left_ships(self):
self.ships=pygame.sprite.Group()
'''set the different position and add into the Group'''
for ship_number in range(self.setting.ship_left):
ship=Ship(self.setting)
ship.rect.x= 100 + ship_number*ship.rect.width
ship.rect.y= self.setting.screen.get_rect().height
self.ships.add(ship)
def blit(self):
'''test two way to blit the left_ship on the screen but all failed'''
self.ships.draw(self.setting.screen)
for left_ship in self.ships:
self.setting.screen.blit(left_ship.image,left_ship.rect)
class Ship(pygame.sprite.Sprite):
def __init__(self,setting):
super().__init__()
'''set two size of ship and set the big ship postion is on the middle bottom of the screen'''
self.setting=setting
temp=pygame.image.load("/home/finals/python/alien/image/title.jpg").convert_alpha()
self.ship=pygame.transform.smoothscale(temp,(200,200))
self.image=pygame.transform.smoothscale(temp,(70,70))
self.screen_rect=self.setting.screen.get_rect()
self.rect=self.ship.get_rect()
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
def blit(self):
self.setting.screen.fill((255,255,255))
self.setting.screen.blit(self.ship,self.rect)
class Check_event():
def __init__(self):
self.moving_up = False
self.moving_down = False
self.moving_left = False
self.moving_right = False
def event(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def game():
pygame.init()
setting=Setting(1200,800)
ship=Ship(setting)
check_event=Check_event()
lifes=Lifes(setting)
while True:
check_event.event()
lifes.blit()
ship.blit()
pygame.display.flip()
game()
There are 2 issues.有2个问题。 The screen is cleared in Ship.blit
, hence everything what is drawn before ship.blit()
is not visible.屏幕在Ship.blit
被清除,因此在ship.blit()
之前绘制的所有内容都不可见。 Clear the screen in the main application loop before drawing the ships:在绘制船舶之前清除主应用程序循环中的屏幕:
class Ship(pygame.sprite.Sprite):
# [...]
def blit(self):
self.setting.screen.blit(self.ship,self.rect)
def game():
# [...]
while True:
check_event.event()
setting.screen.fill((255,255,255))
ship.blit()
lifes.blit()
pygame.display.flip()
Furthermore the small ships are out of the window at the bottom.此外,小船在底部的窗外。 That is caused by ship.rect.y = self.setting.screen.get_rect().height
.这是由ship.rect.y = self.setting.screen.get_rect().height
。 Change the position of the ships.改变船只的位置。 eg:例如:
class Lifes():
# [...]
def left_ships(self):
self.ships=pygame.sprite.Group()
'''set the different position and add into the Group'''
for ship_number in range(self.setting.ship_left):
ship=Ship(self.setting)
ship.rect = ship.image.get_rect()
ship.rect.x = 100 + ship_number*ship.rect.width*2
ship.rect.bottom = self.setting.screen.get_rect().height
self.ships.add(ship)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.