Init
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
# Listen-Literal mit 3 Elementen
|
||||
l = [3, "Hello", 5.0]
|
||||
print(l)
|
||||
|
||||
# Zugriff auf das erste Element der Liste
|
||||
l = [3, "Hello", 5.0]
|
||||
print(f"Das erste Element der Liste ist {l[0]}")
|
||||
|
||||
# Ändern des zweiten Elements der Liste
|
||||
l = [3, "Hello", 5.0]
|
||||
l[1] = "Hallo"
|
||||
print(f"Das zweite Element der Liste ist jetzt {l[1]}")
|
||||
|
||||
# Iteration über die Elemente einer Liste
|
||||
liste = [3, "Hello", 5.0]
|
||||
for element in liste:
|
||||
print(element)
|
||||
|
||||
# Iteration über ein Intervall
|
||||
for zahl in range(3):
|
||||
print(zahl)
|
||||
|
||||
# Typen von Listen und Intervallen
|
||||
print(type([3, "Hello", 5.0]))
|
||||
print(type(range(3)))
|
||||
|
||||
# Iteration über die Zeichen eines Strings
|
||||
for c in "Hallo":
|
||||
print(c)
|
||||
|
||||
# Konvertierung eines Strings in eine Liste
|
||||
print(list("Hallo"))
|
||||
|
||||
# Tupel-Literal mit 3 Elementen, Zugriff, Iteration
|
||||
tupel = (3, "Hello", 5.0)
|
||||
print(tupel[2])
|
||||
for element in tupel:
|
||||
print(element)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import pygame
|
||||
|
||||
# Festlegung der Konstanten
|
||||
WIDTH = 320
|
||||
HEIGHT = 240
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
BLACK = (0, 0, 0)
|
||||
|
||||
|
||||
# Hauptfunktion mit Standardstruktur eines Pygame
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
|
||||
# Initialisierung von Pygame
|
||||
def init_game():
|
||||
pygame.init()
|
||||
return pygame.display.set_mode(SIZE)
|
||||
|
||||
|
||||
# Game-Loop
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if event_handling() == False:
|
||||
break
|
||||
if update_game() == False:
|
||||
break
|
||||
draw_game(screen)
|
||||
|
||||
|
||||
# Beenden von Pygame
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
|
||||
# Behandlung der Events
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# Aktualisierung des Spiels
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
|
||||
# Zeichnen des Spiels
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# Start des Programms
|
||||
main()
|
||||
@@ -0,0 +1,34 @@
|
||||
# Gültigkeitsbereich von Variablen
|
||||
|
||||
|
||||
# 1. Beispiel: Änderungen an lokalen Variablen beeinflussen globale Variablen nicht
|
||||
|
||||
a = 1 # Globale Variable
|
||||
|
||||
def f():
|
||||
a = 2 # Lokale Variable
|
||||
|
||||
f()
|
||||
print(a)
|
||||
|
||||
|
||||
# 2. Beispiel: Das Verdecken der globalen Variable durch eine lokale Variable
|
||||
# kann durch die Verwendung des Schlüsselworts 'global' verhindert werden
|
||||
|
||||
def f():
|
||||
global a
|
||||
a = 2
|
||||
|
||||
f()
|
||||
print(a)
|
||||
|
||||
|
||||
# 3. Beispiel: Eine globale Variable kann in einer Funktion erstellt werden
|
||||
|
||||
def f():
|
||||
global b
|
||||
b = 3
|
||||
|
||||
f()
|
||||
print(b)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import pygame
|
||||
|
||||
# Festlegung der Konstanten
|
||||
WIDTH = 320
|
||||
HEIGHT = 240
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0)
|
||||
CIRCLE_RADIUS = 10
|
||||
|
||||
|
||||
# Hauptfunktion mit Standardstruktur eines Pygame
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
|
||||
# Initialisierung von Pygame
|
||||
def init_game():
|
||||
global position
|
||||
position = (WIDTH // 2, HEIGHT // 2)
|
||||
pygame.init()
|
||||
return pygame.display.set_mode(SIZE)
|
||||
|
||||
|
||||
# Game-Loop
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if event_handling() == False:
|
||||
break
|
||||
if update_game() == False:
|
||||
break
|
||||
draw_game(screen)
|
||||
|
||||
# Beenden von Pygame
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
|
||||
# Behandlung der Events
|
||||
def event_handling():
|
||||
global position
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_UP:
|
||||
position = (position[0], position[1] - 1)
|
||||
if event.key == pygame.K_DOWN:
|
||||
position = (position[0], position[1] + 1)
|
||||
if event.key == pygame.K_LEFT:
|
||||
position = (position[0] - 1, position[1])
|
||||
if event.key == pygame.K_RIGHT:
|
||||
position = (position[0] + 1, position[1])
|
||||
return True
|
||||
|
||||
|
||||
# Aktualisierung des Spiels
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
|
||||
# Zeichnen des Spiels
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# Start des Programms
|
||||
main()
|
||||
@@ -0,0 +1,96 @@
|
||||
import pygame
|
||||
|
||||
# Festlegung der Konstanten
|
||||
WIDTH = 320
|
||||
HEIGHT = 240
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
FPS = 30
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0)
|
||||
CIRCLE_RADIUS = 10
|
||||
KEY_UP = 0
|
||||
KEY_DOWN = 1
|
||||
KEY_LEFT = 2
|
||||
KEY_RIGHT = 3
|
||||
SPEED = 3
|
||||
|
||||
|
||||
# Hauptfunktion mit Standardstruktur eines Pygame
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
|
||||
# Initialisierung von Pygame
|
||||
def init_game():
|
||||
global position
|
||||
global keys_pressed
|
||||
global clock
|
||||
position = (WIDTH // 2, HEIGHT // 2)
|
||||
keys_pressed = [False, False, False, False]
|
||||
clock = pygame.time.Clock()
|
||||
pygame.init()
|
||||
return pygame.display.set_mode(SIZE)
|
||||
|
||||
|
||||
# Game-Loop
|
||||
def game_loop(screen):
|
||||
while True:
|
||||
if event_handling() == False:
|
||||
break
|
||||
if update_game() == False:
|
||||
break
|
||||
draw_game(screen)
|
||||
clock.tick(FPS)
|
||||
|
||||
|
||||
# Beenden von Pygame
|
||||
def exit_game():
|
||||
pygame.quit()
|
||||
|
||||
|
||||
# Behandlung der Events
|
||||
def event_handling():
|
||||
global keys_pressed
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
|
||||
if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
|
||||
new_value = (event.type == pygame.KEYDOWN)
|
||||
if event.key == pygame.K_UP:
|
||||
keys_pressed[KEY_UP] = new_value
|
||||
if event.key == pygame.K_DOWN:
|
||||
keys_pressed[KEY_DOWN] = new_value
|
||||
if event.key == pygame.K_LEFT:
|
||||
keys_pressed[KEY_LEFT] = new_value
|
||||
if event.key == pygame.K_RIGHT:
|
||||
keys_pressed[KEY_RIGHT] = new_value
|
||||
|
||||
return True
|
||||
|
||||
|
||||
# Aktualisierung des Spiels
|
||||
def update_game():
|
||||
global position
|
||||
if keys_pressed[KEY_UP]:
|
||||
position = (position[0], position[1] - SPEED)
|
||||
if keys_pressed[KEY_DOWN]:
|
||||
position = (position[0], position[1] + SPEED)
|
||||
if keys_pressed[KEY_LEFT]:
|
||||
position = (position[0] - SPEED, position[1])
|
||||
if keys_pressed[KEY_RIGHT]:
|
||||
position = (position[0] + SPEED, position[1])
|
||||
return True
|
||||
|
||||
|
||||
# Zeichnen des Spiels
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# Start des Programms
|
||||
main()
|
||||
Reference in New Issue
Block a user