LF08/fahrzeug.py

77 lines
2.4 KiB
Python
Raw Normal View History

2023-03-31 12:33:56 +02:00
import pygame
import math
2023-04-19 08:59:00 +02:00
def get_angle(p1, p2):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
rads = math.atan2(-dy,dx)
rads %= 2*math.pi
degs = math.degrees(rads)
return degs
2023-03-31 12:33:56 +02:00
class Fahrzeug(pygame.sprite.Sprite):
def __init__(self, x, y):
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
2023-04-19 08:59:00 +02:00
self.speed = 1
2023-03-31 12:33:56 +02:00
self.dest_x = x
self.dest_y = y
self.direction = pygame.math.Vector2(0, -1)
self.angle = 0
2023-04-19 12:23:30 +02:00
self.Tank = 250
2023-03-31 12:33:56 +02:00
def draw(self, win):
rotated_image = pygame.transform.rotate(self.image, self.angle)
rotated_rect = rotated_image.get_rect(center=self.rect.center)
2023-04-19 08:59:00 +02:00
#debug, nicht anrühren
2023-03-31 12:33:56 +02:00
#self.angle += 1
#pygame.draw.rect(win, (255, 0, 0), rotated_rect)
2023-04-19 08:59:00 +02:00
2023-03-31 12:33:56 +02:00
win.blit(rotated_image, rotated_rect)
def update(self):
2023-04-19 12:23:30 +02:00
self.move()
if self.rect.centerx != self.dest_x or self.rect.centery != self.dest_y:
if self.Tank > 0:
self.Tank -= 0.1
self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90
def move(self):
2023-04-19 08:59:00 +02:00
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
2023-04-19 12:23:30 +02:00
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
2023-04-14 12:31:54 +02:00
2023-03-31 12:33:56 +02:00
def dest(self, pos):
self.dest_x, self.dest_y = pos