This Python code implements a basic Pygame game where an image can be moved left and right using arrow keys. The code includes initialization, event handling, image rendering, and collision detection. It demonstrates the following concepts:

  • Initialization: Setting up Pygame, creating the game window, loading an image, and defining game objects.
  • Event Handling: Processing events like keyboard presses and window closure.
  • Image Rendering: Displaying the image on the screen.
  • Movement: Updating the image's position based on keyboard input.
  • Collision Detection: Checking if the image collides with a designated area.

The code is well-structured and follows common Pygame practices. It's a good starting point for learning about basic game development concepts using Pygame.

import sys
import pygame
import random
from pygame.locals import (\n    K_UP,\n    K_DOWN,\n    K_LEFT,\n    K_RIGHT,\n    K_ESCAPE,\n    K_SPACE,\n    KEYDOWN,\n    KEYUP,\n    QUIT,\n)
class Game():
    def __init__(self):
        pygame.init()
        pygame.display.set_caption("test game")
        self.screen = pygame.display.set_mode((1440,900))
        self.clock = pygame.time.Clock()
        self.img = pygame.image.load("picture/020.00.00.png").convert()
        self.img_pos = [100,200]
        #[left,right]
        self.count = [False, False]
        #pygame.Rect(left,top,width,height)
        self.collision_area = pygame.Rect(200,0,100,900)
    def run(self):
        while True:
            self.screen.fill((255,255,255))
            self.screen.blit(self.img,self.img_pos)
            self.img_pos[0] += (self.count[1] - self.count[0])*5
            img_r = pygame.Rect(self.img_pos[0],self.img_pos[1],self.img.get_width(),self.img.get_height())
            if img_r.collidedict(self.collision_area):
                pygame.draw.rect(self.screen,(0,0,0),self.collision_area)
            else:
                pygame.draw.rect(self.screen,(100,100,100),self.collision_area)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
                    if event.key == K_LEFT:
                        self.count[0] = True
                    if event.key == K_RIGHT:
                        self.count[1] = True
                if event.type == KEYUP:
                    if event.key == K_LEFT:
                        self.count[0] = False
                    if event.key == K_RIGHT:
                        self.count[1] = False
            pygame.display.flip()
            #frame number
            self.clock.tick(10)
game = Game()
game.run()

The code demonstrates fundamental Pygame concepts, making it a suitable starting point for those learning to develop simple games with Pygame.

Python Pygame Game: Simple Image Movement and Collision Detection

原文地址: http://www.cveoy.top/t/topic/o9ll 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录