Init
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import pygame
|
||||
import math
|
||||
import datetime
|
||||
|
||||
# Festlegung der Konstanten
|
||||
WIDTH = 320
|
||||
HEIGHT = 240
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
DISTANCE = 10
|
||||
CIRCLE_RADIUS = HEIGHT//2 - DISTANCE
|
||||
NUMBER_RADIUS = CIRCLE_RADIUS * 0.9
|
||||
HOURS_LENGTH = NUMBER_RADIUS * 0.6
|
||||
MINUTES_LENGTH = NUMBER_RADIUS * 0.8
|
||||
SECONDS_LENGTH = NUMBER_RADIUS * 0.9
|
||||
FONT_SIZE = 20
|
||||
FONT_NAME = None # None = default font
|
||||
BLACK = (0, 0, 0)
|
||||
WHITE = (255, 255, 255)
|
||||
RED = (255, 0, 0)
|
||||
FPS = 1
|
||||
|
||||
|
||||
# Hauptfunktion mit Standardstruktur eines Pygame
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
|
||||
# Initialisierung von Pygame
|
||||
def init_game():
|
||||
global clock
|
||||
global font
|
||||
pygame.init()
|
||||
clock = pygame.time.Clock()
|
||||
font = pygame.font.Font(FONT_NAME, FONT_SIZE)
|
||||
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()
|
||||
|
||||
|
||||
# Event-Behandlung
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# Aktualisierung des Spiels
|
||||
def update_game():
|
||||
global hours_angle
|
||||
global minuten_angle
|
||||
global seconds_angle
|
||||
now = datetime.datetime.now()
|
||||
hours = now.hour % 12
|
||||
minutes = now.minute
|
||||
seconds = now.second
|
||||
hours_angle = (hours * 60 + minutes) / 720 * 2 * math.pi
|
||||
minuten_angle = minutes / 60 * 2 * math.pi
|
||||
seconds_angle = seconds / 60 * 2 * math.pi
|
||||
return True
|
||||
|
||||
|
||||
# Zeichnen des Spiels
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
position = (WIDTH//2, HEIGHT//2)
|
||||
draw_dial(screen, position)
|
||||
draw_hands(screen, position)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# Zeichnen des Ziffernblatts
|
||||
def draw_dial(screen, position):
|
||||
global font
|
||||
# Rand
|
||||
pygame.draw.circle(screen, WHITE, position, CIRCLE_RADIUS, 1)
|
||||
# Ziffern
|
||||
for i in range(12):
|
||||
angle = i * (2 * math.pi) / 12
|
||||
y = math.cos(angle) * NUMBER_RADIUS * -1
|
||||
x = math.sin(angle) * NUMBER_RADIUS
|
||||
text = font.render(str(i or 12), True, WHITE)
|
||||
text_rect = text.get_rect(center=(WIDTH // 2 + x, HEIGHT // 2 + y))
|
||||
screen.blit(text, text_rect)
|
||||
|
||||
|
||||
# Zeichnen der Zeiger
|
||||
def draw_hands(screen, position):
|
||||
end_pos_hours = (position[0] + math.sin(hours_angle) * HOURS_LENGTH, position[1] + math.cos(hours_angle) * HOURS_LENGTH * -1)
|
||||
pygame.draw.line(screen, WHITE, position, end_pos_hours, 4)
|
||||
|
||||
end_pos_minutes = (position[0] + math.sin(minuten_angle) * MINUTES_LENGTH, position[1] + math.cos(minuten_angle) * MINUTES_LENGTH * -1)
|
||||
pygame.draw.line(screen, WHITE, position, end_pos_minutes, 2)
|
||||
|
||||
end_pos_seconds = (position[0] + math.sin(seconds_angle) * SECONDS_LENGTH, position[1] + math.cos(seconds_angle) * SECONDS_LENGTH * -1)
|
||||
pygame.draw.line(screen, RED, position, end_pos_seconds, 1)
|
||||
|
||||
|
||||
# Start des Programms
|
||||
main()
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import pygame
|
||||
|
||||
# Festlegung der Konstanten
|
||||
WIDTH = 320
|
||||
HEIGHT = 240
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
FPS = 30
|
||||
BLACK = (0, 0, 0)
|
||||
GREEN = (0, 255, 0)
|
||||
RED = (255, 0, 0)
|
||||
CIRCLE_RADIUS = 5
|
||||
MAX_SPEED = 30
|
||||
FRICTION = 0.97
|
||||
|
||||
|
||||
# Hauptfunktion mit Standardstruktur eines Pygame
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
|
||||
# Initialisierung von Pygame
|
||||
def init_game():
|
||||
global position
|
||||
global speed
|
||||
global clock
|
||||
global pushed
|
||||
position = (WIDTH // 2, HEIGHT // 2)
|
||||
speed = (0.0, 0.0)
|
||||
clock = pygame.time.Clock()
|
||||
pushed = False
|
||||
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()
|
||||
|
||||
|
||||
# Event-Behandlung
|
||||
def event_handling():
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
if not pushed and event.type == pygame.MOUSEBUTTONUP:
|
||||
button_pushed()
|
||||
return True
|
||||
|
||||
|
||||
def button_pushed():
|
||||
global speed, pushed
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
delta_x = mouse_pos[0] - position[0]
|
||||
delta_y = mouse_pos[1] - position[1]
|
||||
speed = (MAX_SPEED * delta_x / WIDTH, MAX_SPEED * delta_y / HEIGHT)
|
||||
pushed = True
|
||||
|
||||
|
||||
# Aktualisierung des Spiels
|
||||
def update_game():
|
||||
if pushed:
|
||||
calc_new_position()
|
||||
calc_new_speed()
|
||||
return True
|
||||
|
||||
|
||||
def calc_new_speed():
|
||||
global speed, pushed
|
||||
speed = (speed[0] * FRICTION, speed[1] * FRICTION)
|
||||
if abs(speed[0]) < 0.1 and abs(speed[1]) < 0.1:
|
||||
pushed = False
|
||||
speed = (0.0, 0.0)
|
||||
|
||||
|
||||
def calc_new_position():
|
||||
global speed, position
|
||||
position = (position[0] + speed[0], position[1] + speed[1])
|
||||
if position[0] < CIRCLE_RADIUS or position[0] + CIRCLE_RADIUS >= WIDTH:
|
||||
speed = (-speed[0], speed[1])
|
||||
position = (position[0] + speed[0], position[1] + speed[1])
|
||||
if position[1] < CIRCLE_RADIUS or position[1] + CIRCLE_RADIUS >= HEIGHT:
|
||||
speed = (speed[0], -speed[1])
|
||||
position = (position[0] + speed[0], position[1] + speed[1])
|
||||
|
||||
|
||||
# Zeichnen des Spiels
|
||||
def draw_game(screen):
|
||||
screen.fill(GREEN)
|
||||
pygame.draw.circle(screen, BLACK, position, CIRCLE_RADIUS)
|
||||
if not pushed:
|
||||
mouse_pos = pygame.mouse.get_pos()
|
||||
if mouse_pos[0] > 0 and mouse_pos[0] < WIDTH-1 and mouse_pos[1] > 0 and mouse_pos[1] < HEIGHT-1:
|
||||
pygame.draw.line(screen, RED, position, mouse_pos, 2)
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
|
||||
# Start des Programms
|
||||
main()
|
||||
@@ -0,0 +1,70 @@
|
||||
import pygame
|
||||
|
||||
# Festlegung der Konstanten
|
||||
WIDTH = 320
|
||||
HEIGHT = 240
|
||||
SIZE = (WIDTH, HEIGHT)
|
||||
BLACK = (0, 0, 0)
|
||||
RED = (255, 0, 0)
|
||||
|
||||
|
||||
# Hauptfunktion mit Standardstruktur eines Pygame
|
||||
def main():
|
||||
screen = init_game()
|
||||
game_loop(screen)
|
||||
exit_game()
|
||||
|
||||
|
||||
# Initialisierung von Pygame
|
||||
def init_game():
|
||||
global clicks
|
||||
clicks = []
|
||||
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()
|
||||
|
||||
|
||||
# Event-Behandlung
|
||||
def event_handling():
|
||||
global clicks
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return False
|
||||
if event.type == pygame.MOUSEBUTTONUP:
|
||||
pos = pygame.mouse.get_pos()
|
||||
clicks.append(pos)
|
||||
return True
|
||||
|
||||
|
||||
# Aktualisierung des Spiels
|
||||
def update_game():
|
||||
return True
|
||||
|
||||
|
||||
# Zeichnen des Spiels
|
||||
def draw_game(screen):
|
||||
screen.fill(BLACK)
|
||||
last_pos = None
|
||||
for pos in clicks:
|
||||
if last_pos is not None:
|
||||
pygame.draw.line(screen, RED, last_pos, pos, 2)
|
||||
last_pos = pos
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# Start des Programms
|
||||
main()
|
||||
@@ -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