This commit is contained in:
Yadciel 2023-03-27 08:58:12 +02:00
parent cc7cd5082c
commit 0dd7e66008
10 changed files with 169 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__/

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

10
.idea/TruckSimulator.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (TruckSimulator)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/TruckSimulator.iml" filepath="$PROJECT_DIR$/.idea/TruckSimulator.iml" />
</modules>
</component>
</project>

44
game.py Normal file
View File

@ -0,0 +1,44 @@
import pygame
import player
import sys
def start():
pygame.init()
win = pygame.display.set_mode((800, 800))
player1 = player.Player(0, 0)
# Farben
white = (255, 255, 255)
blue = (0, 0, 255)
# Tilemap
tile_size = 16
map_width = 200
map_height = 200
tilemap = [[0 for y in range(map_height)] for x in range(map_width)]
for x in range(map_width):
for y in range(map_height):
tilemap[x][y] = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
player1.dest(pygame.mouse.get_pos())
# Hintergrundfarbe
win.fill(white)
# Tilemap zeichnen
for x in range(map_width):
for y in range(map_height):
if tilemap[x][y] == 1:
pygame.draw.rect(win, blue, (x * tile_size, y * tile_size, tile_size, tile_size))
player1.update()
player1.draw(win)
pygame.display.update()

51
main.py Normal file
View File

@ -0,0 +1,51 @@
import pygame
import sys
import game
pygame.init()
win = pygame.display.set_mode((500, 500))
# Farben
white = (255, 255, 255)
black = (0, 0, 0)
gray = (128, 128, 128)
# Rechtecke
start_button = pygame.Rect(100, 100, 100, 50)
quit_button = pygame.Rect(300, 100, 100, 50)
# Schriftart
font = pygame.font.Font(None, 32)
# Text
start_text = font.render("Start", True, black)
quit_text = font.render("Beenden", True, black)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
# Start-Button
if start_button.collidepoint(mouse_pos):
game.start()
# Beenden-Button
if quit_button.collidepoint(mouse_pos):
pygame.quit()
sys.exit()
# Hintergrundfarbe
win.fill(white)
# Buttons zeichnen
pygame.draw.rect(win, gray, start_button)
win.blit(start_text, (start_button.x + 25, start_button.y + 10))
pygame.draw.rect(win, gray, quit_button)
win.blit(quit_text, (quit_button.x + 10, quit_button.y + 10))
pygame.display.update()

BIN
player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

36
player.py Normal file
View File

@ -0,0 +1,36 @@
import pygame
import math
class Player(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()
self.rect.x = x
self.rect.y = y
self.speed = 2
self.dest_x = x
self.dest_y = y
self.direction = pygame.math.Vector2(1, 0)
def draw(self, win):
win.blit(self.image, self.rect)
def update(self):
if self.rect.x < self.dest_x:
self.rect.x += self.speed
if self.rect.x > self.dest_x:
self.rect.x -= self.speed
if self.rect.y < self.dest_y:
self.rect.y += self.speed
if self.rect.y > self.dest_y:
self.rect.y -= self.speed
direction = pygame.math.Vector2(self.dest_x - self.rect.centerx, self.dest_y - self.rect.centery)
angle = -math.degrees(math.atan2(direction.y, direction.x))
self.image = pygame.transform.rotate(self.image, angle)
self.rect = self.image.get_rect(center=self.rect.center)
self.direction = direction.normalize()
def dest(self, pos):
self.dest_x, self.dest_y = pos