最近開始學python,感覺挺好玩的,既有指令碼語言的靈活性,又有豐富的類庫與物件導向的特點,開發起來很方便。
遊戲的規則和桌球一樣,如果妙蛙種子掉地上了就算輸,你可以用藍色的蹺蹺板彈它,使他不落到地面上。 game over後可按任意鍵繼續遊戲或選擇退出。**如下:
import sys, pygamefrom random import *
from pygame.locals import *
from pygame.font import *
class myplayer(pygame.sprite.sprite):
def __init__(self, image_file, location, speed):
pygame.sprite.sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed
self.status = true
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left width:
self.speed[0] = -self.speed[0]
if self.rect.top
self.speed[1] = -self.speed[1]
if self.rect.bottom > height:
#game over
self.status = false
class reflector(pygame.sprite.sprite):
def __init__(self, image_file, location, speed):
pygame.sprite.sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left width:
self.speed[0] = -self.speed[0]
if self.rect.top height:
self.speed[1] = -self.speed[1]
def animate(players):
screen.fill([255,255,255])
for player in players:
player.move()
for player in players:
players.remove(player)
if pygame.sprite.spritecollide(player,players,false):
player.speed[0] = -player.speed[0]
player.speed[1] = -player.speed[1]
players.add(player)
player.move()
screen.blit(player.image,player.rect)
pygame.display.flip()
pygame.time.delay(10)
pygame.init()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
pygame.display.set_caption("miaowa game")
def play():
img_player = "c:\users\dswu\desktop\player.png"
players = pygame.sprite.group()
for row in range(0,1):
for column in range(0,1):
playerlocation = [column*250+10,row*250+10]
playerspeed = [choice([-2,2]), choice([-2,2])]
player = myplayer(img_player, playerlocation, playerspeed)
players.add(player)
img_ref_path = "c:\users\dswu\desktop\reflector.png"
ref_pos = [0,464]
ref_speed = [0,0]
reflector = reflector(img_ref_path, ref_pos, ref_speed)
players.add(reflector)
running = true
while running:
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.quit:
game.quit()
if event.type == keydown:
if event.key == k_left:
ref_speed[0] = -2
elif event.key == k_right:
ref_speed[0] = +2
animate(players)
if player.status == false:
running = false
final_text = "game over!"
ft_font = pygame.font.font(none, 100)
ft_surf = ft_font.render(final_text, 1, (0,0,0))
screen.blit(ft_surf, [screen.get_width()/2 - ft_surf.get_width()/2, 100])
tip_text = "type any key to continue"
tip_font = pygame.font.font(none, 50)
tip_surf = tip_font.render(tip_text, 1, (0,0,0))
screen.blit(tip_surf, [screen.get_width()/2 - tip_surf.get_width()/2, 200])
pygame.display.flip()
keepon = true
while keepon:
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.quit:
pygame.quit()
if event.type == keydown:
play()
play()
python寫乙個服務 Python寫乙個服務
coding utf 8 import json from urllib.parse import parse qs from wsgiref.server import make server 定義函式,引數是函式的兩個引數,都是python本身定義的,預設就行了。定義檔案請求的型別和當前請求成功...
寫乙個logger的類
參考 寫乙個logger的類 import logging 建立日誌物件 class logger def init self,logpath 制定log格式 format logging.formatter asctime s filename s name s line lineno d mes...
python寫乙個incaseformat檢測工具
1.13日爆發的incaseformat病毒會在對應的系統時間觸發攻擊行為,能在短時間內刪除除了系統盤之外的所有檔案。據研究人員稱該病毒會在1.23再次爆發,小r根據該病毒生成特點製作了乙個簡單的檢測工具。程式設計環境 python3.7 from tkinter import import os....