前几天在家没事干,花了点心思折腾了两个小东西。第一个射击游戏是跟着pygame官网上下载的一个游戏边学习源码边抄的,抄了个开头就变成自己乱搞加参考了。。虽然有心想装下蒜,但不知道那些协议什么的怎么搞。只能在这提一下了。。
第二个是以前小时候玩过的,不知道该怎么叫,是看cs50公开课时课上的作业,他们叫the game of fifteen...。话说我们这边的学习能力真心普遍弱爆了。人家才上到3周课就干这个了,我们学校到了期末了有些人老师手把手教都还不会。。
第一个:
如图,白色方块是自己的飞机,白色小点是自己子弹,三条命。撞子弹就扣一条,撞敌人没做。。。
代码:
#! /usr/bin/env python #My Space Shooter #qq:289976627 gmail:[email protected]. #Import import os, sys, pygame, random from pygame.locals import * pygame.init() pygame.display.set_caption("My Space Shooter") screen = pygame.display.set_mode((800, 600)) pygame.mouse.set_visible(0) #Background background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((100, 100, 255)) #Music # TODO:Add music #Plane image def get_plane(rect, color = (0, 0, 0)): surface = pygame.Surface(rect).convert() surface.fill(color) rect = surface.get_rect() return surface, rect #Player class Player(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image, self.rect = get_plane((20,20), (255,255,255)) self.rect.center = (400, 500) self.dx = 0 self.dy = 0 self.bullettimer = 0 self.firespeed = 25 self.life = 3 def update(self): self.rect.move_ip((self.dx, self.dy)) #Fire the bullet if self.bullettimer < self.firespeed: self.bullettimer += 1 key = pygame.key.get_pressed() if key[pygame.K_SPACE] and self.bullettimer == self.firespeed: bulletSprites.add(Bullet(self.rect.midtop)) self.bullettimer = 0 #Player Boundaries if self.rect.left < 0: self.rect.left = 0 elif self.rect.right > 800: self.rect.right = 800 if self.rect.top <= 260: self.rect.top = 260 elif self.rect.bottom >= 600: self.rect.bottom = 600 #Check if enemy bullets hit player if pygame.sprite.groupcollide(playerSprite, enemyBulletSprites, 0, 1): self.life -= 1 if self.life <= 0: self.kill() def reset(self): self.rect.bottom = 600 self.firespeed = 25 def firespeed_up(self): self.firespeed -= 5 if self.firespeed <= 5: self.firespeed == 5 #Enemy class class Enemy(pygame.sprite.Sprite): def __init__(self,centerx): pygame.sprite.Sprite.__init__(self) self.image, self.rect = get_plane((20,20),(150,150,255)) self.reset() def update(self): self.rect.centerx += self.dx self.rect.centery += self.dy if self.rect.top > screen.get_height(): self.reset() self.counter += 1 if self.counter >= 60: enemyBulletSprites.add(EnemyBullet(self.rect.midbottom)) self.counter = 0 #Check if enemy plane touch player bullets if pygame.sprite.groupcollide(enemySprites, bulletSprites, 1, 1): #play explode movie and music pass def reset(self): self.counter = random.randint(0, 60) self.rect.bottom = 0 self.rect.centerx = random.randrange(0, screen.get_width()) self.dy = random.randrange(1, 4) self.dx = random.randrange(-2, 2) #Bullet class class Bullet(pygame.sprite.Sprite): def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.image, self.rect = get_plane((5, 5),(255,255,255)) self.rect.center = pos def update(self): if self.rect.top < 0: self.kill() else: self.rect.move_ip(0, -5) #Enemy bullet class class EnemyBullet(pygame.sprite.Sprite): def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.image, self.rect = get_plane((5, 5),(150,150,255)) self.rect.center = pos def update(self): if self.rect.top < 0: self.kill() else: self.rect.move_ip(0, 5) #Class Module class SpaceMenu: #Define the initalize self options def __init__(self, *options): self.options = options self.x = 0 self.y = 0 self.font = pygame.font.Font(None, 32) self.option = 0 self.width = 1 self.color = [0, 0, 0] self.hcolor = [0, 0, 0] self.height = len(options)*self.font.get_height() for o in options: text = o[0] ren = self.font.render(text, 1, (0, 0, 0)) if ren.get_width() > self.width: self.width = ren.get_width() #Draw the menu def draw(self, surface): i = 0 for o in self.options: if i == self.option: clr = self.hcolor else: clr = self.color text = o[0] ren = self.font.render(text, 1, clr) if ren.get_width() > self.width: self.width = ren.get_width() surface.blit(ren, (self.x, self.y + i * self.font.get_height())) i += 1 #Handle events def update(self, events): for e in events: if e.type == pygame.KEYDOWN: if e.key == pygame.K_UP: self.option -= 1 elif e.key == pygame.K_DOWN: self.option += 1 elif e.key == pygame.K_RETURN: self.options[self.option][1]() if self.option > len(self.options) - 1: self.option = 0 elif self.option < 0: self.option = len(self.options) - 1 #Position of menu def set_pos(self, x, y): self.x = x self.y = y #Font Style def set_font(self, font): self.font = font for o in self.options: text = o[0] ren = self.font.render(text, 1, (0, 0, 0)) if ren.get_width() > self.width: self.width = ren.get_width() #Highlight Color def set_highlight_color(self, color): self.hcolor = color #Font Color def set_normal_color(self, color): self.color = color #Font position def center_at(self, x, y): self.x = x-(self.width/2) self.y = y-(self.height/2) def game(): #Game Object #global player player = Player() font = pygame.font.Font(None,32) #Player/enemy global playerSprite playerSprite = pygame.sprite.RenderPlain((player)) global enemySprites enemySprites = pygame.sprite.RenderPlain(()) enemySprites.add(Enemy(200)) enemySprites.add(Enemy(300)) enemySprites.add(Enemy(400)) #Projectiles global bulletSprites bulletSprites = pygame.sprite.RenderPlain(()) global enemyBulletSprites enemyBulletSprites = pygame.sprite.RenderPlain(()) clock = pygame.time.Clock() counter = 0 keepGoing = True while keepGoing: clock.tick(30) #Handle input for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: keepGoing = False elif event.key == pygame.K_UP: player.dy = -10 elif event.key == pygame.K_DOWN: player.dy = 10 elif event.key == pygame.K_LEFT: player.dx = -10 elif event.key == pygame.K_RIGHT: player.dx = 10 elif event.type == pygame.KEYUP: if event.key == pygame.K_UP: player.dy = 0 elif event.key == pygame.K_DOWN: player.dy = 0 elif event.key == pygame.K_LEFT: player.dx = 0 elif event.key == pygame.K_RIGHT: player.dx = 0 #Update screen.blit(background, (0,0)) playerSprite.update() enemySprites.update() bulletSprites.update() enemyBulletSprites.update() text = "LIFE:" + str(player.life) life = font.render(text, True, (255,255,255)) #Draw playerSprite.draw(screen) enemySprites.draw(screen) bulletSprites.draw(screen) enemyBulletSprites.draw(screen) screen.blit(life,(0,0)) pygame.display.flip() #Create new enemies counter += 1 if counter >= 20 and len(enemySprites) < 20: enemySprites.add(Enemy(300)) counter = 0 #if game over if len(playerSprite) == 0: gameOver() keepGoing = False def gameOver(): #Game over screen menuTitle = SpaceMenu( ["GAME OVER"]) menuTitle.set_font(pygame.font.Font(None, 80)) menuTitle.center_at(400,230) menuTitle.set_highlight_color((255,255,255)) info = SpaceMenu( ["Press ESC back to menu"]) info.set_font(pygame.font.Font(None, 40)) info.center_at(400,350) info.set_highlight_color((255,255,255)) keepGoing = True while keepGoing: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: keepGoing = False elif event.type == pygame.QUIT: keepGoing = False menuTitle.draw(screen) info.draw(screen) pygame.display.flip() def aboutMenu(): #About Menu Text #Title for Option Menu menuTitle = SpaceMenu( ["Space Shooter"]) info = SpaceMenu( [""], ["My Space Shooter"], [""], ["Use arrow key to move your plane."], [""], ["Contact me"], [""], ["qq:289976627"], ["gmail:[email protected]"], [""], [" PRESS ESC TO RETURN "]) #About Title Font color, aligment, and font type menuTitle.set_font(pygame.font.Font(None, 60)) menuTitle.center_at(400, 150) menuTitle.set_highlight_color((255, 255, 255)) #About info Font color, aligment, and font type info.center_at(400, 320) info.set_highlight_color((255, 255, 255)) info.set_normal_color((200, 200, 255)) clock = pygame.time.Clock() keepGoing = True while keepGoing: clock.tick(30) #Handle input for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: keepGoing = False elif event.type == QUIT: keepGoing = False #Draw screen.blit(background, (0,0)) menuTitle.draw(screen) info.draw(screen) pygame.display.flip() #Functions def option1(): game() def option2(): aboutMenu() def option3(): pygame.quit() sys.exit() #Main def main(): menuTitle = SpaceMenu( ["My Space Shooter"]) menu = SpaceMenu( ["Start", option1], ["About", option2], ["Exit", option3]) #Title menuTitle.set_font(pygame.font.Font(None, 60)) menuTitle.center_at(400, 150) menuTitle.set_highlight_color((255, 255, 255)) #Menu settings menu.center_at(400, 320) menu.set_highlight_color((255, 255, 255)) menu.set_normal_color((200, 200, 255)) clock = pygame.time.Clock() keepGoing = True while True: clock.tick(30) #Events events = pygame.event.get() #Update Menu menu.update(events) #Handle quit event for e in events: if e.type == pygame.QUIT: pygame.quit() return #Draw screen.blit(background, (0, 0)) menu.draw(screen) menuTitle.draw(screen) pygame.display.flip() if __name__ == "__main__": main()
第二个:
键盘输入是几乘几的矩阵,回车确定
上下左右键移动方块,把全部方块按顺序拼回去就算结束。话说不是每个局面都一定能拼回去。。公开课的作业还有个黑客版本,就是威力加强版,要求做出来自动解题算法。我对着棋盘琢磨了大半天,无奈能力有限。。有哪位肯赐教的,不胜感激!
————————————————————————
几经周折了解到了这个叫做八数码问题,而且好像还是蛮经典的。。
职高真是坑爹,一定要插本,学历倒是其次,主要是这眼界太搓了。。
学校似乎是觉得我们出去能帮人家做个网站拼个音乐播放器什么的就实现了人生目标了。。我可不想这么着。。
(2012/10/13编辑)
————————————————————————
代码:
#! /usr/bin/env python #the game of fifteen #qq:289976627 gmail:[email protected]. #import import random import pygame from pygame.locals import * from sys import exit WHITE = (255, 255, 255) BLUE = (150, 150, 255) NUMPIECEWIDTH = 50 #init pygame.init() global screen screen = pygame.display.set_mode((800, 600)) # NumPiece class class NumPiece(pygame.Surface): font = pygame.font.Font(None, 30) def __init__(self, num): pygame.surface.Surface.__init__(self, (NUMPIECEWIDTH, NUMPIECEWIDTH)) self.fill(BLUE) text = NumPiece.font.render(str(num), 1, WHITE) textRect = text.get_rect() textRect.center = (NUMPIECEWIDTH / 2, NUMPIECEWIDTH / 2) self.blit(text, textRect.topleft) #the game() function def game(): numPieces = [] tmpPieces = range(1, num * num) while len(tmpPieces) > 1: numPieces.append(NumPiece(tmpPieces.pop(random.randint(0, len(tmpPieces) - 1)))) numPieces.append(NumPiece(tmpPieces.pop())) numPieces.append(0) now = num * num - 1 while True: #Handle event event = pygame.event.wait() if event.type == QUIT: pygame.quit() exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() exit() elif event.key == K_UP: if now / num > 0: numPieces[now] = numPieces[now - num] numPieces[now - num] = 0 now = now - num elif event.key == K_DOWN: if now / num < num - 1: numPieces[now] = numPieces[now + num] numPieces[now + num] = 0 now = now + num elif event.key == K_LEFT: if now % num > 0: numPieces[now] = numPieces[now - 1] numPieces[now - 1] = 0 now = now - 1 elif event.key == K_RIGHT: if now % num < num - 1: numPieces[now] = numPieces[now + 1] numPieces[now + 1] = 0 now = now + 1 #Draw screen.fill(WHITE) #Draw NumPieces startPoint = (0, 0) counter = 0 for np in numPieces: if np == 0: counter += 1 continue else: xy = (startPoint[0] + (counter%num) * (NUMPIECEWIDTH + 10), startPoint[1] + (counter/num) * (NUMPIECEWIDTH + 10)) screen.blit(np, xy) counter += 1 pygame.display.flip() #main def main(): font = pygame.font.Font(None, 60) global num num = 0 clock = pygame.time.Clock() while True: clock.tick(30) #Handle input event = pygame.event.wait() if event.type == QUIT: pygame.quit() exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() exit() elif event.key in range(48, 57 + 1): num = num * 10 + event.key - 48 elif event.key == K_RETURN: game() #Update text = font.render(str(num), 1, BLUE) textRect = text.get_rect() textRect.center = (screen.get_width() / 2, screen.get_height() / 2) #Draw screen.fill(WHITE) screen.blit(text, (textRect.topleft)) pygame.display.flip() #start here if __name__ == "__main__": main()