# coding: latin-1 # If you are commenting in icelandic you need the line above. import pygame, math, decimal, random, sys, pickle, time from decimal import* from pygame.locals import * you_lose = "You lose!" want_to_quit = "Press Q to quit" # If we want to use sprites we create a class that inherits from the Sprite class. # Each class has an associated image and a rectangle. class Asteroid(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = asteroid_image self.rect = self.image.get_rect() class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = player_image self.rect = self.image.get_rect() class Missile(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = missile_image self.rect = self.image.get_rect() # Lets load the game images and put them into variables player_image = pygame.image.load('ship_north.png') player_image_east = pygame.image.load('ship_east.png') player_image_west = pygame.image.load('ship_west.png') player_image_north = pygame.image.load('ship_north.png') player_image_south = pygame.image.load('ship_south.png') asteroid_image = pygame.image.load('invader.png') missile_image = pygame.image.load('ball3.png') background_image = pygame.image.load('bgspace_2.jpg') WHITE = (255, 255, 255) pygame.init() 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 'Group.' asteroid_list = pygame.sprite.Group() # Group to hold missiles missile_list = pygame.sprite.Group() # This is a list of every sprite. # All blocks and the player block as well. all_sprites_list = pygame.sprite.Group() for i in range(30): block = Asteroid() # Set a random location for the block block.rect.x = random.randrange(screen_width - 20) block.rect.y = random.randrange(screen_height - 160) asteroid_list.add(block) all_sprites_list.add(block) # Create a player block player = Player() player.rect.x = 320 player.rect.y = 380 player_list = pygame.sprite.Group() player_list.add(player) all_sprites_list.add(player) quitforme = False #asteroid rect pls asteroid = Asteroid # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() score = 0 previous_score = 0 def rot_center(image, rect, angle): #rotate an imagee rot_image = pygame.transform.rotate(image, angle) rot_rect = rot_image.get_rect(center=rect.center) return rot_image,rot_rect # -------- Main Program Loop ----------- # We need to check out what happens when the player hits the space bar in order to "shoot". # A new missile is created and gets it's initial position in the "middle" of the player. # Then this missile is added to the missile sprite-group and also to the all_sprites group. while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: shot = Missile() shot.rect.x = player.rect.x + 30 shot.rect.y = player.rect.y - 15 missile_list.add(shot) all_sprites_list.add(shot) Player.image = player_image_west my_font = pygame.font.SysFont("Comic Sans MS", 30) thescore = my_font.render(repr(score), 1, (255,255,0)) screen.blit(background_image,[0,0]) screen.blit(thescore, (0,0)) #screen.fill(background_image,(0,0)) #screen =pygame.display.set_mode(background_image) #gameDisplay.blit(background_image,(0,0)) # Below is another good example of Sprite and SpriteGroup functionality. # It is now enough to see if some missile has collided with some asteroid # and if so, they are removed from their respective groups. # In other words: A missile exploded and so did an asteroid. # See if the player block has collided with anything. if pygame.sprite.groupcollide(missile_list, asteroid_list, True, True): previous_score = score score += 1 #collide you lose if pygame.sprite.groupcollide(player_list, asteroid_list, True, True): #quit() quitforme = True #if player.rect.colliderect(asteroid.rect): #quit() if quitforme == True: screen.blit(background_image,[0,0]) wantoquittext = my_font.render(want_to_quit,1,(255,255,0)) textonscreen = my_font.render(you_lose,1,(255,255,0)) #moretextonscreen_YW = my_font.render(whattoprint_Youwin, 1, (255,0,255)) screen.blit(textonscreen,(300,150)) screen.blit(wantoquittext,(300,200)) if key[pygame.K_q]: quit() # Missiles move at a constant speed up the screen, towards the enemy for shot in missile_list: shot.rect.y -= 5 enemymovement = random.randrange(10) # All the enemies move down the screen at a constant speed if quitforme == False: for block in asteroid_list: block.rect.x += 1 if score > previous_score and previous_score <= 7: block.rect.x += previous_score if previous_score >= 7: block.rect.x += 7 if block.rect.x >= 700: block.rect.x = -20 block.rect.y += 30 if block.rect.y >= 400: block.rect.y = -20 # Draw all the spites player_list.draw(screen) asteroid_list.draw(screen) missile_list.draw(screen) # Limit to 60 frames per second clock.tick(60) # Go ahead and update the screen with what we've drawn. pygame.display.flip() #Movements #Tried to make the image of the player rotate #It doesnt work for some reason. #Won't even rotate key = pygame.key.get_pressed() if key[pygame.K_LEFT]: rot_center(player_image,player.rect,90) #player_image = player_image_west if key[pygame.K_RIGHT]: player_image = player_image_east player_image = pygame.transform.rotate(player_image,-90) #player_image = pygame.transform.rotate(player_image,-90) #movements rotation #movements rotation #if key[pygame.K_LEFT]: #player.rotation+=0.5*time #if player.rotation>360.0: #player.rotation-=360.0 #if key[pygame.K_RIGHT]: #player.rotation-=0.5*time #if player.rotation<0.0: #player.rotation+=360.0 if key[pygame.K_DOWN]: player_image = player_image_south if player.rect.y > 400: player.rect.y = -60 player.rect.y += 5 if key[pygame.K_UP]: player_image = player_image_north if player.rect.y < -60: player.rect.y = 400 player.rect.y -= 5 pygame.quit()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question