Έφτιαξα ένα παιχνίδι για πλάκα με έναν φίλο μου και είπα να το μοιραστώ μαζί σας.
Κώδικας Κλάσης Sprite:
#Ξέρω ότι υπάρχει και στο pygame κλάση sprite, απλώς ήθελα να φτιάξω την δική μου.
class Sprite:
#-------------Constructor-------------#
def __init__(self, image, resolution, start_pos):
'''Create a new sprite object.'''
self.image = image #Image.
self.pos = start_pos #Position.
self.x, self.y = start_pos #x and y position.
self.width, self.height = resolution #width and height.
self.tag = None #Tag.
return
#-------------Constructor-------------#
#Resetting the position of the sprite.
def set_pos(self, new_pos):
'''Set a new position for this sprite'''
self.pos = new_pos
self.x, self.y = new_pos
return
#Changing the image of the sprite.
def set_image(self, new_image, new_resolution):
'''Change the image of this spite'''
self.image = new_image
self.width, self.height = new_resolution
return
#Setting a name for this sprite.
def set_tag(self, name_of_tag):
'''Set a tag for this sprite.'''
self.tag = name_of_tag
return
#Get the center of the image.
def get_center(self):
'''Get the centered coordinates of the image.'''
#Finding the center
centered_x = self.x + (self.width/2)
centered_y = self.y + (self.height/2)
return (centered_x, centered_y) #Returning the coordinates of the center.
#Get the left side of the sprite.
def get_left(self):
'''Returns the left side (x coordinate) of the sprite.'''
return self.x
#Get the right side of the sprite.
def get_right(self):
'''Returns the left side (x coordinate) of the sprite.'''
return self.x + self.width
#Get the top side of the sprite.
def get_top(self):
'''Returns the top side (y coordinate) of the sprite.'''
return self.y
#Get the bottom side of the sprite.
def get_bottom(self):
'''Returns the bottom side (y coordinate) of the sprite.'''
return self.y + self.height
#Get the tag of this sprite.
def get_tag(self):
'''Get th name tag of this sprite.'''
return self.tag
#Get the image.
def get_image(self):
'''Get the image of this sprite'''
return self.image
#Get position.
def get_pos(self):
'''Get the position of the sprite.'''
return self.pos
#Get x.
def get_posx(self):
'''Get the x coordinate of the sprite.'''
return self.x
#Get y.
def get_posy(self):
'''Get the y coordinate of the sprite.'''
return self.y
#Get the width of the sprite.
def get_width(self):
'''Get the width of the sprite.'''
return self.width
#Get the height of the sprite.
def get_height(self):
'''Get the height of the sprite.'''
return self.height
#Get collision with a list of other sprites.
def get_collision(self, sprite):
'''Detect collision with another sprite. --> Sprite object or None'''
self_upLeft = self.x #Up left corner of this sprite.
self_upRight = self.x + self.width #Up right corner of this sprite.
self_up = self.y #Top height of this sprite.
self_down = self.y + self.height #Low height of this sprite.
sprite_upLeft = sprite.get_posx() #Up left corner of other sprite.
sprite_upRight = sprite.get_posx() + sprite.get_width() #Up right corner of other sprite.
sprite_up = sprite.get_posy() #Top height of other sprite.
sprite_down = sprite.get_posy() + sprite.get_height() #Low height of other sprite.
#Checking for collision.
if (self_upRight >= sprite_upLeft and self_upLeft <= sprite_upRight) or (self_upLeft <= sprite_upRight and self_upRight >= sprite_upLeft):
if (self_up <= sprite_up and self_down >= sprite_up) or (self_up <= sprite_down and self_down >= sprite_down):
return sprite #If this with the other sprite collides, then return the other sprite object.
#Collision didn't detected.
return None
#Move the sprite horizontal.
def move_horizontal(self, left, right, speed):
'''Move the sprite horizontal.'''
#Move left.
if left:
self.set_pos((self.x - speed, self.y))#Changing the position of this sprite object.
pass
#Move right.
elif right:
self.set_pos((self.x + speed, self.y))#Changing the position of this sprite object.
pass
#Move the sprite vertical.
def move_vertical(self, up, down, speed):
'''Move the sprite vertical.'''
#Move up.
if up:
self.set_pos((self.x, self.y - speed)) #Changing the position of this sprite object.
pass
#Move down.
if down:
self.set_pos((self.x, self.y + speed))#Changing the position of this sprite object.
pass
Κώδικας της Main:
import pygame, gameMechanics, sys, random
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()
player_img = pygame.image.load("player.png").convert_alpha()
coin_img = pygame.image.load("coin.png").convert()
player = gameMechanics.Sprite(player_img, (player_img.get_width(), player_img.get_height()), (300,300))
coins = []
for i in range(0,5):
coins.append(gameMechanics.Sprite(coin_img, (coin_img.get_width(), coin_img.get_height()), (random.randrange(0,500), random.randrange(0,500))))
up = False
down = False
left = False
right = False
speed = 10
score = 0
i = 0
while True:
screen.fill((255,255,255))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pass
if event.type == KEYDOWN:
if event.key == K_UP:
up = True
pass
if event.key == K_DOWN:
down = True
pass
if event.key == K_LEFT:
left = True
pass
if event.key == K_RIGHT:
right = True
pass
if event.type == KEYUP:
if event.key == K_UP:
up = False
pass
if event.key == K_DOWN:
down = False
pass
if event.key == K_LEFT:
left = False
pass
if event.key == K_RIGHT:
right = False
pass
player.move_horizontal(left, right, speed)
player.move_vertical(up, down, speed)
for coin in coins:
screen.blit(coin.get_image(), coin.get_pos())
collision = player.get_collision(coin)
if (collision != None):
coins.remove(collision)
screen.blit(player.get_image(), player.get_pos())
clock.tick(60)
pygame.display.update()
Ποια είναι η διαφορά να το ανεβάσω εκεί; Οποιον file hoster και να διαλέξω τη σημασία έχει;
Ξέρω ότι το github χρησιμοποιήτε για να κάνεις file hosting κώδικες αλλά ποια έιναι η διαφορά;