grafika+object skała+agent nie wychodzi poza mapę

This commit is contained in:
wojtekgoralewski 2023-03-30 15:51:20 +02:00
parent af8529865d
commit 4a335218c0
23 changed files with 108 additions and 74 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

101
agent.py
View File

@ -1,50 +1,51 @@
import pygame import pygame
from config import * from config import *
class Agent(pygame.sprite.Sprite): class Agent(pygame.sprite.Sprite):
def __init__(self, game, x, y): def __init__(self, game, x, y):
self.game = game self.game = game
self.groups = self.game.all_sprites self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups) pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILE_SIZE self.x = x * TILE_SIZE
self.y = y * TILE_SIZE self.y = y * TILE_SIZE
self.width = TILE_SIZE self.width = TILE_SIZE
self.height = TILE_SIZE self.height = TILE_SIZE
self.x_change = 0 self.x_change = 0
self.y_change = 0 self.y_change = 0
self.AGENT_IMG = pygame.image.load("./zdjecia/gandalf.jpg") self.AGENT_IMG = pygame.image.load("./zdjecia/gandalf.png")
self.AGENT = pygame.transform.scale(self.AGENT_IMG,(64,64)) self.AGENT = pygame.transform.scale(self.AGENT_IMG,(64,64))
self.image = pygame.Surface([self.width, self.height]) self.image = pygame.Surface([self.width, self.height])
self.image.blit(self.AGENT, (0,0)) self.image.blit(self.AGENT, (0,0))
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = self.x self.rect = self.image.get_rect()
self.rect.y = self.y self.rect.x = self.x
self.rect.y = self.y
self.health = 100
self.health = 100
def update(self):
self.movement() def update(self):
self.movement()
self.rect.x += self.x_change
self.rect.y += self.y_change self.rect.x += self.x_change
self.rect.y += self.y_change
self.x_change = 0
self.y_change = 0 self.x_change = 0
self.y_change = 0
def movement(self):
keys = pygame.key.get_pressed() def movement(self):
if keys[pygame.K_LEFT]: keys = pygame.key.get_pressed()
self.x_change -= PLAYER_SPEED if keys[pygame.K_LEFT] and self.rect.x > 0:
if keys[pygame.K_RIGHT]: self.x_change -= TILE_SIZE
self.x_change += PLAYER_SPEED if keys[pygame.K_RIGHT] and self.rect.x < 832 - 64:
if keys[pygame.K_UP]: self.x_change += TILE_SIZE
self.y_change -= PLAYER_SPEED if keys[pygame.K_UP] and self.rect.y > 0:
if keys[pygame.K_DOWN]: self.y_change -= TILE_SIZE
self.y_change += PLAYER_SPEED if keys[pygame.K_DOWN] and self.rect.y < 832 - 64:
self.y_change += TILE_SIZE

View File

@ -1,6 +1,6 @@
FRAMERATE = 30 FRAMERATE = 30
WIDTH, HEIGHT = 800, 800 WIDTH, HEIGHT = 832, 832
TILE_SIZE = 64 TILE_SIZE = 64
BLACK = ((0,0,0)) BLACK = ((0,0,0))
WHITE = ((255,255,255)) WHITE = ((255,255,255))
PLAYER_SPEED = 3 #PLAYER_SPEED = 3

43
main.py
View File

@ -1,7 +1,7 @@
import pygame import pygame
import random
from config import * from config import *
from agent import * from agent import *
from map_add_ons import *
class Game: class Game:
@ -10,30 +10,36 @@ class Game:
self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
self.running = True self.running = True
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
# te wszystkie obrazki trzeba dać w oddzielnej klasie dla potworków jak coś później
#self.ARCHER_ORK_IMG = pygame.image.load("./zdjecia/ork_lucznik.png")
#self.INFATRY_ORK_IMG = pygame.image.load("./zdjecia/ork-piechota.png")
#self.INFANTRY_ORK2_IMG = pygame.image.load("./zdjecia/ork-piechota2.png")
#self.SAURON_IMG = pygame.image.load("./zdjecia/sauron.png")
self.BACKGROUND_IMG= pygame.image.load("./zdjecia/podloze.jpg")
# te wszystkie obrazki trzeba dać w oddzielnej klasie dla potworków jak coś później #self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
self.ARCHER_ORK_IMG = pygame.image.load("./zdjecia/ork_lucznik.jpg") #self.INFATRY_ORK = pygame.transform.scale(self.INFATRY_ORK_IMG,(64,64))
self.INFATRY_ORK_IMG = pygame.image.load("./zdjecia/ork-piechota.jpg") #self.INFANTRY_ORK2 = pygame.transform.scale(self.INFANTRY_ORK2_IMG,(64,64))
self.INFANTRY_ORK2_IMG = pygame.image.load("./zdjecia/ork-piechota2.jpg") #self.SAURON = pygame.transform.scale(self.SAURON_png,(64,64))
self.SAURON_JPG = pygame.image.load("./zdjecia/sauron.jpg") self.BACKGROUND = pygame.transform.scale(self.BACKGROUND_IMG,(64,64))
#self.ORK_LIST = [self.ARCHER_ORK,self.INFANTRY_ORK2, self.INFATRY_ORK]
self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
self.INFATRY_ORK = pygame.transform.scale(self.INFATRY_ORK_IMG,(64,64))
self.INFANTRY_ORK2 = pygame.transform.scale(self.INFANTRY_ORK2_IMG,(64,64))
self.SAURON = pygame.transform.scale(self.SAURON_JPG,(64,64))
self.ORK_LIST = [self.ARCHER_ORK,self.INFANTRY_ORK2, self.INFATRY_ORK]
pygame.display.set_caption('Do_Nazwania') pygame.display.set_caption('Do_Nazwania')
def new(self): # tworzy się nowa sesja grania def new(self): # tworzy się nowa sesja grania
self.all_sprites = pygame.sprite.LayeredUpdates() self.all_sprites = pygame.sprite.LayeredUpdates()
self.rock_sprites = pygame.sprite.Group()
self.agent = Agent(self,1,1) self.agent = Agent(self,1,1)
for y in range(5):
self.rock = Rocks(self,3,y)
def update(self): # update postaci na mapie, związane z poruszaniem się def update(self): # update postaci na mapie, związane z poruszaniem się
self.all_sprites.update() self.all_sprites.update()
pygame.sprite.spritecollide(self.agent, self.rock_sprites, True)
#ograniczyć ruch agenta gdy jest kolizja ze skałami
def events(self): def events(self):
for event in pygame.event.get(): for event in pygame.event.get():
@ -42,20 +48,21 @@ class Game:
pygame.quit() pygame.quit()
def map(self): # tworzenie mapy def map(self): # tworzenie mapy
self.SCREEN.fill(BLACK)
self.all_sprites.draw(self.SCREEN)
self.clock.tick(FRAMERATE) self.clock.tick(FRAMERATE)
for x in range(0, WIDTH, TILE_SIZE): for x in range(0, WIDTH, TILE_SIZE):
for y in range(0, HEIGHT, TILE_SIZE): for y in range(0, HEIGHT, TILE_SIZE):
self.SCREEN.blit(self.BACKGROUND,(x,y))
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE) self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
pygame.draw.rect(self.SCREEN, WHITE, self.rect, 1) pygame.draw.rect(self.SCREEN, BLACK, self.rect, 1)
self.all_sprites.draw(self.SCREEN)
self.rock_sprites.draw(self.SCREEN)
pygame.display.update() pygame.display.update()
def main(self): def main(self):
self.events() self.events()
self.update() self.update()
self.map() self.map()
g = Game() g = Game()
g.new() g.new()
while g.running: while g.running:

26
map_add_ons.py Normal file
View File

@ -0,0 +1,26 @@
import pygame
from config import *
class Rocks(pygame.sprite.Sprite):
def __init__(self,game,x,y):
self.game = game
self.groups = self.game.rock_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILE_SIZE
self.y = y * TILE_SIZE
self.width = TILE_SIZE
self.height = TILE_SIZE
self.ROCKS_PNG = pygame.image.load("./zdjecia/rock.png")
self.ROCKS = pygame.transform.scale(self.ROCKS_PNG,(64,64))
self.image = pygame.Surface([self.width, self.height])
self.image.blit(self.ROCKS,(0,0))
self.image.set_colorkey((0, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

BIN
zdjecia/gandalf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 90 KiB

BIN
zdjecia/ork-piechota.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 183 KiB

BIN
zdjecia/ork-piechota2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 KiB

BIN
zdjecia/ork_lucznik.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

BIN
zdjecia/podloze.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

BIN
zdjecia/rock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
zdjecia/rock3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

BIN
zdjecia/sauron.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB