agent poruszający się po mapie + zmiany w main

This commit is contained in:
Weranda 2023-03-29 18:11:34 +02:00
parent 46524da263
commit af8529865d
5 changed files with 100 additions and 33 deletions

Binary file not shown.

Binary file not shown.

50
agent.py Normal file
View File

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

View File

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

76
main.py
View File

@ -1,46 +1,62 @@
import pygame import pygame
import random import random
from config import * from config import *
from agent import *
pygame.init() class Game:
ARCHER_ORK_IMG = pygame.image.load("./zdjecia/ork_lucznik.jpg") def __init__(self):
INFATRY_ORK_IMG = pygame.image.load("./zdjecia/ork-piechota.jpg") pygame.init()
INFANTRY_ORK2_IMG = pygame.image.load("./zdjecia/ork-piechota2.jpg") self.SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
SAURON_JPG = pygame.image.load("./zdjecia/sauron.jpg") self.running = True
AGENT_IMG = pygame.image.load("./zdjecia/gandalf.jpg") self.clock = pygame.time.Clock()
ARCHER_ORK = pygame.transform.scale(ARCHER_ORK_IMG,(100,100)) # te wszystkie obrazki trzeba dać w oddzielnej klasie dla potworków jak coś później
INFATRY_ORK = pygame.transform.scale(INFATRY_ORK_IMG,(100,100)) self.ARCHER_ORK_IMG = pygame.image.load("./zdjecia/ork_lucznik.jpg")
INFANTRY_ORK2 = pygame.transform.scale(INFANTRY_ORK2_IMG,(100,100)) self.INFATRY_ORK_IMG = pygame.image.load("./zdjecia/ork-piechota.jpg")
SAURON = pygame.transform.scale(SAURON_JPG,(100,100)) self.INFANTRY_ORK2_IMG = pygame.image.load("./zdjecia/ork-piechota2.jpg")
AGENT = pygame.transform.scale(AGENT_IMG,(100,100)) self.SAURON_JPG = pygame.image.load("./zdjecia/sauron.jpg")
pygame.display.set_caption('Do_Nazwania')
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
def map(): self.ARCHER_ORK = pygame.transform.scale(self.ARCHER_ORK_IMG,(64,64))
SCREEN.fill(BLACK) self.INFATRY_ORK = pygame.transform.scale(self.INFATRY_ORK_IMG,(64,64))
ORK_LIST = [ARCHER_ORK,INFANTRY_ORK2, INFATRY_ORK] self.INFANTRY_ORK2 = pygame.transform.scale(self.INFANTRY_ORK2_IMG,(64,64))
for x in range(0, WIDTH, TILE_SIZE): self.SAURON = pygame.transform.scale(self.SAURON_JPG,(64,64))
for y in range(0, HEIGHT, TILE_SIZE):
rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
pygame.draw.rect(SCREEN, WHITE, rect, 1)
SCREEN.blit(AGENT, (500,1000))
def main(): self.ORK_LIST = [self.ARCHER_ORK,self.INFANTRY_ORK2, self.INFATRY_ORK]
running = True
clock = pygame.time.Clock()
map()
while running: pygame.display.set_caption('Do_Nazwania')
clock.tick(FRAMERATE)
def new(self): # tworzy się nowa sesja grania
self.all_sprites = pygame.sprite.LayeredUpdates()
self.agent = Agent(self,1,1)
def update(self): # update postaci na mapie, związane z poruszaniem się
self.all_sprites.update()
def events(self):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
self.running = False
pygame.quit() pygame.quit()
pygame.display.flip() def map(self): # tworzenie mapy
self.SCREEN.fill(BLACK)
self.all_sprites.draw(self.SCREEN)
self.clock.tick(FRAMERATE)
for x in range(0, WIDTH, TILE_SIZE):
for y in range(0, HEIGHT, TILE_SIZE):
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
pygame.draw.rect(self.SCREEN, WHITE, self.rect, 1)
pygame.display.update()
main() def main(self):
self.events()
self.update()
self.map()
g = Game()
g.new()
while g.running:
g.main()