Übersetzungen dieser Seite:

Raphael

Lernen

Code

raphael1.py
# -*- coding: utf-8 -*-
import turtle as t
t.speed(0)
t.setpos(-10,0)
t.left(20)
for du in range(75):
   t.fd(10)
   t.rt(45)
   t.fd(du)
t.penup()#nicht mehr zeichnen
t.setpos(-100,-10)
t.seth(270)
t.pendown() #wieder zeichnen
t.speed(3)
t.circle(110)  
t.penup()#nicht mehr zeichnen
t.setpos(0,0)
t.pendown() #wieder zeichnen
for ihm in range(8):
  t.fd(100)
  t.back(100)
  t.rt(45)
raphael2.py
# -*- coding: utf-8 -*-
"""
part2step005_ball_with_fixed_movement
 
bouncing ball. each frame the complete screen is filled with the background,
making this example simple to code but possible slow on larger resolutions.
Each frame, a random-coloured circle is drawn with randomized radius directly on the screen.
Try to manipulate the display.set_mode values to change the resolution."""
import pygame
import random
pygame.init()
screen=pygame.display.set_mode((640,480)) # try out larger values and see what happens !
background = pygame.Surface(screen.get_size())
background.fill((random.randint(0,255)
                ,random.randint(0,255) 
                ,random.randint(0,255)))     # (red,green,blue)
background = background.convert()
ball1 = pygame.Surface((50,50))     #create a new surface (black by default)
ball2 = pygame.Surface((50,50)) 
ball3 = pygame.Surface((50,50)) 
ball1.set_colorkey((0,0,0))         #make black the transparent color (red,green,blue)
ball2.set_colorkey((0,0,0))
ball3.set_colorkey((0,0,0))
#pygame.draw.circle(Surface, color, pos, radius, width=0)
pygame.draw.circle(ball1, (0,0,255), (25,25),25) # paint blue circle
pygame.draw.circle(ball2, (0,0,255), (25,25),25)
pygame.draw.circle(ball3, (0,0,255), (25,25),25)
ball1 = ball1.convert_alpha()       # if you use tranparent colors you need convert_alpha()
ball2 = ball2.convert_alpha()
ball3 = ball3.convert_alpha()
ball1x, ball1y = 0, 750           # start position of the ball (x,y)
ball2x, ball2y = 0, 0
ball3x, ball3y = 750, 350
dx1, dy1 = random.randint(5,10), random.randint(5,10)                 # speed vector of the ball in pixel per frame
dx2, dy2 = random.randint(5,10), random.randint(5,10)
dx3, dy3 = random.randint(5,10), random.randint(5,10)
screen.blit(background, (0,0))     #draw background on screen (overwriting all)
screen.blit(ball1, (ball1x, ball1y))  #draw the topleft corner of ball surface at pos (ballx, bally)
screen.blit(ball2, (ball2x, ball2y))
screen.blit(ball3, (ball3x, ball3y))
clock = pygame.time.Clock()
mainloop = True
zeit=0
FPS = 30 # desired framerate in frames per second. 
while mainloop:
    # do all this each frame
    milliseconds = clock.tick(FPS) # do not go faster than this framerate
    zeit+=milliseconds
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False # pygame window closed by user
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False # user pressed ESC
    pygame.display.set_caption("FPS: %.2f X: %.2f Y: %.2f dx: %.2f dy:"
                               " %.2f" % (clock.get_fps(), ball1x, ball1y, dx1, dy1))
    # if zeit > random.randint(15000,20000): 
        # zeit=0
        # background.fill((random.randint(0,255)
                       # ,random.randint(0,255) 
                       # ,random.randint(0,255)))
        # screen.blit(background, (0,0))     #draw background on screen (overwriting all)
    #inside mainloop, after screen.blit(background,(0,0))
    #colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
    #radius = random.randint(50,55)
    #pygame.draw.circle(screen, colour , (100,100), radius, 2) # draw pulsating circle
    #calculate new center of ball 
    ball1x += dx1
    ball2x += dx2
    ball3x += dx3
    ball1y += dy1
    ball2y += dy2
    ball3y += dy3
    if (( abs(ball1x - ball2x) < 25 and abs(ball1y - ball2y) < 25)  or ( abs(ball1x - ball3x) < 25 and abs(ball1y - ball3y) < 25) or ( abs(ball2x - ball3x) < 25 and abs(ball2y - ball3y) < 25)):
		   background.fill((random.randint(0,255) ,random.randint(0,255) ,random.randint(0,255)))
		   screen.blit(background, (0,0))
 
    dx1+= random.randint(-1,1)
    dx2+= random.randint(-1,1)
    dx3+= random.randint(-1,1)
    dy1+= random.randint(-1,1) 
    dy2+= random.randint(-1,1)
    dy3+= random.randint(-1,1)
    # bounce ball if out of screen
    if ball1x < 0:
        ball1x = 0
        #dx *= -1 
        dx1 =random.randint(5,10)
    if ball2x < 0:
        ball2x = 0 
        dx2 =random.randint(5,10)
    if ball3x < 0:
        ball3x = 0 
        dx3 =random.randint(5,10)
    if ball1x + ball1.get_width() > screen.get_width():
        ball1x = screen.get_width() - ball1.get_width()
        #dx *= -1
        dx1= -1*random.randint(5,10)
    if ball2x + ball2.get_width() > screen.get_width():
        ball2x = screen.get_width() - ball2.get_width()
        dx2= -1*random.randint(5,10)
    if ball3x + ball3.get_width() > screen.get_width():
        ball3x = screen.get_width() - ball3.get_width()
        dx3= -1*random.randint(5,10)
    if ball1y < 0:
        ball1y = 0
        #dy *= -1
        dy1 =random.randint(5,10)
    if ball2y < 0:
        ball2y = 0
        dy2 =random.randint(5,10)
    if ball3y < 0:
        ball3y = 0
        dy3 =random.randint(5,10)
    if ball1y + ball1.get_height() > screen.get_height():
        ball1y = screen.get_height() - ball1.get_height()
        #dy *= -1
        dy1= -1*random.randint(5,10)
    if ball2y + ball2.get_height() > screen.get_height():
        ball2y = screen.get_height() - ball2.get_height()
        dy2= -1*random.randint(5,10)
    if ball3y + ball3.get_height() > screen.get_height():
        ball3y = screen.get_height() - ball3.get_height()
        dy3= -1*random.randint(5,10)
 
    # paint the ball    
    pygame.draw.circle(ball1, (random.randint(200,255),random.randint(100,200),random.randint(0,100)), (25,25),25)
    pygame.draw.circle(ball2, (random.randint(0,100),random.randint(100,200),random.randint(200,255)), (25,25),25)
    pygame.draw.circle(ball3, (random.randint(100,200),random.randint(200,255),random.randint(0,100)), (25,25),25)
    screen.blit(ball1, (round(ball1x,0), round(ball1y,0)))    
    screen.blit(ball2, (round(ball2x,0), round(ball2y,0)))
    screen.blit(ball3, (round(ball3x,0), round(ball3y,0)))
    pygame.display.flip()          # flip the screen FPS times a second

SINUS & COSINUS

raphael2.py
# -*- coding: utf-8 -*-
"""
part2step011-rotozoom.py
 
loading the images snake.gif from a subfolder called 'data'
The subfolder must be inside the same folder as the program itself. 
The snake surface can be moved with the cursor keys, 
rotated with a and d key and and zoomed with w and s key.
"""
import pygame
import os
import sys
import math
try:
    # load from subfolder 'data'
    background = pygame.image.load(os.path.join("data","background640x480_a.jpg"))
    snake = pygame.image.load(os.path.join("data","snake.gif"))
except:
    sys.exit("Unable to find the images in the folder 'data' :-( ")  
