簡體   English   中英

如何在pygame中使窗口邊緣的精靈反彈

[英]how to make a sprite bounce of the edges of the window in pygame

到目前為止,我的程序從左到右彈跳,但是現在我想彈跳牆壁。 我該怎么辦?

delta = 5
u = 1 # x coordinate

u += delta
if u >= 440: #480 is the window size, I have it stopping at 440
   delta = -5 #Go back by 5
elif u < 0:
   delta = 5 #Go up by 5

較大的代碼片段,可以解決,但sprite不在屏幕上。要運行代碼,請確保您具有名為“ bird.png”的圖片或更改圖片:

import pygame
import random

class mySprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("bird.png")
        self.rect = self.image.get_rect()
##        slef.rect(50,50,self.image.get_width(), self.image.get_height())

    def update(self,x,y):
        self.rect.x = x
        self.rect.y = y

class newSprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("ball.png")
        self.rect = self.image.get_rect()

    def update(self,x,y):
        self.rect.x = x
        self.rect.y = y

class otherSprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("bird.png")
        self.rect = self.image.get_rect()

    def update(self,x,y):
        self.rect.x = x
        self.rect.y = y

def main():
    """ Set up the game and run the main game loop """
    pygame.init() # prepare the pygame module for use
    surfaceSz = 480 # Desired physical surface size, in pixels.
    # Create surface of (width, height), and its window.
    main_surface = pygame.display.set_mode((surfaceSz, surfaceSz))

    small_rect = (300, 200, 150, 90)
    some_color = (255,0,0)
    main_surface.fill((0,200,255))

    s = mySprite()
    all_sprites = pygame.sprite.Group()

    s.add(all_sprites)

    delta_x = 5
    delta_y = 5
    u = 1 # x coord
    v = 1 # y coord

    clock = pygame.time.Clock()
    while True:
        ev = pygame.event.poll() # look for any event
        if ev.type == pygame.QUIT: # window close button clicked?
            break # ... leave game loop

        main_surface.fill((0,200,255))

        #u += delta
        #if u >= 440:
        #    delta = -5
        #elif u < 0:
        #    delta = 5

        u += delta_x
        v += delta_y
        if (v >=440): #assuming your y axis is 440 pixels
            delta_y = -5
        elif v<=0 :
            delta_y = 5

        if u >= 440: #440 pixels u have
            delta = -5
        elif u < 0:
            delta = 5 #Go up by 5


        all_sprites.update(u,50)
        pos =(random.randint(0, surfaceSz), random.randint(0, surfaceSz))
        all_sprites.draw(main_surface)

        # Now the surface is ready, tell pygame to display it!
        pygame.display.flip()
        clock.tick(60)

    pygame.quit() # once we leave the loop, close the window.

main()

也使坐標

delta_y = 5
delta_x = 5
u = 1 # the x coordinate
v = 1 # the y coordinate

u +=delta_x    
v +=delta_y

if (v >=300): #assuming your y axis is 300 pixels 
    delta_y = -5
elif v<=0 :
    delta_y = 5

if u >= 440: #440 pixels u have
    delta_x = -5 
elif u < 0:
    delta_x = 5 #Go up by 5

這是一個從牆上彈起的動靜的鳥/球,首先,您只需要一種類型的精靈即可,只需創建具有不同圖像的不同實例即可。 希望能幫助到你

import sys, pygame, random

SIZE = width, height = 640, 480
SPEED = [2, 2]
black = 0, 0, 0

class mySprite(pygame.sprite.Sprite):
    def __init__(self, image="ball.bmp", speed=[2,2]):

        pygame.sprite.Sprite.__init__(self)
        self.speed = speed
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()

    def update(self):
        global SIZE
        #i used 110 px because it's the size of the image, but it's better to use the rect property of the pygame
        if (self.rect.x <0) or (self.rect.x > 640-110):
            self.speed[0] *= -1
        if (self.rect.y<0) or (self.rect.y > 480-110):
            self.speed[1] *= -1

        self.rect.x =self.rect.x + self.speed[0]
        self.rect.y =self.rect.y + self.speed[1]

#OR:        self.rect = self.rect.move(self.speed)

    def draw(self, screen):
        screen.blit(self.image, self.rect)


#    
pygame.init()
screen = pygame.display.set_mode(SIZE)

iaka = mySprite()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    screen.fill(black)
    iaka.draw(screen)
    iaka.update()
    pygame.display.flip()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM