Helicopter, player, Fahrzeug, Ableitung

This commit is contained in:
Yadciel 2023-04-20 09:08:35 +02:00
parent 5a5550b29b
commit 032229a115
6 changed files with 154 additions and 45 deletions

View File

@ -14,10 +14,6 @@ def get_angle(p1, p2):
class Fahrzeug(pygame.sprite.Sprite): class Fahrzeug(pygame.sprite.Sprite):
def __init__(self, x, y): def __init__(self, x, y):
super().__init__() super().__init__()
self.image = pygame.image.load("player.png").convert_alpha()
self.rect = self.image.get_rect(center=(x, y))
self.rect.centerx = x
self.rect.centery = y
self.speed = 1 self.speed = 1
self.dest_x = x self.dest_x = x
self.dest_y = y self.dest_y = y
@ -25,16 +21,6 @@ class Fahrzeug(pygame.sprite.Sprite):
self.angle = 0 self.angle = 0
self.Tank = 250 self.Tank = 250
def draw(self, win):
rotated_image = pygame.transform.rotate(self.image, self.angle)
rotated_rect = rotated_image.get_rect(center=self.rect.center)
#debug, nicht anrühren
#self.angle += 1
#pygame.draw.rect(win, (255, 0, 0), rotated_rect)
win.blit(rotated_image, rotated_rect)
def update(self): def update(self):
self.move() self.move()
@ -44,33 +30,8 @@ class Fahrzeug(pygame.sprite.Sprite):
self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90 self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90
def move(self): def move(self):
if self.Tank > 0: pass
if self.rect.centerx < self.dest_x:
self.rect.centerx += self.speed
if self.rect.centerx > self.dest_x:
self.rect.centerx -= self.speed
if self.rect.centery < self.dest_y:
self.rect.centery += self.speed
if self.rect.centery > self.dest_y:
self.rect.centery -= self.speed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.centerx -= self.speed
self.dest_x -= self.speed
self.angle = 90
if keys[pygame.K_RIGHT]:
self.rect.centerx += self.speed
self.dest_x += self.speed
self.angle = 270
if keys[pygame.K_UP]:
self.rect.centery -= self.speed
self.dest_y -= self.speed
self.angle = 0
if keys[pygame.K_DOWN]:
self.rect.centery += self.speed
self.dest_y += self.speed
self.angle = 180
def dest(self, pos): def dest(self, pos):
self.dest_x, self.dest_y = pos self.dest_x, self.dest_y = pos

View File

@ -1,5 +1,6 @@
import pygame import pygame
import fahrzeug import player
import helicopter
import background import background
import tilemap import tilemap
import sys import sys
@ -7,7 +8,8 @@ import sys
def start(): def start():
pygame.init() pygame.init()
win = pygame.display.set_mode((800, 800)) win = pygame.display.set_mode((800, 800))
player1 = fahrzeug.Fahrzeug(0, 0) player1 = player.Player(0, 0)
enemy = helicopter.Helicopter(5, 5)
Erzmine = background.Erz() Erzmine = background.Erz()
Tanken = background.Tank() Tanken = background.Tank()
Ziele = background.Ziel() Ziele = background.Ziel()
@ -70,4 +72,7 @@ def start():
player1.update() player1.update()
player1.draw(win) player1.draw(win)
enemy.update(player1)
enemy.draw(win)
pygame.display.update() pygame.display.update()

BIN
helicopter.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

52
helicopter.py Normal file
View File

@ -0,0 +1,52 @@
import pygame
import player
import rotor
from fahrzeug import Fahrzeug
class Helicopter(Fahrzeug, pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(x, y)
self.image = pygame.image.load("helicopter.jpg").convert_alpha()
self.image = self.image.subsurface(44, 0, 44, 99)
self.rect = self.image.get_rect(center=(x, y))
self.rect.centerx = x
self.rect.centery = y
self.rotor = rotor.Rotor(self.rect.centerx, self.rect.centery)
self.dest_x = x
self.dest_y = y
self.rect = self.image.get_rect()
self.angle = 0
self.speed = 1
def update(self, player):
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.rect.centerx -= self.speed
self.dest_x -= self.speed
self.angle = 90
if keys[pygame.K_d]:
self.rect.centerx += self.speed
self.dest_x += self.speed
self.angle = 270
if keys[pygame.K_w]:
self.rect.centery -= self.speed
self.dest_y -= self.speed
self.angle = 0
if keys[pygame.K_s]:
self.rect.centery += self.speed
self.dest_y += self.speed
self.angle = 180
self.rotor.update(self)
def draw(self, win):
rotated_image = pygame.transform.rotate(self.image, self.angle)
rotated_rect = rotated_image.get_rect(center=self.rect.center)
#debug, nicht anrühren
#self.angle += 1
#pygame.draw.rect(win, (255, 0, 0), rotated_rect)
win.blit(rotated_image, rotated_rect)
self.rotor.draw(win)

59
player.py Normal file
View File

@ -0,0 +1,59 @@
import pygame
from fahrzeug import Fahrzeug
class Player(Fahrzeug, pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(x, y)
self.image = pygame.image.load("player.png").convert_alpha()
self.rect = self.image.get_rect(center=(x, y))
self.rect.centerx = x
self.rect.centery = y
self.speed = 1
self.dest_x = x
self.dest_y = y
self.direction = pygame.math.Vector2(0, -1)
self.angle = 0
self.Tank = 250
def draw(self, win):
rotated_image = pygame.transform.rotate(self.image, self.angle)
rotated_rect = rotated_image.get_rect(center=self.rect.center)
#debug, nicht anrühren
#self.angle += 1
#pygame.draw.rect(win, (255, 0, 0), rotated_rect)
win.blit(rotated_image, rotated_rect)
def move(self):
if self.Tank > 0:
if self.rect.centerx < self.dest_x:
self.rect.centerx += self.speed
if self.rect.centerx > self.dest_x:
self.rect.centerx -= self.speed
if self.rect.centery < self.dest_y:
self.rect.centery += self.speed
if self.rect.centery > self.dest_y:
self.rect.centery -= self.speed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.centerx -= self.speed
self.dest_x -= self.speed
self.angle = 90
if keys[pygame.K_RIGHT]:
self.rect.centerx += self.speed
self.dest_x += self.speed
self.angle = 270
if keys[pygame.K_UP]:
self.rect.centery -= self.speed
self.dest_y -= self.speed
self.angle = 0
if keys[pygame.K_DOWN]:
self.rect.centery += self.speed
self.dest_y += self.speed
self.angle = 180
def dest(self, pos):
self.dest_x, self.dest_y = pos

32
rotor.py Normal file
View File

@ -0,0 +1,32 @@
import pygame
import helicopter
from fahrzeug import Fahrzeug
class Rotor(Fahrzeug, pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(x, y)
self.image = pygame.image.load("helicopter.jpg").convert_alpha()
self.image = self.image.subsurface(132, 98, 94, 94)
self.rect = self.image.get_rect(center=(x, y))
def update(self, helicopter):
self.rect.center = helicopter.rect.center
if helicopter.angle == 0:
self.rect.centery -= 10
if helicopter.angle == 90:
self.rect.centerx -= 10
if helicopter.angle == 180:
self.rect.centery += 10
if helicopter.angle == 270:
self.rect.centerx += 10
def draw(self, win):
rotated_image = pygame.transform.rotate(self.image, self.angle)
rotated_rect = rotated_image.get_rect(center=self.rect.center)
#debug, nicht anrühren
self.angle += 1
#pygame.draw.rect(win, (255, 0, 0), rotated_rect)
win.blit(rotated_image, rotated_rect)