#finally:
pygame.init()
screen=pygame.display.set_mode((640,480)) # try out larger values and see what happens !
background = background.convert()  # jpg can not have transparency
snake = snake.convert_alpha()      # png image has transparent color 
snake_original = snake.copy()      # store a unmodified copy of the snake surface
snakex, snakey = 250, 240            # start position of snake surface
dx, dy  = 0, 0                   # snake speed in pixel per second !
speed = 0                       # in pixel / second
angle = 0                        # current orientation of snake
zoom = 1.0                       # current zoom factor
zoomspeed = 0.01                   
turnspeed = 30                  # in Grad (360) per second
screen.blit(background, (0,0))     # blit background on screen (overwriting all)
screen.blit(snake, (snakex, snakey))  # blit the snake shape 
clock = pygame.time.Clock()        # create pygame clock object 
mainloop = True
FPS = 60                           # desired max. framerate in frames per second. 
while mainloop:
    milliseconds = clock.tick(FPS)  # milliseconds passed since last frame
    seconds = milliseconds / 1000.0 # seconds passed since last frame
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False # pygame window closed by user
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False # user pressed ESC
    pygame.display.set_caption("press cursor keys and w a s d - fps:"
        "%.2f zoom: %.2f angle %.2f" % (clock.get_fps(), zoom, angle))
    # only blit the part of the background where the snake was (cleanrect)
    try:
        #if the subsurface is outside the screen pygame would raise an error
        #this can happen when using rotozoom, therfore check inside try..except
        dirtyrect = background.subsurface((round(snakex,0), 
                round(snakey,0), snake.get_width(), snake.get_height()))
 
        screen.blit(dirtyrect, (round(snakex,0), round(snakey,0))) 
    except:
        screen.blit(background,(0,0)) # blit the whole background (slow but secure)
    # move snake with cursor keys
    pressedkeys = pygame.key.get_pressed()
    dx, dy  = 0, 0   # no cursor key, no movement
    angleradiant = angle * (math.pi * 2.0) / 180.0
    print angle
    dx += math.cos(angleradiant) * speed
    dy += math.sin(angleradiant) * speed
 
    # if pressedkeys[pygame.K_LEFT]:
        # dx -= speed
    # if pressedkeys[pygame.K_RIGHT]:
        # dx += speed
    if pressedkeys[pygame.K_UP]:
        speed += 2
    if pressedkeys[pygame.K_DOWN]:
        speed -= 2
    if speed < 0:
        speed =0    
 
    #calculate new center of snake 
    snakex += dx * seconds # time based movement
    snakey += dy * seconds
    # rotate snake with a and d key
    turnfactor = 0  # neither a nor d, no turning
    #if pressedkeys[pygame.K_a]:
        #turnfactor += 1 # counter-clockwise
    if pressedkeys[pygame.K_LEFT]:
        turnfactor += 1 
   # if pressedkeys[pygame.K_d]:
       # turnfactor -= 1 #clock-wise
    if pressedkeys[pygame.K_RIGHT]:
        turnfactor -= 1     
    # zoom snake with w and s key
    zoomfactor = 1.0 # neither w nor s, no zooming
    if pressedkeys[pygame.K_w]:
        zoomfactor += zoomspeed
    if pressedkeys[pygame.K_s]:
        zoomfactor -= zoomspeed
    if turnfactor != 0 or zoomfactor !=1.0:
        angle += turnfactor * turnspeed * seconds # time-based turning
        if angle > 360:
			angle -= 360
        if angle < -360:
            angle += 360
        zoom *= zoomfactor 
        # the surface shrinks and zooms and moves by rotating
        oldrect = snake.get_rect() # store current surface rect
        snake = pygame.transform.rotozoom(snake_original, angle, zoom)
        newrect = snake.get_rect() # store new surface rect
        # put new surface rect center on same spot as old surface rect center
        snakex += oldrect.centerx - newrect.width / 2
        snakey += oldrect.centery - newrect.height / 2
    # paint the snake    
    screen.blit(snake, (round(snakex,0), round(snakey,0)))    
    pygame.display.flip()          # flip the screen 30 times a second                # flip the screen 30 (or FPS) times a second

Raumschiffspiel01


de/personen/raphael/start.txt · Zuletzt geändert: 2010/04/29 18:05 (Externe Bearbeitung)