1
什麼作品:pygame精靈沒有渲染或更新
1)船舶和背景載荷沒有搭便車。 2)船舶運動起作用 3)I是能夠保持船的從能夠退出的視圖 4)程序創建像素場(以模擬子彈)在K_SPACEBAR
的KEYDOWN事件我需要幫助的:
1)儘管創造了「子彈」之後,我盡了最大努力精靈不會動畫 2)的精靈用來表示碎片甚至不會創造更談不上動畫 3)當然,這意味着我可以甚至不測試我的子彈和碎片之間的單位碰撞 4)不知道如何開始處理與船舶檢測碎片(精靈)檢測10 5)由於某些原因,子彈精靈呈現在船的左側。不知道在哪裏/如何操縱它們的x值
代碼,因爲它代表: 我留下的筆記所有的地方,以幫助解釋我的瘋狂
bif="bg.jpg"
ship = "spaceship.png"
import pygame, sys
from pygame import *
from pygame.locals import *
import random
pygame.init()
yellow=(255,255,51)
green=(50,190,50)
difficulty = 10
i=0
debris_list=pygame.sprite.Group()
bullet_list=pygame.sprite.Group()
class Square(pygame.sprite.Sprite):#makes squares
def __init__(self, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface ([7,7])
self.image.fill(color)
self.rect=self.image.get_rect()
class Bullet(pygame.sprite.Sprite): #bullet creation
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image= pygame.Surface([7,7])
self.image.fill(yellow)
self.rect= self.image.get_rect()
def update(self): #sprite movement
if bullet:
self.rect.y-=5
if debris:
self.rect.y+=5
class debris(pygame.sprite.Sprite): #debris creation
def __init__(self,color):
pygame.sprite.Sprite.__init__(self)
self.image= pygame.Surface([15,15])
self.image.fill(green)
self.rect= self.image.get_rect()
window =pygame.display.set_caption("Space Explorer")
clock=pygame.time.Clock()
speed=250
screen = pygame.display.set_mode((800,600),0,32)
x,y =0,0
movex,movey= 0,0
background = pygame.image.load(bif).convert() # puts background on screen
ship_pos=pygame.image.load(ship).convert_alpha() #loads player ship
while True:
for event in pygame.event.get():
if event.type == QUIT: #escape functionality
pygame.quit()
sys.exit()
if event.type == KEYDOWN: #ship movement
if event.key == K_LEFT:
movex=-1
elif event.key == K_RIGHT:
movex=+1
elif event.key ==K_UP:
movey=-1
elif event.key ==K_DOWN:
movey=+1
elif event.key == K_a:
movex=-1
elif event.key == K_d:
movex=+1
elif event.key ==K_w:
movey=-1
elif event.key ==K_s:
movey=+1
if event.type==KEYUP:
if event.key == K_LEFT:
movex=0
elif event.key == K_RIGHT:
movex=0
elif event.key ==K_UP:
movey=0
elif event.key ==K_DOWN:
movey=0
elif event.key == K_a:
movex=0
elif event.key == K_d:
movex=0
elif event.key ==K_w:
movey=0
elif event.key ==K_s:
movey=0
if event.type == KEYDOWN:#player input bullet creation:
if event.key==K_SPACE:
bullet = Bullet()
bullet.rect.x=x
bullet.rect.y=y
bullet_list.add(bullet)
#hit tracking and score
#no collision detection for debris and ship yet.
for bullet in bullet_list:
debris_hit_list=pygame.sprite.spritecollide(bullet,debris_list,True)
for debris in debris_hit_list:
bullet_list.remove(bullet)
score+1
print score
if bullet.rect.y<10:
bullet_list.remove(bullet)#stop tracking rogue bullets
#debris spawn location and rate
#every 10 iterations "debris" should spawn randomly on the x axis
#and move downwards
if i == difficulty:
debris = Square(green)
debris.rect.x= random.randrange(800)
debris.rect.y=-700
debris_list.add(debris)
i=0
i=i+1
bullet_list.update()
debris_list.update()
#keeps ship within screen
if x >=680:
x=680
if x <= 5:
x=5
if y >=500:
y= 500
if y <=5:
y=5
x+=movex
y+=movey
bullet_list.draw(screen)
debris_list.draw(screen)
screen.blit(background, (0,0))
screen.blit(ship_pos,(x,y))
pygame.display.update()