Drehung¶
Automatisches Drehen eines Rechtecks¶
rotate_a_square_automatisch.py
import pygame as py
py.init()
screen = py.display.set_mode((200, 200))
clock = py.time.Clock()
surface = py.Surface((50 , 50), py.SRCALPHA)
surface.fill((0, 0, 0))
rotated_surface = surface
rect = surface.get_rect()
angle = 0
is_running = True
while is_running:
for event in py.event.get():
if event.type == py.QUIT:
is_running = False
screen.fill((255, 255, 255))
angle += 5
rotated_surface = py.transform.rotate(surface, angle)
rect = rotated_surface.get_rect(center = (100, 100))
screen.blit(rotated_surface, (rect.x, rect.y))
py.display.update()
clock.tick(30)
py.quit()
Drehen eines Rechtecks mit den Tasten¶
rotate_a_square_with_key.py
import pygame as py
from pygame.constants import *
# R G B
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
DARKGRAY = ( 40, 40, 40)
py.init()
# Fenstergroesse
board_length = 800
board_height = 600
# Das Fenster erstellen
screen = py.display.set_mode((board_length, board_height), 0, 32)
py.display.set_caption('Rotation')
class My_rect(py.sprite.Sprite):
"""This class represents the ball."""
def __init__(self, x_pos = 200, y_pos = 200, lenge = 100, breite = 100, color = [0,255,0]):
super().__init__()
self.image = py.Surface([lenge, breite], py.SRCALPHA, 32)
self.image = self.image.convert_alpha()
self.image.fill(color)
self.rect = self.image.get_rect()
self.rot_image = self.image
self.angle = 0
self.set_pos(x_pos, y_pos)
def set_pos(self, x, y):
self.rect.x = x
self.rect.y = y
def add_pos(self, x, y):
self.rect.x = self.rect.x + x
self.rect.y = self.rect.y + y
def rot_rect(self, add_angle):
center_coordinate = self.rect.center
self.angle = (self.angle + add_angle) % 360
self.rot_image = py.transform.rotate(self.image, self.angle)
self.rect = self.rot_image.get_rect()
self.rect.center = center_coordinate
def draw(self):
screen.blit(self.rot_image, self.rect)
def key_handler():
key = py.key.get_pressed()
if key[py.K_LEFT]:
chuny_rect.rot_rect(-5)
if key[py.K_RIGHT]:
chuny_rect.rot_rect(5)
if key[py.K_d]:
chuny_rect.add_pos(5, 0)
if key[py.K_a]:
chuny_rect.add_pos(-5, 0)
if key[py.K_w]:
chuny_rect.add_pos(0, -5)
if key[py.K_s]:
chuny_rect.add_pos(0, 5)
FPS = 60 # frames per second setting
fps_clock = py.time.Clock()
is_running = True
chuny_rect = My_rect()
group_rect = py.sprite.Group()
group_rect.add(chuny_rect)
while is_running: # main game loop
screen.fill(WHITE)
for event in py.event.get():
if event.type == py.QUIT or py.key.get_pressed()[py.K_ESCAPE]:
is_running = False
key_handler()
chuny_rect.draw()
py.display.update()
fps_clock.tick(FPS)
py.quit()
print("Programm beendet.")
Drehen eines Raumschiffs mit den Tasten¶
Note
Für die folgenden Code Snippets werden diese Raumschiffbilder benötigt.
ship_rot_with_key.py
import pygame as py
####################################################
# Klassen für die Objekte #
####################################################
class Ship(py.sprite.Sprite):
"""This class represents the Space-Ship."""
def __init__(self, x_pos = 200, y_pos = 200, lenge = 100, breite = 100, color = [0,255,0]):
super().__init__()
self.animation = load_images("res/ship/","ship0",".gif",4)
self.image = self.animation[0]
self.costume = 0
self.rect = self.image.get_rect()
self.rot_image = self.image
self.angle = 0
self.set_pos(x_pos, y_pos)
def set_pos(self, x, y):
self.rect.x = x
self.rect.y = y
def add_pos(self, x, y):
self.rect.x = self.rect.x + x
self.rect.y = self.rect.y + y
def update(self):
self.costume = (self.costume + 1) % 4
self.image = self.animation[self.costume]
self.rot_rect(0)
def rot_rect(self, add_angle):
center_coordinate = self.rect.center
self.angle = (self.angle + add_angle) % 360
self.rot_image = py.transform.rotate(self.image, self.angle)
self.rect = self.rot_image.get_rect()
self.rect.center = center_coordinate
def draw(self):
screen.blit(self.rot_image, self.rect)
####################################################
# Hilfreiche Funktionen #
####################################################
def key_handler():
key = py.key.get_pressed()
if key[py.K_LEFT]:
space_ship.rot_rect(-rotation_speed)
if key[py.K_RIGHT]:
space_ship.rot_rect(rotation_speed)
if key[py.K_d]:
space_ship.add_pos(5, 0)
if key[py.K_a]:
space_ship.add_pos(-5, 0)
if key[py.K_w]:
space_ship.add_pos(0, -5)
if key[py.K_s]:
space_ship.add_pos(0, 5)
def load_images(path, names, ending, number):
file_names = []
animation = []
for i in range(number):
file_names.append(path + names + str(i) + ending)
animation.append(py.image.load(file_names[i]).convert_alpha())
return animation
####################################################
# Start des Programms #
####################################################
py.init()
# Fenstergroesse und Konstanten
board_length = 800
board_height = 600
rotation_speed = 5
# Das Fenster erstellen
screen = py.display.set_mode((board_length, board_height), 0, 32)
py.display.set_caption('Ship rotate with Arrow-Keys')
FPS = 60 # frames per second setting
fps_clock = py.time.Clock()
is_running = True
space_ship = Ship()
group_rect = py.sprite.Group()
group_rect.add(space_ship)
while is_running:
screen.fill([255, 255, 255])
for event in py.event.get():
if event.type == py.QUIT or py.key.get_pressed()[py.K_ESCAPE]:
is_running = False
key_handler()
space_ship.update()
space_ship.draw()
py.display.update()
fps_clock.tick(FPS)
py.quit()
print("Programm beendet.")
Drehen eines Raumschiffs mit der Maus¶
ship_rot_with_mouse.py
import pygame as py
####################################################
# Klassen für die Objekte #
####################################################
class Ship(py.sprite.Sprite):
"""This class represents the Space-Ship."""
def __init__(self, x_pos = 200, y_pos = 200, lenge = 100, breite = 100, color = [0,255,0]):
super().__init__()
self.animation = load_images("res/ship/","ship0",".gif",4)
self.image = self.animation[0]
self.costume = 0
self.rect = self.image.get_rect()
self.rot_image = self.image
self.set_pos(x_pos, y_pos)
#self.mask = py.mask.from_surface(self.image)
def set_pos(self, x, y):
self.rect.x = x
self.rect.y = y
def add_pos(self, x, y):
self.rect.x = self.rect.x + x
self.rect.y = self.rect.y + y
def update(self):
self.costume = (self.costume + 1) % 4
self.image = self.animation[self.costume]
self.rot_rect()
def rot_rect(self):
center_coordinate = self.rect.center
self.rot_image = py.transform.rotate(self.image, self.calc_angle())
self.rect = self.rot_image.get_rect()
self.rect.center = center_coordinate
def calc_angle(self):
mouse = py.Vector2(py.mouse.get_pos())
ship = py.Vector2(self.rect.center)
return -(ship - mouse).as_polar()[1] + 90
def draw(self):
screen.blit(self.rot_image, self.rect)
####################################################
# Hilfreiche Funktionen #
####################################################
def key_handler():
key = py.key.get_pressed()
if key[py.K_d]:
space_ship.add_pos(5, 0)
if key[py.K_a]:
space_ship.add_pos(-5, 0)
if key[py.K_w]:
space_ship.add_pos(0, -5)
if key[py.K_s]:
space_ship.add_pos(0, 5)
def load_images(path, names, ending, number):
file_names = []
animation = []
for i in range(number):
file_names.append(path + names + str(i) + ending)
animation.append(py.image.load(file_names[i]).convert_alpha())
return animation
####################################################
# Start des Programms #
####################################################
py.init()
# Fenstergroesse und Konstanten
board_length = 800
board_height = 600
rotation_speed = 5
# Das Fenster erstellen
screen = py.display.set_mode((board_length, board_height), 0, 32)
py.display.set_caption('Ship rotate around the Mouse')
FPS = 60
fps_clock = py.time.Clock()
is_running = True
space_ship = Ship()
group_rect = py.sprite.Group()
group_rect.add(space_ship)
while is_running:
screen.fill([255, 255, 255])
for event in py.event.get():
if event.type == py.QUIT or py.key.get_pressed()[py.K_ESCAPE]:
is_running = False
key_handler()
space_ship.update()
space_ship.draw()
py.display.update()
fps_clock.tick(FPS)
py.quit()
print("Programm beendet.")