PyGame Anfänger braucht Hilfe :D

Hier werden alle anderen GUI-Toolkits sowie Spezial-Toolkits wie Spiele-Engines behandelt.
Antworten
bonzaki
User
Beiträge: 1
Registriert: Samstag 28. Januar 2012, 10:18

Hallo Com, ich habe neulich mit der Programmiersprache PyGame und somit auch mit Python angefangen. Ich wusste garnicht was ich machen sollte, jedoch habe ich mir dann ein Muster aus dem Internet gedownloaded. Ich wollte nun das das Bild Spieleroben.gif mit pygame.transform.rotate jeweils mit den Pfeiltasten drehen, heißt wenn ich nach links drücke dreht sich das Bild um 90° nach rechts & andersrum nach links, und natürlich 180° nach unten. Danke für eure Hilfe. Ich währe sehr lieb von euch wenn ich meinen Code kopieren würdet und dann funktionierend wieder einfügen könntet. Wenn ihr wollt könnt ihr mir auch noch einen Musikbefehel einbinden, sodass später Musik im Hintergrund läuft. :D :D :D







Code: Alles auswählen

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu
 
import pygame
import random

# Define some colors
black    = (   0,   0,   0)
white    = ( 255, 255, 255)
red      = ( 255,   0,   0)
 
# This class represents the ball        
# It derives from the "Sprite" class in Pygame
class Block(pygame.sprite.Sprite):
     
    # Constructor. Pass in the color of the block, 
    # and its x and y position
    def __init__(self, color, filename):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self) 
 
        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(black)
 
        # Fetch the rectangle object that has the dimensions of the image
        # image.
        # Update the position of this object by setting the values 
        # of rect.x and rect.y
        self.rect = self.image.get_rect()
 
# Initialize Pygame
pygame.init()
 
# Set the height and width of the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])
 
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'RenderPlain.'
block_list = pygame.sprite.RenderPlain()
 
# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.RenderPlain()
player_image="SpielerOben.gif"
 
for i in range(20):
    # This represents a block
    block = Block(black, "gegner.gif")
 
    # Set a random location for the block
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)
     
    # Add the block to the list of objects
    block_list.add(block)
    all_sprites_list.add(block)
     
# Create a red player block
player = Block(red, player_image)
all_sprites_list.add(player)
 
#Loop until the user clicks the close button.
done=False
 
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
 
score = 0
x_speed=0
y_speed=0
 
# Current position
x_coord=10
y_coord=10
 
# -------- Main Program Loop -----------
while done==False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
 
    # Clear the screen
    screen.fill(black)
 
    # Get the current mouse position. This returns the position
    # as a list of two numbers.
    if event.type == pygame.KEYDOWN:
            # abhaengig von der Taste, bewegt sich das Objekt
            if event.key == pygame.K_LEFT:
                x_speed=-5
            if event.key == pygame.K_RIGHT:
                x_speed=5
                rotated_image=pygame.transform.rotate(player_image, 90)
                
            if event.key == pygame.K_UP:
                y_speed=-5
            if event.key == pygame.K_DOWN:
                y_speed=5
                 
        # Wenn der User die Taste loslaesst
    if event.type == pygame.KEYUP:
            # If it is an arrow key, reset vector back to zero
            if event.key == pygame.K_LEFT:
                x_speed=0
            if event.key == pygame.K_RIGHT:
                x_speed=0
            if event.key == pygame.K_UP:
                y_speed=0
            if event.key == pygame.K_DOWN:
                y_speed=0
 

     
    x_coord=x_coord+x_speed
    y_coord=y_coord+y_speed

     
    # Fetch the x and y out of the list, 
       # just like we'd fetch letters out of a string.
    # Set the player object to the mouse location
    player.rect.x=x_coord
    player.rect.y=y_coord


        
    # See if the player block has collided with anything.
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)  
     
    # Check the list of collisions.
    if len(blocks_hit_list) > 0:
        score +=len(blocks_hit_list)
        print( score )
         
    # Draw all the spites
    all_sprites_list.draw(screen)
     
    # Limit to 20 frames per second
    clock.tick(20)
 
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
 
pygame.quit()
deets

bonzaki hat geschrieben: Ich währe sehr lieb von euch wenn ich meinen Code kopieren würdet und dann funktionierend wieder einfügen könntet. Wenn ihr wollt könnt ihr mir auch noch einen Musikbefehel einbinden, sodass später Musik im Hintergrund läuft. :D :D :D
Sollen wir auch gleich ein Spiel draus machen das ungefaehr so cool ist wie Crysis 2? Ich frag nur, weil es fuer uns einfacher ist, wenn wir wissen, wohin du eigentlich willst.
Benutzeravatar
/me
User
Beiträge: 3555
Registriert: Donnerstag 25. Juni 2009, 14:40
Wohnort: Bonn

bonzaki hat geschrieben:Hallo Com, ich habe neulich mit der Programmiersprache PyGame und somit auch mit Python angefangen. Ich wusste garnicht was ich machen sollte, jedoch habe ich mir dann ein Muster aus dem Internet gedownloaded. Ich wollte nun das das Bild Spieleroben.gif mit pygame.transform.rotate jeweils mit den Pfeiltasten drehen, heißt wenn ich nach links drücke dreht sich das Bild um 90° nach rechts & andersrum nach links, und natürlich 180° nach unten.
Glaub mir, dein Ansatz funktioniert so nicht. Damit meine ich nicht das Vorhaben, sondern die Vorgehensweise.

Du kannst nicht ohne die geringste Ahnung von Python ein Programm damit schreiben. Alles was wir dir erklären könnten bevor du zumindest die elementaren Konzepte aus dem Tutorial verstanden hast, würdest du nur sehr schwer - wenn überhaupt - verstehen können.

Um direkt vorne anzufangen: PyGame ist keine Programmiersprache.
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

deets hat geschrieben:
bonzaki hat geschrieben: Ich währe sehr lieb von euch wenn ich meinen Code kopieren würdet und dann funktionierend wieder einfügen könntet. Wenn ihr wollt könnt ihr mir auch noch einen Musikbefehel einbinden, sodass später Musik im Hintergrund läuft. :D :D :D
Sollen wir auch gleich ein Spiel draus machen das ungefaehr so cool ist wie Crysis 2?
Quatsch! SCNR!

@bonzaki: Dein Skript läuft nicht, weil die Grafikdateien fehlen. Es ist schwer dir zu helfen wenn du es einem so schwer machst. Und mit dem "und hey, ihr könnt auch noch umsonst das für mich machen" wirst du dir hier eher keine Freunde machen.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Antworten