Python pygame is the first real game!

Source: Internet
Author: User
#-*-Coding: UTF-8-*-import pygame, sysfrom pygame. locals import * class myballclass (pygame. sprite. sprite): def _ init _ (self, image_file, speed, location): 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 <0 or self. rect. right> screen. get_width (): Self. speed [0] =-self. speed [0] If self. rect. top <= 0: Self. speed [1] =-self. speed [1] def over (Self): Self. rect = self. rect. move (self. speed) If self. rect. bottom> screen. get_height (): font = pygame. font. sysfont ("", 40) text_surface = font. render (U "game over", true, (0, 0, 255) screen. BITs (text_surface, (320,240) return 0 class mypaddleclass (pygame. sprite. sprite): def _ init _ (self, location): pygame. sprite. sprite. _ init _ (Self) image_surface = pygame. surface. surface ([100, 20]) # control the size of the blue tag image_surface.fill ([213,213,123]) # control the color self. image = image_surface.convert () self. rect = self. image. get_rect () self. rect. left, self. rect. top = locationpygame. init () screen = pygame. display. set_mode ([1280,960]) background = pygame. image. load ("6.jpg "). convert () Clock = pygame. time. clock () ball_speed = [4,-4] score = 0 # shift direction control, when the parameter is set to a correct value and the larger the value moves faster, T # The last parameter controls the initial position of the image. myball = myballclass('5.png ', ball_speed, []) ballgroup = pygame. sprite. group (myball) paddle = mypaddleclass ([270,400]) # control the initial position of the blue Tag time = 20 While true: For event in pygame. event. get (): If event. type = pygame. quit: SYS. exit () Elif event. type = pygame. mousemotion: paddle. rect. centerx = event. pos [0] If pygame. sprite. spritecollide (paddle, ballgroup, 0): myball. speed [1] =-myball. speed [1] time = Time + 1 score = score + 10 clock. tick (time) screen. fill ([255,255,255]) font = pygame. font. sysfont ("", 40) text_surface = font. render (u'fractional '+ STR (score), true, (255,) screen. BITs (text_surface, () # usage of the open () parameter A + continuous write does not overwrite, R + writes the last one, W + writes the full coverage of scorefile1_open('a.txt ', 'r + ') for a in scorefile. readline (10): text_surface1 = font. render (u'highest record '+ STR (A), true, (255,) screen. BITs (text_surface1, (320,24) print "woqu" print a print "Nima" Print score if (a <score): print a * a print score scorefile. flush () scorefile. writelines (STR (score) scorefile. close () myball. move () myball. over () screen. BITs (myball. image, myball. rect) screen. BITs (paddle. image, paddle. rect) pygame. display. flip ()

This is an Iot platform game! The speed keeps getting faster after the connection is received until the connection fails.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.