Most of them need pygame so download PYGAME ASAP!
def collide(x1, x2, y1, y2, w1, w2, h1, h2):
if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True
else:return False
def die(screen, score):
f=pygame.font.SysFont('Arial', 30);t=f.render('Your score was: '+str(score), True, (0, 0, 0));screen.blit(t, (10, 270));pygame.display.update();pygame.time.wait(2000);sys.exit(0)
xs = [290, 290, 290, 290, 290];ys = [290, 270, 250, 230, 210];dirs = 0;score = 0;applepos = (random.randint(0, 590), random.randint(0, 590));pygame.init();s=pygame.display.set_mode((600, 600));pygame.display.set_caption('Snake');appleimage = pygame.Surface((10, 10));appleimage.fill((0, 255, 0));img = pygame.Surface((20, 20));img.fill((255, 0, 0));f = pygame.font.SysFont('Arial', 20);clock = pygame.time.Clock()
while True:
clock.tick(10)
for e in pygame.event.get():
if e.type == QUIT:
sys.exit(0)
elif e.type == KEYDOWN:
if e.key == K_UP and dirs != 0:dirs = 2
elif e.key == K_DOWN and dirs != 2:dirs = 0
elif e.key == K_LEFT and dirs != 1:dirs = 3
elif e.key == K_RIGHT and dirs != 3:dirs = 1
i = len(xs)-1
while i >= 2:
if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)
i-= 1
if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))
if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: die(s, score)
i = len(xs)-1
while i >= 1:
xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1
if dirs==0:ys[0] += 20
elif dirs==1:xs[0] += 20
elif dirs==2:ys[0] -= 20
elif dirs==3:xs[0] -= 20 s.fill((255, 255, 255)) for i in range(0, len(xs)):
s.blit(img, (xs[i], ys[i]))
s.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));s.blit(t, (10, 10));pygame.display.update()
WIDTH = 800 HEIGHT = 600 FPS = 30 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) class Player(pygame.sprite.Sprite): # sprite for the Player def __init__(self): # this line is required to properly create the sprite pygame.sprite.Sprite.__init__(self) # create a plain rectangle for the sprite image self.image = pygame.Surface((50, 50)) self.image.fill(GREEN) # find the rectangle that encloses the image self.rect = self.image.get_rect() # center the sprite on the screen self.rect.center = (WIDTH / 2, HEIGHT / 2) def update(self): # any code here will happen every time the game loop updates self.rect.x += 5 if self.rect.left > WIDTH: self.rect.right = 0 # initialize pygame and create window pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Sprite Example") clock = pygame.time.Clock() all_sprites = pygame.sprite.Group() player = Player() all_sprites.add(player) # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False # Update all_sprites.update() # Draw / render screen.fill(BLACK) all_sprites.draw(screen) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit()
moves = ['r', 'p', 's']
player_wins = ['pr', 'rs', 'sp']
while True:
player_move = input ("Your Move!")
computer_move = random.choice(moves)
if player == 'q':
break
print("Your Move")
print("Python: " computer_move)
if player_move == computer_move:
print ("TIE!")
elif player_move + computer_move in player_wins:
print("YOU WIN!!!!!!!!!")
else print("You lose, play again")
1 import pygame, random, sys
from pygame.locals import *def collide(x1, x2, y1, y2, w1, w2, h1, h2):
if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True
else:return False
def die(screen, score):
f=pygame.font.SysFont('Arial', 30);t=f.render('Your score was: '+str(score), True, (0, 0, 0));screen.blit(t, (10, 270));pygame.display.update();pygame.time.wait(2000);sys.exit(0)
xs = [290, 290, 290, 290, 290];ys = [290, 270, 250, 230, 210];dirs = 0;score = 0;applepos = (random.randint(0, 590), random.randint(0, 590));pygame.init();s=pygame.display.set_mode((600, 600));pygame.display.set_caption('Snake');appleimage = pygame.Surface((10, 10));appleimage.fill((0, 255, 0));img = pygame.Surface((20, 20));img.fill((255, 0, 0));f = pygame.font.SysFont('Arial', 20);clock = pygame.time.Clock()
while True:
clock.tick(10)
for e in pygame.event.get():
if e.type == QUIT:
sys.exit(0)
elif e.type == KEYDOWN:
if e.key == K_UP and dirs != 0:dirs = 2
elif e.key == K_DOWN and dirs != 2:dirs = 0
elif e.key == K_LEFT and dirs != 1:dirs = 3
elif e.key == K_RIGHT and dirs != 3:dirs = 1
i = len(xs)-1
while i >= 2:
if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)
i-= 1
if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))
if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: die(s, score)
i = len(xs)-1
while i >= 1:
xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1
if dirs==0:ys[0] += 20
elif dirs==1:xs[0] += 20
elif dirs==2:ys[0] -= 20
elif dirs==3:xs[0] -= 20 s.fill((255, 255, 255)) for i in range(0, len(xs)):
s.blit(img, (xs[i], ys[i]))
s.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));s.blit(t, (10, 10));pygame.display.update()
2 # Pygame sprite Example
import pygameimport randomWIDTH = 800 HEIGHT = 600 FPS = 30 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) class Player(pygame.sprite.Sprite): # sprite for the Player def __init__(self): # this line is required to properly create the sprite pygame.sprite.Sprite.__init__(self) # create a plain rectangle for the sprite image self.image = pygame.Surface((50, 50)) self.image.fill(GREEN) # find the rectangle that encloses the image self.rect = self.image.get_rect() # center the sprite on the screen self.rect.center = (WIDTH / 2, HEIGHT / 2) def update(self): # any code here will happen every time the game loop updates self.rect.x += 5 if self.rect.left > WIDTH: self.rect.right = 0 # initialize pygame and create window pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Sprite Example") clock = pygame.time.Clock() all_sprites = pygame.sprite.Group() player = Player() all_sprites.add(player) # Game loop running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False # Update all_sprites.update() # Draw / render screen.fill(BLACK) all_sprites.draw(screen) # *after* drawing everything, flip the display pygame.display.flip() pygame.quit()
3 import random
secret = random.randrange(1, 101)
guess = 0
tries = 0
while guess != secret:
tries = tries+1
guess = int(input("make a guess"))
if guess>secret:
print ("too high")
elif guess<secret:
print ("too low")
else:
print ("Got it ;)")
print ("number of tries:", tries)
4 import random
moves = ['r', 'p', 's']
player_wins = ['pr', 'rs', 'sp']
while True:
player_move = input ("Your Move!")
computer_move = random.choice(moves)
if player == 'q':
break
print("Your Move")
print("Python: " computer_move)
if player_move == computer_move:
print ("TIE!")
elif player_move + computer_move in player_wins:
print("YOU WIN!!!!!!!!!")
else print("You lose, play again")
No comments:
Post a Comment