pygame精灵没有渲染或更新

时间:2014-02-23 23:51:18

标签: animation pygame render collision sprite

什么有效:

1)船舶和背景负荷无故障。 2)船舶运动正在发挥作用 3)我能够让船不能离开视野 4)程序在K_SPACEBAR的KEYDOWN事件中创建像素(模拟项目符号)

我需要帮助:

1)尽管我在制作“子弹”后尽了最大的努力,精灵也不会生动 2)用于表示碎片的精灵甚至不会产生更多的动画 3)当然这意味着我甚至无法测试子弹和碎片之间的单位碰撞 4)不知道如何开始用船检测碎片(精灵)检测 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()

1 个答案:

答案 0 :(得分:0)

(对不起我的英文)

1)在Bullet.update()中使用

    if bullet: 
        self.rect.y-=5
    if debris:
        self.rect.y+=5

但物体子弹和碎片是

答:未定义(在代码的开头)

B:返回false(我认为)

但如果它们都返回真实,则子弹停留在同一个地方。

2)从不使用碎片类(使用rect类)

碎片y = -700屏幕上方是什么

3)我认为结合工作

4)定义Ship_Rect并执行

for d in debris_list:
    if Ship_Rect.colliderect(d.rect):
        //Stuff to do when ship collides debris

5)对象的X和Y位于左上角:

    bullet.rect.x= x + (Ship_Rect.heigth/2)
    bullet.rect.y= y + (Ship_Rect.length/2)